google / google/gson

JDK21 toJson failure

Open
#2,689 10 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

# Gson version

version 2.10.1
Using JDK 21 to perform fromJson, the Date type contains hidden characters causing the failure of the toJson operation.

In GitHub issues, it seems that I cannot illustrate it, so I will use images to show
![image](https://github.com/google/gson/assets/19943636/d0239890-8921-43ba-aa93-24a156d4dccf)

```
public static void main(String[] args) {
String jdk17="{\"clientId\":\"123\",\"secret\":\"123\",\"creator\":\"trump\",\"gmtCreated\":\"Dec 14, 2023, 11:07:35 AM\",\"gmtModified\":\"Dec 15, 2023, 4:45:51 PM\"}";
OauthClient oc17 = gson.fromJson(jdk17, OauthClient.class);
String jdk21= "{\"clientId\":\"123\",\"secret\":\"123\",\"creator\":\"trump\",\"gmtCreated\":\"Mar 21, 2022, 11:03:07 AM\",\"gmtModified\":\"Mar 21, 2022, 11:03:07 AM\"}";
OauthClient oc21 = gson.fromJson(jdk21, OauthClient.class);
}
```

```
Exception in thread "main" com.google.gson.JsonSyntaxException: Failed parsing 'Mar 21, 2022, 11:03:07 AM' as Date; at path $.gmtCreated
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:90)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.readIntoField(ReflectiveTypeAdapterFactory.java:212)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$FieldReflectionAdapter.readField(ReflectiveTypeAdapterFactory.java:433)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:393)
at com.google.gson.Gson.fromJson(Gson.java:1227)
at com.google.gson.Gson.fromJson(Gson.java:1137)
at com.google.gson.Gson.fromJson(Gson.java:1047)
at com.google.gson.Gson.fromJson(Gson.java:982)
at cn.tongdun.arch.luc.biz.OauthClientBusinessService.main(OauthClientBusinessService.java:78)
Caused by: java.text.ParseException: Failed to parse date ["Mar 21, 2022, 11:03:07 AM"]: Invalid number: Mar
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:279)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:88)
... 10 more
Caused by: java.lang.NumberFormatException: Invalid number: Mar
at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:316)
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:133)
... 11 more
```

# Java / Android version

jdk21

# Used tools

- [ ] Maven; version:
- [ ] Gradle; version:
- [ ] ProGuard (attach the configuration file please); version:
- [ ] ...

# Description

## Expected behavior

## Actual behavior

# Reproduction steps

1.Convert an object containing a date variable to JSON.
2.Copy the output JSON string.
3.When pasted into an editor like IntelliJ IDEA, hidden characters appear with JDK 21, while JDK 17 does not exhibit this behavior for such operations.

# Exception stack trace

```

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.