Java Code Examples for org.ojai.Document#getValue()
The following examples show how to use
org.ojai.Document#getValue() .
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: TestDelegatingJsonDocumentReader.java From ojai with Apache License 2.0 | 5 votes |
@Test public void testFieldTransformation() throws IOException { final Events.Delegate EVENTDELEGATE = new BaseDelegate() { @Override public boolean process(DocumentReader reader, EventType event, Queue<EventDescriptor> eventQueue) { if (reader.inMap() && event == EventType.DOUBLE && reader.getFieldName().equals("age")) { // replace a DOUBLE with a BYTE value eventQueue.offer(new EventDescriptor(EventType.BYTE) .setFieldName("ageB") .setValue(JsonValueBuilder.initFrom((byte)reader.getDouble()))); return true; } return false; } }; try (InputStream in = getJsonStream("org/ojai/test/data/complex.json"); DocumentStream stream = Json.newDocumentStream(in, EVENTDELEGATE)) { for (Document document : stream) { Value ageValue = document.getValue("age"); assertNull(ageValue); ageValue = document.getValue("ageB"); assertNotNull(ageValue); assertEquals(Type.BYTE, ageValue.getType()); assertEquals(23, ageValue.getByte()); } } }
Example 2
Source File: Documents.java From ojai with Apache License 2.0 | 4 votes |
/** * Returns the value at the specified fieldPath as a {@link Value} or the * specified {@code defaultValue} if the specified {@code FieldPath} does not * exist in the document. */ public static Value getValue(@NonNullable Document document, @NonNullable FieldPath fieldPath, @Nullable Value defaultValue) { Value docValue = document.getValue(fieldPath); return docValue != null ? docValue : defaultValue; }
Example 3
Source File: TestJsonDocumentEquals.java From ojai with Apache License 2.0 | 4 votes |
@Test public void testFieldsEquals() { Document rec = Json.newDocument(); rec.set("map.field1", (byte) 100); rec.set("map.field2", (short) 10000); rec.set("map.string", "eureka"); rec.set("map.boolean", false); rec.set("map.date", ODate.parse("2013-02-12")); OTime time = OTime.parse("07:42:46"); rec.set("map.time", time); OTimestamp timeStamp = OTimestamp.parse("2012-10-20T07:42:46"); rec.set("map.timestamp", timeStamp); rec.set("map.int", 12345678); byte[] byteArray = "abracadabra".getBytes(); rec.set("map.bytearray", byteArray); rec.setNull("map.null"); Map<String, Object> m = new HashMap<String, Object>(); m.put("a", 500); m.put("b", "abcd"); List<Object> newlist = new ArrayList<Object>(); newlist.add("aaaaa"); newlist.add(1234567.89); m.put("c",newlist); rec.set("map2", m); List<Object> l = new ArrayList<Object>(); l.add(12345.678901); l.add("abracadabra"); Map<String, Object> m2 = new HashMap<String, Object>(); m2.put("a1", 111); m2.put("b1", false); l.add(m2); rec.set("list1", l); Document rec2 = rec; assertEquals(true, rec2.equals(rec)); Value stringValue = rec.getValue("map.string"); assertEquals(true, stringValue.equals("eureka")); byte b = rec.getByte("map.field1"); assertEquals(true, rec.getValue("map.field1").equals(b)); Byte bite = new Byte(b); assertEquals(true, bite.equals(rec.getByte("map.field1"))); short s = rec.getShort("map.field2"); assertEquals(true, rec.getValue("map.field2").equals(s)); assertEquals(true, rec.getValue("map.field2").equals(JsonValueBuilder.initFrom(s))); assertEquals(true, rec.getValue("map.boolean").equals(false)); assertEquals(true, rec.getValue("map.date").equals(ODate.parse("2013-02-12"))); assertEquals(true, rec.getValue("map2").equals(m)); int x = rec.getInt("map.int"); assertEquals(true, rec.getValue("map.int").equals(x)); assertEquals(true, rec.getList("list1").equals(l)); assertEquals(true, rec.getValue("map.time").equals(time)); assertEquals(true, rec.getValue("map.timestamp").equals(timeStamp)); assertEquals(true, rec.getValue("map.bytearray").equals(ByteBuffer.wrap(byteArray))); Value myValue; myValue = rec.getValue("map.date"); assertEquals(true, rec.getValue("map.date").equals(myValue)); myValue = rec.getValue("map.time"); assertEquals(true, rec.getValue("map.time").equals(myValue)); myValue = rec.getValue("map.timestamp"); assertEquals(true, rec.getValue("map.timestamp").equals(myValue)); myValue = rec.getValue("list1"); assertEquals(true,rec.getValue("list1").equals(myValue)); myValue = rec.getValue("map2"); assertEquals(true,rec.getValue("map2").equals(myValue)); Value nval = rec.getValue("map.null"); assertEquals(true, rec.getValue("map.null").equals(nval)); Document r1 = Json.newDocument(); Document r2 = Json.newDocument(); Document r3 = Json.newDocument(); r1.setNull("a"); r2.setNull("a"); r3.setNull("b"); assertEquals(true, r1.equals(r2)); assertEquals(false, r1.equals(r3)); }
Example 4
Source File: TestTypeMappedJsonDocumentReader.java From ojai with Apache License 2.0 | 4 votes |
@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()); } } }