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

JsonWriter don't work correctly with float #1127

Closed
votoanthuan opened this issue Jul 29, 2017 · 3 comments · Fixed by #2130
Closed

JsonWriter don't work correctly with float #1127

votoanthuan opened this issue Jul 29, 2017 · 3 comments · Fixed by #2130

Comments

@votoanthuan
Copy link

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.

@Harlber
Copy link

Harlber commented Aug 29, 2017

see ObjectTypeAdapter

@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:

@votoanthuan
Copy link
Author

votoanthuan commented Aug 30, 2017

@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:
float x = 3.723379;
JsonWriter writer = ...
writer.value(Float.valueOf(x)); //The simple way to avoid this bug but it is not an effective way

And we should not convert number to string in JsonWriter, it has different results

@Harlber
Copy link

Harlber commented Aug 30, 2017

Similar issue see #1084

Capstan added a commit to Capstan/gson that referenced this issue Jun 8, 2022
This avoids floats being treated as doubles and having an unwarranted level of precision.

Fixes google#1127.
Capstan added a commit to Capstan/gson that referenced this issue Jun 8, 2022
This avoids floats being treated as doubles and having an unwarranted level of precision.

Fixes google#1127.
eamonnmcmanus pushed a commit that referenced this issue Jun 8, 2022
This avoids floats being treated as doubles and having an unwarranted level of precision.

Fixes #1127.
Capstan added a commit to Capstan/gson that referenced this issue Jun 10, 2022
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.
Capstan added a commit to Capstan/gson that referenced this issue Jun 11, 2022
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.
eamonnmcmanus pushed a commit that referenced this issue Jun 21, 2022
Follow-up to comments on #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 #1127.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants