@Test public void decodeLongField() throws Exception{ String jsonStr = "{\"timestamp\":1585204833128, \"count\":2, \"value\": 1.1, \"on\":true}"; byte[] jsonStrBytes = jsonStr.getBytes(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jn = objectMapper.readTree(new String(jsonStrBytes, 0, jsonStrBytes.length, UTF_8)); GenericJsonRecord record = new GenericJsonRecord(null, Collections.emptyList(), jn); Object longValue = record.getField("timestamp"); assertTrue(longValue instanceof Long); assertEquals(1585204833128L, longValue); Object intValue = record.getField("count"); assertTrue(intValue instanceof Integer); assertEquals(2, intValue); Object value = record.getField("value"); assertTrue(value instanceof Double); assertEquals(1.1, value); Object boolValue = record.getField("on"); assertTrue((boolean)boolValue); }
@Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(schemaVersion, fields, fn, schemaInfo); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isBigInteger()) { if (fn.canConvertToLong()) { return fn.asLong(); } else { return fn.asText(); } } else if (fn.isNumber()) { return fn.numberValue(); } else if (fn.isBinary()) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else if (isBinaryValue(fieldName)) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else { return fn.asText(); } }
GenericJsonRecord extends VersionedGenericRecord { @Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(schemaVersion, fields, fn, schemaInfo); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isBigInteger()) { if (fn.canConvertToLong()) { return fn.asLong(); } else { return fn.asText(); } } else if (fn.isNumber()) { return fn.numberValue(); } else if (fn.isBinary()) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else if (isBinaryValue(fieldName)) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else { return fn.asText(); } } }
GenericJsonRecord extends VersionedGenericRecord { @Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(schemaVersion, fields, fn, schemaInfo); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isBigInteger()) { if (fn.canConvertToLong()) { return fn.asLong(); } else { return fn.asText(); } } else if (fn.isNumber()) { return fn.numberValue(); } else if (fn.isBinary()) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else if (isBinaryValue(fieldName)) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else { return fn.asText(); } } GenericJsonRecord(byte[] schemaVersion,
                      List<Field> fields,
                      JsonNode jn);   GenericJsonRecord(byte[] schemaVersion,
                      List<Field> fields,
                      JsonNode jn, SchemaInfo schemaInfo); }
GenericJsonRecord extends VersionedGenericRecord { @Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(schemaVersion, fields, fn, schemaInfo); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isBigInteger()) { if (fn.canConvertToLong()) { return fn.asLong(); } else { return fn.asText(); } } else if (fn.isNumber()) { return fn.numberValue(); } else if (fn.isBinary()) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else if (isBinaryValue(fieldName)) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else { return fn.asText(); } } GenericJsonRecord(byte[] schemaVersion,
                      List<Field> fields,
                      JsonNode jn);   GenericJsonRecord(byte[] schemaVersion,
                      List<Field> fields,
                      JsonNode jn, SchemaInfo schemaInfo); JsonNode getJsonNode(); @Override Object getField(String fieldName); }
GenericJsonRecord extends VersionedGenericRecord { @Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(schemaVersion, fields, fn, schemaInfo); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isBigInteger()) { if (fn.canConvertToLong()) { return fn.asLong(); } else { return fn.asText(); } } else if (fn.isNumber()) { return fn.numberValue(); } else if (fn.isBinary()) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else if (isBinaryValue(fieldName)) { try { return fn.binaryValue(); } catch (IOException e) { return fn.asText(); } } else { return fn.asText(); } } GenericJsonRecord(byte[] schemaVersion,
                      List<Field> fields,
                      JsonNode jn);   GenericJsonRecord(byte[] schemaVersion,
                      List<Field> fields,
                      JsonNode jn, SchemaInfo schemaInfo); JsonNode getJsonNode(); @Override Object getField(String fieldName);  }