/** * Encodes an Avro value into a JSON string. * * Fields with default values are omitted. * * @param value Avro value to encode. * @param schema Avro schema of the value. * @return Pretty string representation of the JSON-encoded value. * @throws IOException on error. */ public static String toJsonString(Object value, Schema schema) throws IOException { final JsonNode node = ToJson.toJsonNode(value, schema); final StringWriter stringWriter = new StringWriter(); final JsonGenerator generator = JSON_FACTORY.createJsonGenerator(stringWriter); // We have disabled this because we used unions to represent row key formats // in the table layout. This is a HACK and needs a better solution. // TODO: Find better solution. https://jira.fiji.org/browse/SCHEMA-174 //generator.disable(Feature.QUOTE_FIELD_NAMES); generator.setPrettyPrinter(new DefaultPrettyPrinter()); final ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(generator, node); return stringWriter.toString(); }
return (null == value) ? JSON_NODE_FACTORY.nullNode() : toJsonNode(value, optionalType); for (Schema type : schema.getTypes()) { try { final JsonNode actualNode = toJsonNode(value, type); union.put(type.getFullName(), actualNode); return union;
final Iterable<Object> javaArray = (Iterable<Object>) value; for (Object element : javaArray) { jsonArray.add(toJsonNode(element, schema.getElementType())); final Map<String, Object> javaMap = (Map<String, Object>) value; for (Map.Entry<String, Object> entry : javaMap.entrySet()) { jsonObject.put(entry.getKey(), toJsonNode(entry.getValue(), schema.getValueType())); final JsonNode fieldNode = toJsonNode(fieldValue, field.schema());