-
Notifications
You must be signed in to change notification settings - Fork 1.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
Numbers deserialization using gson #771
Comments
Just a reminder for implementation: The default Gson instance should be configured with a custom strategy that combines the logic in the code block at the end of #616 (comment), i.e. JsonNumber number = parser.next();
String sval = number.stringValue();
if (sval.contains(".")) {
return toDecimal(sval); //returns a Float, Double or a BigDecimal depending on value size
} else {
return toInteger(sval); //returns an Integer, Long or BigInteger depending on value size
} and maybe a variant of all of the logic in #616 (comment). Ideally, the smallest That is, a |
They also have this option that at first sight should do the job. ToNumberPolicy.LAZILY_PARSED_NUMBER that implements the default behavior of the Number type adapter. Basically you get a LazilyParsedNumber and return the needed number type.
Regards. |
I could be missing something, but I don't think that The code samples shown in the PR comment google/gson#1290 (comment) show that the values returned are instances of Gson gson = new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER)
.create();
List<Object> actual = gson.fromJson("[null, 10, 10.0]", new TypeToken<List<Object>>() {}.getType());
List<LazilyParsedNumber> expected = Arrays.asList(null, new LazilyParsedNumber("10"), new LazilyParsedNumber("10.0"));
assertEquals(expected, actual); I assume that the large majority of app developers do not want to deal with Gson-specific data types in their JSON data model. And at least from the JJWT perspective, the JSON parser should be totally transparent. If I switch from Based on that, I think JJWT's default Gson instance should enable a custom We could even provide an enum that indicates the 'level' of precision desired - everything as a Just thinking out loud. |
Starting from this issue #616
I can say that :
this bug was closed in Gson library with this PR google/gson#1290 merged in version 2.9.0 of GSON library. Right now the right way to have numbers parsed correctly is to configure the gson() in this way.
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
I suggest to make a new release that relay on latest gson release (right now is 2.10.1). And to close the bug adding this line to the gson builder.
Thanks.
Andrea.
The text was updated successfully, but these errors were encountered: