question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
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)

https://github.com/google/gson/blob/b8f616c939c652b8540c95fa2b377b8c628ef3ff/gson/src/main/java/com/google/gson/DefaultDateTypeAdapter.java#L95

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()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:8

github_iconTop GitHub Comments

4reactions
bmartycommented, Oct 29, 2018

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

1reaction
mohamedmelsayedcommented, Oct 24, 2018

TypeAdapter<Date> safeDateTypeAdapter = dateTypeAdapter.nullSafe();

it worked for me @bmarty thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you specify default values for Jackson deserialization
You can define a custom getter property where setting your default value in case of null . public Integer getAge() { if (age...
Read more >
Gson (Gson 2.8.0 API) - Javadoc.io
Moreover, if a field is not null, but its generated JSON is empty, the field is kept. You can configure Gson to serialize...
Read more >
Custom Serializers in Apache Kafka - Baeldung
In this tutorial, we'll see how to implement Kafka default format converters and custom serializers for specific use cases.
Read more >
Json.Net Error Deserializing DataTable from Json - MSDN
Can't figure out why this json text, which was serialized by Json.Net, can't simply be ... It does not deserialize Null values properly....
Read more >
JSON parse error: Cannot deserialize value of type `java.time ...
Your browser can't play this video. ... LocalDateTime` This is the JsonFormat pattern that you will most likely need: "yyyy-MM-dd'T'HH:mm:ss ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found