You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Matt Grimwade (JIRA)" <ji...@apache.org> on 2015/06/05 11:59:00 UTC
[jira] [Created] (AVRO-1678) JSON encoding of empty record should
be "{}"
Matt Grimwade created AVRO-1678:
-----------------------------------
Summary: JSON encoding of empty record should be "{}"
Key: AVRO-1678
URL: https://issues.apache.org/jira/browse/AVRO-1678
Project: Avro
Issue Type: Bug
Components: java
Affects Versions: 1.7.7
Reporter: Matt Grimwade
An empty record (i.e. an instance of a record schema with no fields) should be encoded in JSON as {{"{}"}}. (This is my interpretation of [the spec|https://avro.apache.org/docs/1.7.7/spec.html#schema_record], which states only that records are encoded as JSON objects.)
However as of Avro 1.7.7, the Java implementation yields {{""}}. This is the case for both generic and specific datum writers. Here is a failing test case (using generic):
{code}
@Test
public void testThis() throws Exception {
Schema emptyRecordSchema = new Schema.Parser().parse("{\"type\":\"record\",\"name\":\"EmptyRecord\",\"fields\":[]}");
Schema arraySchema = Schema.createArray(emptyRecordSchema);
GenericRecord emptyRecord = (GenericRecord) GenericData.get().newRecord(null, emptyRecordSchema);
GenericArray<GenericRecord> emptyArray = new GenericData.Array<>(arraySchema, Collections.<GenericRecord>emptyList());
GenericArray<GenericRecord> arrayContainingEmptyRecord = new GenericData.Array<>(arraySchema, Arrays.asList(emptyRecord));
Assert.assertThat(toJson(emptyArray), Matchers.is("[]")); // OK
Assert.assertThat(toJson(arrayContainingEmptyRecord), Matchers.is("[{}]")); // OK
Assert.assertThat(toJson(emptyRecord), Matchers.is("{}")); // fail: json is ""
}
private static String toJson(GenericContainer record) throws IOException {
Schema schema = record.getSchema();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ParsingEncoder encoder = EncoderFactory.get().jsonEncoder(schema, buffer);
@SuppressWarnings("unchecked") DatumWriter<GenericContainer> writer = GenericData.get().createDatumWriter(schema);
writer.write(record, encoder);
encoder.flush();
return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
}
{code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)