@Override public Row deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { // Parse and convert the root object to Row as if it's a nested field with name 'root' return (Row) extractJsonNodeValue( FieldValue.of("root", FieldType.row(schema), jsonParser.readValueAsTree())); }
private static Object jsonArrayToList(FieldValue arrayFieldValue) { if (!arrayFieldValue.isJsonArray()) { throw new UnsupportedRowJsonException( "Expected JSON array for field '" + arrayFieldValue.name() + "'. " + "Instead got " + arrayFieldValue.jsonNodeType().name()); } return arrayFieldValue .jsonArrayElements() .map( jsonArrayElement -> extractJsonNodeValue( FieldValue.of( arrayFieldValue.name() + "[]", arrayFieldValue.arrayElementType(), jsonArrayElement))) .collect(toList()); }
private static Row jsonObjectToRow(FieldValue rowFieldValue) { if (!rowFieldValue.isJsonObject()) { throw new UnsupportedRowJsonException( "Expected JSON object for field '" + rowFieldValue.name() + "'. " + "Unable to convert '" + rowFieldValue.jsonValue().asText() + "'" + " to Beam Row, it is not a JSON object. Currently only JSON objects " + "can be parsed to Beam Rows"); } return rowFieldValue .rowSchema() .getFields() .stream() .map( schemaField -> extractJsonNodeValue( FieldValue.of( schemaField.getName(), schemaField.getType(), rowFieldValue.jsonFieldValue(schemaField.getName())))) .collect(toRow(rowFieldValue.rowSchema())); }