-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
GSON Serialization/Deserialization of Java 8 Date API #1059
Comments
This is due to some historical baggage where Gson will happily serialize any object inside the platform package ( The correct solution here would be to register your own type adapter for the A quick search yields https://github.com/gkopff/gson-javatime-serialisers which you may just be able to drop in, but otherwise building a |
a) still it would be great to have an "official" gson plugin/package with Java 8 types support |
Dates come in from JSON as string values, right? I keep seeing examples of registering adapters for specific Java classes like ZonedDateTime etc, but how would Gson know that you want to convert a particular incoming string value into a ZonedDateTime specifically? |
@jugimaster Because you tell Gson the expected class when deserializing: SomeClass obj = gson.fromJson(jsonString, SomeClass.class); So Gson would either know it from there, or if your |
But I'm not using JavaBeans/"domain model classes" - I just have Maps, and I was calling Gson from Clojure too. Can strings be converted into Dates when deserializing into a Map? |
@jugimaster I'm not familiar with clojure unfortunately, but as far as I remember in Java, if you're using a However, you seem to be using a "map of maps" kind of structure without generic type information (even maybe using mixed types as values in your maps), and in this case I don't see how Gson could possibly infer a more accurate type than the JSON type itself, which would be String in your case. Again, I'm speaking for Java, but I guess if you want statically typed data, you should define your classes. If you don't, then you should expect to handle this kind of conversions yourself at runtime. |
Right :) But with Jackson, I was able to configure my own class to handle incoming Strings, and convert the ones that represent dates into Date instances. I couldn't figure out how to do that with Gson. I may be completely missing something here, of course. |
Dear maintainers, Is it possible to implement it? We have Java 21 released, and |
Hi @remal I guess it's not possible to implement that now because Gson still uses Java 7 source compatibility. So it just can't support Java 8 types. So the only approach is to use custom type adapters or migrate to another serialization library. |
Well, a couple of comments. First, we could always use reflection to allow the The existing support for serializing and deserializing A further wrinkle is that the default serialization doesn't work with recent JDK versions unless you use the appropriate |
Should this be done then as new Maybe it would be good then to also add opt-in support other Java 8 types, such as
That sounds like a good idea. Detecting if this exception occurs for one of the types which will have opt-in support should definitely be possible.
This could risk though that new users (who haven't used |
Any updates? This is blocking us from updating to Java17 :( |
When serializing / deserializing an object with the Date and Time classes of the new java.time API, the results are diferent from expected.
For example, here's a java.util.Date serialized with gson (using version 2.8.0):
"created": "2017-04-07T18:07:00",
and here's a java.time.LocalDate:
"expiration": {
"year": 2017,
"month": 4,
"day": 7
}
the expected result for javatime api was a ISO-8601.
The text was updated successfully, but these errors were encountered: