You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by amrishsintu <am...@gmail.com> on 2018/04/25 13:35:01 UTC

JSON format change while converting from Avro Pojo

Hi,

I am trying to convert a POJO which is set with data to JSON in java.
While converting I get data as 
"lastName": {
                    "string": "XYZ"
                }
This happens when we use the following snippet of the schema.
"name": "lastName",
"type": ["string", "null"],

But it works fine when we use below schema
"name": "lastName",
"type": ["string", "null"],
And produces result as :- lastName":  "XYZ"

But we need to make the schema such that it should work properly even if
lastName comes as null.

Please help us so that we can resolve the union issue of [“string”:”null”]

Thanks,
Amrish




--
Sent from: http://apache-avro.679487.n3.nabble.com/Avro-Users-f679479.html

Re: JSON format change while converting from Avro Pojo

Posted by amrishsintu <am...@gmail.com>.
It worked. Now I am getting desired Json without any type annotation.

Thanks  



--
Sent from: http://apache-avro.679487.n3.nabble.com/Avro-Users-f679479.html

Re: JSON format change while converting from Avro Pojo

Posted by Doug Cutting <cu...@gmail.com>.
If you don't want Avro's json encoding then don't use an Avro json
encoder.  Just use toString() directly on your POJO.  If it's a generic,
specific or reflect datum, toString() will generate valid json without any
type annotation around values of unions.

So replace all of the above code with:

String avroGeneratedJson = test.toString();

Doug

On Wed, Apr 25, 2018 at 12:34 PM, amrishsintu <am...@gmail.com> wrote:

> I use below java code to convert to json.
>
>  Schema schema = ReflectData.get().getSchema(Test.class);
>  DatumWriter<PartyResult> userDatumWriter = new
> GenericDatumWriter<PartyResult>(schema);
>
>  ByteArrayOutputStream stream = new ByteArrayOutputStream();
> JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, stream);
>  userDatumWriter.write(test, encoder); // test is the reference of Test
> class pojo
>  encoder.flush();
> String  avroGeneratedJson = stream.toString();
>
> RESULT : "familyName": {
>                     "string": "Seils"
>                 }
> I am trying for - {"familyName":  "Seils"}
>
> Help me to understand how can I use Json Encoding here.
>
>
>
> --
> Sent from: http://apache-avro.679487.n3.nabble.com/Avro-Users-f679479.html
>

Re: JSON format change while converting from Avro Pojo

Posted by amrishsintu <am...@gmail.com>.
I use below java code to convert to json.

 Schema schema = ReflectData.get().getSchema(Test.class);
 DatumWriter<PartyResult> userDatumWriter = new
GenericDatumWriter<PartyResult>(schema);

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, stream);
 userDatumWriter.write(test, encoder); // test is the reference of Test
class pojo
 encoder.flush();
String  avroGeneratedJson = stream.toString();

RESULT : "familyName": {
                    "string": "Seils"
                }
I am trying for - {"familyName":  "Seils"}

Help me to understand how can I use Json Encoding here.



--
Sent from: http://apache-avro.679487.n3.nabble.com/Avro-Users-f679479.html

Re: JSON format change while converting from Avro Pojo

Posted by Marcelo Valle <mv...@gmail.com>.
Is this supported by the python avro library?

It doesn't seem to be.

On 25 April 2018 at 17:02, Doug Cutting <cu...@gmail.com> wrote:

> To preserve type information, Avro's json encoding tags union values with
> their type.
>
> https://avro.apache.org/docs/current/spec.html#json_encoding
>
> If you wish to avoid this tagging, then you may use toString() on Avro
> data.  This will generate valid Json, although some type information may be
> lost in the case of unions.
>
> For example, a union of a string and an enum cannot always be losslessly
> converted to and from Json without tagging.  Nor can unions of ints and
> longs or unions of records and maps.  Avro's type model is more complex
> than Json's.  However if your schemas do not contain any unions that are
> ambiguous in Json then you can safely use toString().
>
> Doug
>
> On Wed, Apr 25, 2018 at 6:35 AM, amrishsintu <am...@gmail.com>
> wrote:
>
>> Hi,
>>
>> I am trying to convert a POJO which is set with data to JSON in java.
>> While converting I get data as
>> "lastName": {
>>                     "string": "XYZ"
>>                 }
>> This happens when we use the following snippet of the schema.
>> "name": "lastName",
>> "type": ["string", "null"],
>>
>> But it works fine when we use below schema
>> "name": "lastName",
>> "type": ["string", "null"],
>> And produces result as :- lastName":  "XYZ"
>>
>> But we need to make the schema such that it should work properly even if
>> lastName comes as null.
>>
>> Please help us so that we can resolve the union issue of [“string”:”null”]
>>
>> Thanks,
>> Amrish
>>
>>
>>
>>
>> --
>> Sent from: http://apache-avro.679487.n3.nabble.com/Avro-Users-f679479.h
>> tml
>>
>
>


-- 
Marcelo Valle
http://mvalle.com - @mvallebr

Re: JSON format change while converting from Avro Pojo

Posted by Doug Cutting <cu...@gmail.com>.
To preserve type information, Avro's json encoding tags union values with
their type.

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

If you wish to avoid this tagging, then you may use toString() on Avro
data.  This will generate valid Json, although some type information may be
lost in the case of unions.

For example, a union of a string and an enum cannot always be losslessly
converted to and from Json without tagging.  Nor can unions of ints and
longs or unions of records and maps.  Avro's type model is more complex
than Json's.  However if your schemas do not contain any unions that are
ambiguous in Json then you can safely use toString().

Doug

On Wed, Apr 25, 2018 at 6:35 AM, amrishsintu <am...@gmail.com> wrote:

> Hi,
>
> I am trying to convert a POJO which is set with data to JSON in java.
> While converting I get data as
> "lastName": {
>                     "string": "XYZ"
>                 }
> This happens when we use the following snippet of the schema.
> "name": "lastName",
> "type": ["string", "null"],
>
> But it works fine when we use below schema
> "name": "lastName",
> "type": ["string", "null"],
> And produces result as :- lastName":  "XYZ"
>
> But we need to make the schema such that it should work properly even if
> lastName comes as null.
>
> Please help us so that we can resolve the union issue of [“string”:”null”]
>
> Thanks,
> Amrish
>
>
>
>
> --
> Sent from: http://apache-avro.679487.n3.nabble.com/Avro-Users-f679479.html
>