Skip to content

Celos/avro-json-encoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

avro-json-encoder

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.

why

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.

how

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);

About

JSON encoder for AVRO that skips null values

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages