-
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
JsonWriter don't work correctly with float #1127
Comments
see @Override public Object read(JsonReader in) throws IOException {
JsonToken token = in.peek();
switch (token) {
case BEGIN_ARRAY:
List<Object> list = new ArrayList<Object>();
in.beginArray();
while (in.hasNext()) {
list.add(read(in));
}
in.endArray();
return list;
case BEGIN_OBJECT:
Map<String, Object> map = new LinkedTreeMap<String, Object>();
in.beginObject();
while (in.hasNext()) {
map.put(in.nextName(), read(in));
}
in.endObject();
return map;
case STRING:
return in.nextString();
case NUMBER:
return in.nextDouble();
case BOOLEAN:
return in.nextBoolean();
case NULL:
in.nextNull();
return null;
default:
throw new IllegalStateException();
}
} NUMBER defaults to double Solutions: |
@Harlber this is a bug, and i report for gson to fix it. The simple way to avoid this bug is convert float to Float, like this: And we should not convert number to string in JsonWriter, it has different results |
Similar issue see #1084 |
This avoids floats being treated as doubles and having an unwarranted level of precision. Fixes google#1127.
This avoids floats being treated as doubles and having an unwarranted level of precision. Fixes google#1127.
This avoids floats being treated as doubles and having an unwarranted level of precision. Fixes #1127.
Follow-up to comments on google#2130, which introduced a new override which was not overridden by JsonTreeWriter. Also tweaks the doccomments for both float and double varients of JsonWriter.value. Supplement to the fix for google#1127.
Follow-up to comments on google#2130, which introduced a new override which was not overridden by `JsonTreeWriter`. Also tweaks the doccomments for `float`, `double` and `Number` variants of `JsonWriter.value`. Supplement to the fix for google#1127.
float x = 3.723379;
JsonWriter writer = ...
writer.value(x); //is will call value(double value), so it will wrong
result is 3.723378896713257
Please add new function for float.
Thank you.
The text was updated successfully, but these errors were encountered: