avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doug Cutting <cutt...@apache.org>
Subject Re: Defining optional records? (The usual way with unions doesn't work)
Date Wed, 12 Sep 2012 16:57:18 GMT
On Wed, Sep 12, 2012 at 4:52 AM, Tayssir John Gabbour <tjg@pentaside.org> wrote:
> SCHEMA:
> { "type": "record",
>   "name": "User",
>   "fields" : [
>     {"name": "username", "type": "string"},
>     {"name": "x", "type": ["null",
>                            {"type": "record",
>                             "name": "Test",
>                             "fields" : [
>                               {"name": "a", "type": "int"}
>                             ]}
>                           ]}
>   ]}
>
> DATA:
> {"username": "john", "x": {"a": 1}}
> {"username": "ryan", "x": {"a": 1}}

Non-null union values in Json need to be tagged with the intended branch.

http://avro.apache.org/docs/current/spec.html#json_encoding

So I think this needs to instead be something like:

{"username": "john", "x": {"Test": {"a": 1}}}

If you're generating this Json from Avro then you should use
JsonEncoder or the "tojson" command to do so, not the record's
.toString() method which doesn't tag unions correctly.  (Perhaps we
should fix the .toString() method to also tag unions, but that would
be an incompatible change.)

This has been asked before.  Perhaps we should add it to the FAQ?

Doug

Mime
View raw message