[ 
https://issues.apache.org/jira/browse/AVRO-1582?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17761367#comment-17761367
 ] 

Michael Nielson commented on AVRO-1582:
---------------------------------------

The Jackson solution has a few major problems. A mixin to exclude 
getSpecificData and getSchema helps quite a bit but then falls down as soon as 
you don't provide json for fields with defaults, for instance in idl:

{code}
record Blah {
  string id;
  array<string> someBoringArray = [];
}
{code}

It would be nice to deserialize this from the json:

{code}
{ "id": "some-id" }
{code}

You can almost get the example below to work, Jackson will give you a Blah 
instance, but as soon as you try and serialize that entity to Avro you will get 
an exception stating that someBoringArray cannot be null.
{code}
Blah blah = new ObjectMapper().readValue(Blah.class, "{ \"id\": \"some-id\" }");
{code}

To solve for that I update my record.vm template to add the JsonDeserialize 
annotation to my record classes:

{code}
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(builder = 
${this.mangleTypeIdentifier($schema.getName())}.Builder.class)
{code}

And then this annotation to my builders:
{code}
@com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder(buildMethodName = 
"build", withPrefix = "set")
{code}

Since I am mucking with the velocity template anyway I added 
@java.beans.Transient to the getSchema and getSpecificData methods.

I am quite happy with this solution. 

Would there be any interest in a PR with the change? 

I don't see annotations as a part of the public API (but I'll search around for 
a mixin style Jackson integration for builders, because that would be even 
better).


> 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.10#820010)

Reply via email to