google / google/gson

JsonPrimitive string to number is inconsistent and bad

Open
#453 0 comments 0 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

```
What steps will reproduce the problem?

(0. run the attached test case)

1. use an long attribute with exponent like "dateCreated": 1.020204000000e+12
2. Object deserialization works with new Gson().fromJson(...)
3. When using GsonBuilder + custom JsonDeserializer it fails when deserializing
on the given context (see Test case line 65 or below
context.deserialize(jsonElement, Content.class))

public TestObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();

TestObject testObject = new TestObject();
JsonArray jsonArray = jsonObject.getAsJsonArray("content");
for (JsonElement jsonElement : jsonArray) {
Content content = context.deserialize(jsonElement, Content.class);
testObject.content.add(content);
}
return testObject;
}

What is the expected output? What do you see instead?
- expected: successfully deserialize the value to 1020204000000
- actual: com.google.gson.JsonSyntaxException: java.lang.NumberFormatException:
For input string: "1.0202040"

What version of the product are you using? On what operating system?
- >= 2.0 (1.6. works)

Please provide any additional information below.
- see attached JUnit test case
```

Original issue reported on code.google.com by `jenshadl...@googlemail.com` on 22 Jun 2012 at 10:11

Attachments:
- [GsonUnserializeTest.java](https://storage.googleapis.com/google-code-attachments/google-gson/issue-453/comment-0/GsonUnserializeTest.java)

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.