GsonDataFormat "useList" option?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

GsonDataFormat "useList" option?

Jeremy Ross
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!
Reply | Threaded
Open this post in threaded view
|

Re: GsonDataFormat "useList" option?

Alex Dettinger
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 <[hidden email]> 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!
>
Reply | Threaded
Open this post in threaded view
|

Re: GsonDataFormat "useList" option?

Jeremy Ross
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 <[hidden email]>
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 <[hidden email]>
> 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!
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: GsonDataFormat "useList" option?

Jeremy Ross
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 <[hidden email]> 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 <[hidden email]>
> 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 <[hidden email]>
>> 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!
>> >
>>
>