You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Jeremy Ross <je...@gmail.com> on 2020/06/02 16:42:15 UTC

GsonDataFormat "useList" option?

Hi All,

Is it possible to unmarshal a List of some type with the GsonDataFormat?
JacksonDataFormat has this options, and lots others, but not seeing any
such option for gson.

Thanks!

Re: GsonDataFormat "useList" option?

Posted by Jeremy Ross <je...@gmail.com>.
Solved the date parsing issue. Apparently gson can't handle OffsetDateTime
out of the box. Here's my solution:

Gson gson = new GsonBuilder().registerTypeAdapter(OffsetDateTime.class, new
JsonDeserializer() {

    @Override
    public Object deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws
JsonParseException {
        try {
            if (type == OffsetDateTime.class) {
                return OffsetDateTime.parse(jsonElement.getAsString(),
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
            }
        }
        catch (DateTimeParseException e) {
            throw new JsonParseException(e);
        }
        throw new IllegalArgumentException("unknown type: " + type);
    }
}).create();
gsonDataFormat = new GsonDataFormat(gson, new
TypeToken<List<MatchResult>>(){}.getType());

On Tue, Jun 2, 2020 at 12:12 PM Jeremy Ross <je...@gmail.com> wrote:

> Hey Alex,
>
> Thank you. It looks like this may work. I'm getting a separate gson
> exception, but I believe it's because gson is expecting Birthdate to be an
> object, not a string: Expected BEGIN_OBJECT but was STRING at line 1 column
> 271 path $[0].Person.Birthdate. Separate issue I'll look into.
>
>
> On Tue, Jun 2, 2020 at 11:51 AM Alex Dettinger <al...@gmail.com>
> wrote:
>
>> Hi Jeremy,
>>
>>   It reminds me something, could you please try something like below:
>>
>> https://github.com/apache/camel-quarkus/blob/master/integration-tests/dataformats-json/src/main/java/org/apache/camel/quarkus/component/dataformats/json/JsonDataformatsRoute.java#L58..L61
>>
>> hth,
>> Alex
>>
>> On Tue, Jun 2, 2020 at 6:42 PM Jeremy Ross <je...@gmail.com>
>> wrote:
>>
>> > Hi All,
>> >
>> > Is it possible to unmarshal a List of some type with the GsonDataFormat?
>> > JacksonDataFormat has this options, and lots others, but not seeing any
>> > such option for gson.
>> >
>> > Thanks!
>> >
>>
>

Re: GsonDataFormat "useList" option?

Posted by Jeremy Ross <je...@gmail.com>.
Hey Alex,

Thank you. It looks like this may work. I'm getting a separate gson
exception, but I believe it's because gson is expecting Birthdate to be an
object, not a string: Expected BEGIN_OBJECT but was STRING at line 1 column
271 path $[0].Person.Birthdate. Separate issue I'll look into.


On Tue, Jun 2, 2020 at 11:51 AM Alex Dettinger <al...@gmail.com>
wrote:

> Hi Jeremy,
>
>   It reminds me something, could you please try something like below:
>
> https://github.com/apache/camel-quarkus/blob/master/integration-tests/dataformats-json/src/main/java/org/apache/camel/quarkus/component/dataformats/json/JsonDataformatsRoute.java#L58..L61
>
> hth,
> Alex
>
> On Tue, Jun 2, 2020 at 6:42 PM Jeremy Ross <je...@gmail.com>
> wrote:
>
> > Hi All,
> >
> > Is it possible to unmarshal a List of some type with the GsonDataFormat?
> > JacksonDataFormat has this options, and lots others, but not seeing any
> > such option for gson.
> >
> > Thanks!
> >
>

Re: GsonDataFormat "useList" option?

Posted by Alex Dettinger <al...@gmail.com>.
Hi Jeremy,

  It reminds me something, could you please try something like below:
https://github.com/apache/camel-quarkus/blob/master/integration-tests/dataformats-json/src/main/java/org/apache/camel/quarkus/component/dataformats/json/JsonDataformatsRoute.java#L58..L61

hth,
Alex

On Tue, Jun 2, 2020 at 6:42 PM Jeremy Ross <je...@gmail.com> wrote:

> Hi All,
>
> Is it possible to unmarshal a List of some type with the GsonDataFormat?
> JacksonDataFormat has this options, and lots others, but not seeing any
> such option for gson.
>
> Thanks!
>