Java Code Examples for org.ojai.Value#Type

The following examples show how to use org.ojai.Value#Type . 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: TestTypeMappedJsonDocumentReader.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeMappingsMultiLevelWithTags() throws IOException {
  Map<FieldPath, Value.Type> fieldPathTypeMap = Maps.newHashMap();
  fieldPathTypeMap.put(FieldPath.parseFrom("map.boolean"), Value.Type.BOOLEAN);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.string"), Value.Type.STRING);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.byte"), Value.Type.BYTE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.short"), Value.Type.SHORT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.int"), Value.Type.INT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.long"), Value.Type.LONG);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.float"), Value.Type.FLOAT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.double"), Value.Type.DOUBLE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.decimal"), Value.Type.DECIMAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.date"), Value.Type.DATE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.time"), Value.Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.timestamp"), Value.Type.TIMESTAMP);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.interval"), Value.Type.INTERVAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.binary"), Value.Type.BINARY);

  try (InputStream in = getJsonStream("org/ojai/test/data/test4.json");
       DocumentStream jsonRecordStream = Json.newDocumentStream(in, fieldPathTypeMap)) {
    Iterator<Document> docIterator = jsonRecordStream.iterator();
    assertTrue(docIterator.hasNext());
    Document doc = docIterator.next();
    assertTrue(doc.getBoolean("map.boolean"));
    assertEquals("eureka", doc.getString("map.string"));
    assertEquals((byte) 127, doc.getByte("map.byte"));
    assertEquals((short) 32767, doc.getShort("map.short"));
    assertEquals(2147483647, doc.getInt("map.int"));
    assertEquals(9223372036854775807L, doc.getLong("map.long"));
    assertEquals((float) 3.4028235, doc.getFloat("map.float"), 0);
    assertEquals(1.7976931348623157e308, doc.getDouble("map.double"), 0);
    assertEquals(ODate.parse("2012-10-20"), doc.getDate("map.date"));
    assertEquals(OTime.parse("07:42:46.123"), doc.getTime("map.time"));
    assertEquals(OTimestamp.parse("2012-10-20T07:42:46.123-07:00"), doc.getTimestamp("map.timestamp"));
    assertEquals(new OInterval(172800000), doc.getInterval("map.interval"));
    assertEquals(Values.parseBinary("YWJjZA=="), doc.getBinary("map.binary"));
  }
}
 
Example 2
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 3
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;
}
 
Example 4
Source File: TestTypeMappedJsonDocumentReader.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testTypeMappings() throws IOException {
  Map<FieldPath, Value.Type> fieldPathTypeMap = Maps.newHashMap();
  fieldPathTypeMap.put(FieldPath.parseFrom("null"), Value.Type.NULL);
  fieldPathTypeMap.put(FieldPath.parseFrom("boolean"), Value.Type.BOOLEAN);
  fieldPathTypeMap.put(FieldPath.parseFrom("string"), Value.Type.STRING);
  fieldPathTypeMap.put(FieldPath.parseFrom("byte"), Value.Type.BYTE);
  fieldPathTypeMap.put(FieldPath.parseFrom("short"), Value.Type.SHORT);
  fieldPathTypeMap.put(FieldPath.parseFrom("int"), Value.Type.INT);
  fieldPathTypeMap.put(FieldPath.parseFrom("long"), Value.Type.LONG);
  fieldPathTypeMap.put(FieldPath.parseFrom("float"), Value.Type.FLOAT);
  fieldPathTypeMap.put(FieldPath.parseFrom("double"), Value.Type.DOUBLE);
  fieldPathTypeMap.put(FieldPath.parseFrom("decimal"), Value.Type.DECIMAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("date"), Value.Type.DATE);
  fieldPathTypeMap.put(FieldPath.parseFrom("time"), Value.Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("timestamp"), Value.Type.TIMESTAMP);
  fieldPathTypeMap.put(FieldPath.parseFrom("interval"), Value.Type.INTERVAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("binary"), Value.Type.BINARY);

  try (InputStream in = getJsonStream("org/ojai/test/data/test3.json");
       DocumentStream stream = Json.newDocumentStream(in, fieldPathTypeMap)) {
    for (Document document : stream) {
      Value value = document.getValue("null");
      assertNotNull(value);
      assertEquals(Type.NULL, value.getType());
      assertEquals(null, value.getObject());

      value = document.getValue("boolean");
      assertNotNull(value);
      assertEquals(Type.BOOLEAN, value.getType());
      assertEquals(true, value.getObject());

      value = document.getValue("string");
      assertNotNull(value);
      assertEquals(Type.STRING, value.getType());
      assertEquals("123456789012345678901234567890123456789012345678901234567890", value.getString());

      value = document.getValue("byte");
      assertNotNull(value);
      assertEquals(Type.BYTE, value.getType());
      assertEquals(127, value.getByte());

      value = document.getValue("short");
      assertNotNull(value);
      assertEquals(Type.SHORT, value.getType());
      assertEquals(32767, value.getShort());

      value = document.getValue("int");
      assertNotNull(value);
      assertEquals(Type.INT, value.getType());
      assertEquals(2147483647, value.getInt());

      value = document.getValue("long");
      assertNotNull(value);
      assertEquals(Type.LONG, value.getType());
      assertEquals(9223372036854775807L, value.getLong());

      value = document.getValue("float");
      assertNotNull(value);
      assertEquals(Type.FLOAT, value.getType());
      assertEquals(3.4028235, value.getFloat(), 0.0000001);

      value = document.getValue("double");
      assertNotNull(value);
      assertEquals(Type.DOUBLE, value.getType());
      assertEquals(1.7976931348623157e308, value.getDouble(), 0.00000000000001);

      value = document.getValue("decimal");
      assertNotNull(value);
      assertEquals(Type.DECIMAL, value.getType());
      assertEquals(new BigDecimal("123456789012345678901234567890123456789012345678901.23456789"), value.getDecimal());

      value = document.getValue("date");
      assertNotNull(value);
      assertEquals(Type.DATE, value.getType());
      assertEquals(ODate.parse("2012-10-20"), value.getDate());

      value = document.getValue("time");
      assertNotNull(value);
      assertEquals(Type.TIME, value.getType());
      assertEquals(OTime.parse("07:42:46.123"), value.getTime());

      value = document.getValue("timestamp");
      assertNotNull(value);
      assertEquals(Type.TIMESTAMP, value.getType());
      assertEquals(OTimestamp.parse("2012-10-20T07:42:46.123-07:00"), value.getTimestamp());

      value = document.getValue("interval");
      assertNotNull(value);
      assertEquals(Type.INTERVAL, value.getType());
      assertEquals(new OInterval(172800000), value.getInterval());

      value = document.getValue("binary");
      assertNotNull(value);
      assertEquals(Type.BINARY, value.getType());
      assertEquals(Values.parseBinary("YWJjZA=="), value.getBinary());
    }
  }
}
 
Example 5
Source File: QueryCondition.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a condition that tests if the {@code Value} at the specified
 * {@code FieldPath} is of the specified {@code Type}.
 *
 * @param path the {@code FieldPath} to test
 * @return {@code this} for chained invocation
 */
public QueryCondition typeOf(@NonNullable String path, @NonNullable Value.Type type);
 
Example 6
Source File: QueryCondition.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a condition that tests if the {@code Value} at the specified
 * {@code FieldPath} is of the specified {@code Type}.
 *
 * @param path the {@code FieldPath} to test
 * @return {@code this} for chained invocation
 */
public QueryCondition typeOf(@NonNullable FieldPath path, @NonNullable Value.Type type);
 
Example 7
Source File: QueryCondition.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a condition that tests if the {@code Value} at the specified
 * {@code FieldPath} is not of the specified {@code Type}.
 *
 * @param path the {@code FieldPath} to test
 * @return {@code this} for chained invocation
 */
public QueryCondition notTypeOf(@NonNullable String path, @NonNullable Value.Type type);
 
Example 8
Source File: QueryCondition.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a condition that tests if the {@code Value} at the specified
 * {@code FieldPath} is not of the specified {@code Type}.
 *
 * @param path the {@code FieldPath} to test
 * @return {@code this} for chained invocation
 */
public QueryCondition notTypeOf(@NonNullable FieldPath path, @NonNullable Value.Type type);