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

GSON failure toJson in JDK21 #1101

Closed
1 task
funky-eyes opened this issue May 29, 2024 · 6 comments
Closed
1 task

GSON failure toJson in JDK21 #1101

funky-eyes opened this issue May 29, 2024 · 6 comments
Labels
bug Something isn't working jbs:reported Someone from our org has reported it to OpenJDK

Comments

@funky-eyes
Copy link

Please provide a brief summary of the bug

Gson 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

    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

Did you test with the latest update version?

  • Yes

Please provide steps to reproduce where possible

No response

Expected Results

Consistent with jdk17 output

Actual Results

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

What Java Version are you using?

21.0.2

What is your operating system and platform?

mac os arm64

How did you install Java?

tar.gz

Did it work before?

No response

Did you test with other Java versions?

No response

Relevant log output

No response

@funky-eyes funky-eyes added the bug Something isn't working label May 29, 2024
@funky-eyes
Copy link
Author

  public String toJson(Object src, Type typeOfSrc) {
    StringWriter writer = new StringWriter();
    toJson(src, typeOfSrc, writer);
    return writer.toString();
  }

The above is the source code of the toJson method in Gson. I suspect this issue is related to StringBuffer or AbstractStringBuilder.

@karianna
Copy link
Contributor

@funky-eyes you need to report this to the GSon folks in the first instance.

@jerboaa
Copy link

jerboaa commented May 29, 2024

FWIW, this sounds like https://bugs.openjdk.org/browse/JDK-8324308 and friends (caused by the CLDR update in 21).

@funky-eyes
Copy link
Author

funky-eyes commented May 29, 2024

@funky-eyes you need to report this to the GSon folks in the first instance.

Yes, I have already reported it to the Gson community at google/gson#2689 . However, I believe this is caused by changes in the JDK, as the same code produces different results when using the same logic with different JDK versions.

@funky-eyes
Copy link
Author

FWIW, this sounds like https://bugs.openjdk.org/browse/JDK-8324308 and friends (caused by the CLDR update in 21).

thx.

@karianna karianna reopened this May 29, 2024
@karianna karianna added the jbs:reported Someone from our org has reported it to OpenJDK label May 29, 2024
@jerboaa
Copy link

jerboaa commented May 29, 2024

@karianna I don't think there is anything for us to do here. The fix needs to be in gson to support JDK 21+. So closing this issue.

@jerboaa jerboaa closed this as completed May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jbs:reported Someone from our org has reported it to OpenJDK
Projects
None yet
Development

No branches or pull requests

3 participants