avro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matt Grimwade (JIRA)" <j...@apache.org>
Subject [jira] [Created] (AVRO-1678) JSON encoding of empty record should be "{}"
Date Fri, 05 Jun 2015 09:59:00 GMT
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)

Mime
View raw message