Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.8.1 can't serialize and deserialize date null (2.8.0 works fine) #1096

Closed
xanscale opened this issue Jun 5, 2017 · 8 comments
Closed

2.8.1 can't serialize and deserialize date null (2.8.0 works fine) #1096

xanscale opened this issue Jun 5, 2017 · 8 comments

Comments

@xanscale
Copy link

xanscale commented Jun 5, 2017

W/System.err: com.google.gson.JsonParseException: The date should be a string value
W/System.err:     at com.google.gson.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:96)
W/System.err:     at com.google.gson.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:40)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
W/System.err:     at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)

if (in.peek() != JsonToken.STRING) {

this will trows JsonParseException

    if (in.peek() != JsonToken.STRING) {
      throw new JsonParseException("The date should be a string value");
    }

this is my conf
new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create()

@inder123
Copy link
Collaborator

inder123 commented Jun 6, 2017

Can you create a PR with a failing test? Thanks.

@tylerOrMe
Copy link

tylerOrMe commented Jun 7, 2017

Having similar issue in serialization.

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create()
gson.toJson(myObjectWhoContainsNullDates)

throws NullPointerException from Gson 2.8.1
2.8.0 works fine.

if you remove setDateFormat serialization works

Gson gson = new GsonBuilder().create()
gson.toJson(myObjectWhoContainsNullDates)

@SergeyTrukhachev
Copy link

Faced the same issue with 2.8.1.
2.8.0 works well.

@bmarty
Copy link

bmarty commented Aug 25, 2017

Hi,

This is my workaround for this bug, waiting for 2.8.2:

private Gson getGson() {
    // Trick to get the DefaultDateTypeAdatpter instance
    // Create a first instance a Gson
    Gson gson = new GsonBuilder()
            .setDateFormat(DATE_FORMAT_COMPLETE)
            .create();

    // Get the date adapter
    TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);

    // Ensure the DateTypeAdapter is null safe
    TypeAdapter<Date> safeDateTypeAdapter = dateTypeAdapter.nullSafe();

    // Build the definitive safe Gson instance
    return new GsonBuilder()
            .registerTypeAdapter(Date.class, safeDateTypeAdapter)
            .create();
}

Hope it will help

BR

@rafa-js
Copy link

rafa-js commented Sep 5, 2017

Nice @bmarty .
I get the impression the last .setDateFormat(DATE_FORMAT_COMPLETE) is not needed. You actually defined within the safeDateTypeAdapter all the details to parse the date.

@bmarty
Copy link

bmarty commented Sep 5, 2017

@rafa0809 yes, indeed!

@nachodd
Copy link

nachodd commented Sep 12, 2017

Facing the same .
Edit: @bmarty solution worked for me. And also the 2.8.0 worked fine.

inder123 pushed a commit that referenced this issue Sep 18, 2017
…ntract (#1100)

* Fixed DefaultDateTypeAdapter nullability issue and JSON primitives contract

Regression in:

* b8f616c - Migrate DefaultDateTypeAdapter to streaming adapter (#1070)

Bug reports:

* #1096 - 2.8.1 can't serialize and deserialize date null (2.8.0 works fine)
* #1098 - Gson 2.8.1 DefaultDateTypeAdapter is not null safe.

* Fixed DefaultDateTypeAdapter nullability on write
sebasjm pushed a commit to sebasjm/gson that referenced this issue Mar 11, 2018
…ntract (google#1100)

* Fixed DefaultDateTypeAdapter nullability issue and JSON primitives contract

Regression in:

* b8f616c - Migrate DefaultDateTypeAdapter to streaming adapter (google#1070)

Bug reports:

* google#1096 - 2.8.1 can't serialize and deserialize date null (2.8.0 works fine)
* google#1098 - Gson 2.8.1 DefaultDateTypeAdapter is not null safe.

* Fixed DefaultDateTypeAdapter nullability on write
@mohamedmelsayed
Copy link

TypeAdapter safeDateTypeAdapter = dateTypeAdapter.nullSafe();

it worked for me @bmarty
thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants