Java Code Examples for org.ojai.Value#getString()

The following examples show how to use org.ojai.Value#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Values.java    From ojai with Apache License 2.0 6 votes vote down vote up
/**
 * @return The specified value as a <code>boolean</code>.
 * @throws IllegalArgumentException if the specified value is
 * not can not be converted to a boolean.
 */
public static boolean asBoolean(@NonNullable Value value) {
  Preconditions.checkNotNull(value);
  switch (value.getType()) {
  case NULL:
    return false;
  case BOOLEAN:
    return value.getBoolean();
  case STRING:
    final String strValue = value.getString();
    if (strValue.equalsIgnoreCase("true")) {
      return true;
    } else if (strValue.equalsIgnoreCase("false")) {
      return false;
    }
    break;
  default:
    if (value.getType().isNumeric()) {
      return 0.0 != asNumber(value).doubleValue();
    }
    // falls through to throw TypeException
  }
  throw new TypeException(value.asJsonString() + " can not be converted to a Boolean value.");
}
 
Example 2
Source File: Values.java    From ojai with Apache License 2.0 6 votes vote down vote up
/**
 * @return The specified value as a <code>String</code>.
 */
public static String asString(@NonNullable Value value) {
  Preconditions.checkNotNull(value);
  switch (value.getType()) {
  case NULL:
    return null;
  case STRING:
    return value.getString();
  case DATE:
    return value.getDate().toDateStr();
  case INTERVAL:
    return String.valueOf(value.getInterval().getTimeInMillis());
  case TIME:
    return value.getTime().toTimeStr();
  case TIMESTAMP:
    return value.getTimestamp().toUTCString();
  default:
    return Json.toJsonString(value.asReader(), JsonOptions.WITHOUT_TAGS);
  }
}
 
Example 3
Source File: Values.java    From ojai with Apache License 2.0 5 votes vote down vote up
/**
 * @return The specified value as a <code>Number</code>.
 * This may involve rounding or truncation.
 * @throws IllegalArgumentException if the specified value is
 * not one of the number types or a string that can not be converted to a number.
 */
public static Number asNumber(@NonNullable Value value) {
  Preconditions.checkNotNull(value);
  switch (value.getType()) {
  case BYTE:
    return value.getByte();
  case SHORT:
    return value.getShort();
  case INT:
    return value.getInt();
  case LONG:
    return value.getLong();
  case FLOAT:
    return value.getFloat();
  case DOUBLE:
    return value.getDouble();
  case DECIMAL:
    return value.getDecimal();
  case STRING:
    try {
      return new BigDecimal(value.getString());
    } catch (NumberFormatException e) {}
    // falls through to throw TypeException
  default:
    throw new TypeException(value.asJsonString() + " can not be converted to a Number.");
  }
}