You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Jerry Smith <je...@gmail.com> on 2017/11/01 21:47:09 UTC

Gson Json unmarshal List

Using the Camel 2.19.3 Java DSL how do I go about unmarshalling a list of
pojos?

Currently I've tried:

.unmarshal().json(JsonLibrary.Gson, new ArrayList<MyPojo>().getClass())

but the generic gets erased so Gson has no idea what it's a list of, so
then I tried this for grins:

.unmarshal().json(JsonLibrary.Gson, MyPojo.class)

hoping the library would be pick up it was an array of this type, but it
doesn't.

Re: Gson Json unmarshal List

Posted by Zoran Regvart <zo...@regvart.com>.
Hi Jerry,

yeah my mistake, typed from memory, that should have been
`TypeToken<List<MyPojo>>(){}.getType()`. And that won't work either we
expect java.lang.Class parameter there not `java.lang.reflect.Type`,
perhaps we should add support for that also. I created an issue to
bring support for that CAMEL-11990[1].

zoran

[1] https://issues.apache.org/jira/browse/CAMEL-11990

On Thu, Nov 2, 2017 at 4:14 PM, Jerry Smith <je...@gmail.com> wrote:
> Zoran, using the TypeToken I don't get any errors.  However, after the
> unmarshal call the body of my exchange is empty.  I checked the json going
> into the unmarshal call and there is data there.  Any idea what might be
> happening?




-- 
Zoran Regvart

Re: Gson Json unmarshal List

Posted by Jerry Smith <je...@gmail.com>.
Zoran, using the TypeToken I don't get any errors.  However, after the
unmarshal call the body of my exchange is empty.  I checked the json going
into the unmarshal call and there is data there.  Any idea what might be
happening?

Claus, I found ListJacksonDataFormat and that seems to unmarshal it
correctly.

On Thu, Nov 2, 2017 at 7:20 AM Claus Ibsen <cl...@gmail.com> wrote:

> There is also a useList option on the JsonDataFormat you can configure and
> use.
>
>
>
> On Thu, Nov 2, 2017 at 1:13 PM, Zoran Regvart <zo...@regvart.com> wrote:
> > Hi Jerry,
> > I think you need to use the type token pattern, that is the
> > TypeToken[1] subclass, something like:
> >
> >     .unmarshal().json(JsonLibrary.Gson, new
> > TypeToken<List<MyPojo>>(){}.getClass())
> >
> > zoran
> >
> > [1]
> https://google.github.io/gson/apidocs/com/google/gson/reflect/TypeToken.html
> >
> > On Wed, Nov 1, 2017 at 10:47 PM, Jerry Smith <je...@gmail.com>
> wrote:
> >> Using the Camel 2.19.3 Java DSL how do I go about unmarshalling a list
> of
> >> pojos?
> >>
> >> Currently I've tried:
> >>
> >> .unmarshal().json(JsonLibrary.Gson, new ArrayList<MyPojo>().getClass())
> >>
> >> but the generic gets erased so Gson has no idea what it's a list of, so
> >> then I tried this for grins:
> >>
> >> .unmarshal().json(JsonLibrary.Gson, MyPojo.class)
> >>
> >> hoping the library would be pick up it was an array of this type, but it
> >> doesn't.
> >
> >
> >
> > --
> > Zoran Regvart
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>

Re: Gson Json unmarshal List

Posted by Claus Ibsen <cl...@gmail.com>.
There is also a useList option on the JsonDataFormat you can configure and use.



On Thu, Nov 2, 2017 at 1:13 PM, Zoran Regvart <zo...@regvart.com> wrote:
> Hi Jerry,
> I think you need to use the type token pattern, that is the
> TypeToken[1] subclass, something like:
>
>     .unmarshal().json(JsonLibrary.Gson, new
> TypeToken<List<MyPojo>>(){}.getClass())
>
> zoran
>
> [1] https://google.github.io/gson/apidocs/com/google/gson/reflect/TypeToken.html
>
> On Wed, Nov 1, 2017 at 10:47 PM, Jerry Smith <je...@gmail.com> wrote:
>> Using the Camel 2.19.3 Java DSL how do I go about unmarshalling a list of
>> pojos?
>>
>> Currently I've tried:
>>
>> .unmarshal().json(JsonLibrary.Gson, new ArrayList<MyPojo>().getClass())
>>
>> but the generic gets erased so Gson has no idea what it's a list of, so
>> then I tried this for grins:
>>
>> .unmarshal().json(JsonLibrary.Gson, MyPojo.class)
>>
>> hoping the library would be pick up it was an array of this type, but it
>> doesn't.
>
>
>
> --
> Zoran Regvart



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: Gson Json unmarshal List

Posted by Zoran Regvart <zo...@regvart.com>.
Hi Jerry,
I think you need to use the type token pattern, that is the
TypeToken[1] subclass, something like:

    .unmarshal().json(JsonLibrary.Gson, new
TypeToken<List<MyPojo>>(){}.getClass())

zoran

[1] https://google.github.io/gson/apidocs/com/google/gson/reflect/TypeToken.html

On Wed, Nov 1, 2017 at 10:47 PM, Jerry Smith <je...@gmail.com> wrote:
> Using the Camel 2.19.3 Java DSL how do I go about unmarshalling a list of
> pojos?
>
> Currently I've tried:
>
> .unmarshal().json(JsonLibrary.Gson, new ArrayList<MyPojo>().getClass())
>
> but the generic gets erased so Gson has no idea what it's a list of, so
> then I tried this for grins:
>
> .unmarshal().json(JsonLibrary.Gson, MyPojo.class)
>
> hoping the library would be pick up it was an array of this type, but it
> doesn't.



-- 
Zoran Regvart