[
https://issues.apache.org/jira/browse/AVRO-1582?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17444771#comment-17444771
]
Ryan commented on AVRO-1582:
----------------------------
A workaround is to use the Jackson JSON ObjectMapper API instead. Replace:
{code:java}
MyCompiledEntity entity = new MyCompiledEntity.newBuilder().build();
SpecificDatumWriter<MyCompiledEntity> writer = new
SpecificDatumWriter<>(entity.getSchema());
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonEncoder encoder = EncoderFactory.get().jsonEncoder(entity.getSchema(), out);
writer.write(entity, encoder);
encoder.flush();
String json = out.toString(Charset.forName("UTF-8"));{code}
With:
{code:java}
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(MyCompiledEntity.class, MyEntityMixin.class);
String json = mapper.writeValueAsString(value);
{code}
where the mixin defines the actual fields you want (alternatively you could try
to exclude all the AVRO specific fields):
{code:java}
@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
interface MyEntityMixin {
@JsonProperty
String getField1();
@JsonProperty
String getField2();
}
{code}
> Json serialization of nullable fields and fields with default values
> improvement.
> ---------------------------------------------------------------------------------
>
> Key: AVRO-1582
> URL: https://issues.apache.org/jira/browse/AVRO-1582
> Project: Apache Avro
> Issue Type: Improvement
> Components: java
> Affects Versions: 1.8.0
> Reporter: Zoltan Farkas
> Priority: Major
> Attachments: AVRO-1582-PATCH
>
>
> Currently serializing a nullable field of type union like:
> "type" : ["null","some type"]
> when serialized as JSON results in:
> "field":{"some type":"value"}
> when it could be:
> "field":"value"
> Also fields that equal the the default value can be omitted from the
> serialized data. This is possible because the reader will have the writer's
> schema and can infer the field values. This reduces the size of the json
> messages.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)