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

The following examples show how to use org.ojai.Value#getType() . 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: JsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
private JsonDocumentBuilder iterDocument(Iterator<Entry<String, Value>> it) {
  while (it.hasNext()) {
    Entry<String, Value> kv = it.next();
    String key = kv.getKey();
    Value value = kv.getValue();
    if (value.getType() == MAP) {
      putNewMap(key);
      iterDocument(((Document) value).iterator());
    } else if (value.getType() == ARRAY) {
      putArray(key, value.getList());
    } else {
      // process element.
      put(key, value);
    }
  }
  endMap();
  return this;
}
 
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>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 3
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 4
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.");
  }
}
 
Example 5
Source File: MapRDBCDCSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void setId(Record record, Value id) throws StageException {
  if(id.getType() == Value.Type.STRING) {
    record.set("/_id", Field.create(id.getString()));
  } else if(id.getType() == Value.Type.BINARY) {
    record.set("/_id", Field.create(id.getBinary().array()));
  } else {
    throw new OnRecordErrorException(record, MaprDBCDCErrors.MAPRDB_04, id.getType().name());
  }
}
 
Example 6
Source File: MapRDBCDCSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private Field generateField(Value value) {
  switch (value.getType()) {
    case INT:
      return Field.create(value.getInt());
    case LONG:
      return Field.create(value.getLong());
    case SHORT:
      return Field.create(value.getShort());
    case BOOLEAN:
      return Field.create(value.getBoolean());
    case DECIMAL:
      return Field.create(value.getDecimal());
    case BYTE:
      return Field.create(value.getByte());
    case DATE:
      return Field.createDate(value.getDate().toDate());
    case FLOAT:
      return Field.create(value.getFloat());
    case DOUBLE:
      return Field.create(value.getDouble());
    case STRING:
      return Field.create(value.getString());
    case MAP:
      return Field.create(value.getMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, this::generateField)));
    case TIME:
      return Field.createTime(value.getTime().toDate());
    case ARRAY:
      return Field.create(value.getList().stream().map(this::generateField).collect(Collectors.toList()));
    case BINARY:
      return Field.create(value.getBinary().array());
    case TIMESTAMP:
      return Field.createDatetime(value.getTimestamp().toDate());
    case INTERVAL:
      return Field.create(value.getInterval().getTimeInMillis());
    default:
      throw new IllegalArgumentException("Unsupported type " + value.getType().toString());
  }
}
 
Example 7
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, Value value) {
  Value.Type t = value.getType();
  switch (t) {
  case NULL:
    putNull(field);
    break;
  case BOOLEAN:
    put(field, value.getBoolean());
    break;
  case STRING:
    put(field, value.getString());
    break;
  case BYTE:
    put(field, value.getByte());
    break;
  case SHORT:
    put(field, value.getShort());
    break;
  case INT:
    put(field, value.getInt());
    break;
  case LONG:
    put(field, value.getLong());
    break;
  case FLOAT:
    put(field, value.getFloat());
    break;
  case DOUBLE:
    put(field, value.getDouble());
    break;
  case DECIMAL:
    put(field, value.getDecimal());
    break;
  case DATE:
    put(field, value.getDate());
    break;
  case TIME:
    put(field, value.getTime());
    break;
  case TIMESTAMP:
    put(field, value.getTimestamp());
    break;
  case INTERVAL:
    put(field, value.getInterval());
    break;
  case BINARY:
    put(field, value.getBinary());
    break;
  case MAP:
    put(field, (Document)value);
    break;
  case ARRAY:
    putArray(field, value.getList());
    break;
  default:
    break;
  }
  return this;
}
 
Example 8
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder add(Value value) {
  Value.Type t = value.getType();
  switch (t) {
  case NULL:
    addNull();
    break;
  case BOOLEAN:
    add(value.getBoolean());
    break;
  case STRING:
    add(value.getString());
    break;
  case BYTE:
    add(value.getByte());
    break;
  case SHORT:
    add(value.getShort());
    break;
  case INT:
    add(value.getInt());
    break;
  case LONG:
    add(value.getLong());
    break;
  case FLOAT:
    add(value.getFloat());
    break;
  case DOUBLE:
    add(value.getDouble());
    break;
  case DATE:
    add(value.getDate());
    break;
  case TIME:
    add(value.getTime());
    break;
  case TIMESTAMP:
    add(value.getTimestamp());
    break;
  case INTERVAL:
    add(value.getInterval());
    break;
  case BINARY:
    add(value.getBinary());
    break;
  case MAP:
    add((Document)value);
    break;
  case DECIMAL:
    add(value.getDecimal());
    break;
  case ARRAY:
    putArray(null, value.getList());
    break;
  default:
    throw new IllegalStateException("Unknown object type");
  }

  return this;
}