/** * Decodes a JSON encoded record. * * @param json JSON tree to decode, encoded as a string. * @param schema Avro schema of the value to decode. * @return the decoded value. * @throws IOException on error. */ public static Object fromJsonString(String json, Schema schema) throws IOException { final ObjectMapper mapper = new ObjectMapper(); final JsonParser parser = new JsonFactory().createParser(json) .enable(JsonParser.Feature.ALLOW_COMMENTS) .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES) .enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES); final JsonNode root = mapper.readTree(parser); return fromJsonNode(root, schema); }
while (it.hasNext()) { final JsonNode element = it.next(); list.add(fromJsonNode(element, schema.getElementType())); while (it.hasNext()) { final Map.Entry<String, JsonNode> entry = it.next(); map.put(entry.getKey(), fromJsonNode(entry.getValue(), schema.getValueType())); final SpecificRecord record = newSpecificRecord(schema.getFullName()); for (Schema.Field field : schema.getFields()) { final String fieldName = field.name(); final JsonNode fieldElement = json.get(fieldName); if (fieldElement != null) { final Object fieldValue = fromJsonNode(fieldElement, field.schema()); record.put(field.pos(), fieldValue); } else if (field.defaultValue() != null) { record.put(field.pos(), fromJsonString(field.defaultValue().toString(), field.schema())); } else { throw new IOException(String.format( return fromUnionJsonNode(json, schema); return enumValue(schema.getFullName(), enumValStr);
final Schema optionalType = AvroUtils.getOptionalType(schema); if (optionalType != null) { return json.isNull() ? null : fromJsonNode(json, optionalType); return fromJsonNode(actualNode, type); return fromJsonNode(json, type); } catch (IOException ioe) {