1
votes

I have a union in my avro schema for favorite_number which could be a null or an int. When I json encode the object I get:

{"name": "Alyssa", "favorite_number": {"int": 7}, "favorite_color": null}

I am trying to get rid of the type indicator for the union, in this case the int, so that it becomes:

{"name": "Alyssa", "favorite_number": 7, "favorite_color": "blue"}

avro schema:

{"name": "person", "type": "record",
           "fields": [
              {"name": "name", "type": "string"},
              {"name": "favorite_number", "type": ["null", "int"], "default": null},
              {"name": "favorite_color", "type": "string"}
           ]
   }

Is there a way to do this? Thanks!

1
You should show some code how did you do it to get help - TuyenNTA
Did you get how to resolve this? - Prashanth
did you get this solved? I have the same problem - Nate

1 Answers

3
votes

Editing whole answer :)

It looks like it is not possible do it directly with the Avro API. There is a pull request pending for a while to support this for nullable types

https://issues.apache.org/jira/browse/AVRO-1582

EDITING:

Base on your comments, I found the same issue trying to serialize without data types, I ended up using toString function defined in https://avro.apache.org/docs/1.8.1/api/java/org/apache/avro/generic/GenericData.html#toString(java.lang.Object), The only cases where it does not work properly, is for schemas with circular references.