JSON encoder for AVRO that skips default values. Based on org.apache.avro.io.JsonEncoder from AVRO 1.8.2 and org.apache.avro.io.ExtendedJsonEncoder by zolyfarkas.
This encoder is meant to be used with avro-json-decoder as it is capable of decoding Avro JSON with missing values.
Given this schema (in AVRO IDL)
record User {
string username;
union {null, string} name = null;
}
a user without a name will be encoded as
{"username":"user1","name":null}
Using this encoder, the output will instead be
{"username":"user1"}
The encoder allows skipping specifying nulls. Use with avro-json-decoder.
Replace
JsonEncoder encoder = EncoderFactory.get().jsonEncoder(SCHEMA, OUTPUT_STREAM);
with
ExtendedJsonEncoder encoder = new ExtendedJsonEncoder(SCHEMA, OUTPUT_STREAM);
and pass it to the new ExtendedGenericDatumWriter:
DatumWriter<T> writer = new ExtendedGenericDatumWriter<>(SCHEMA_OR_CLASS);
writer.write(DATA, encoder);