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

BigDecimal loses precision on round trip #72

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments
Closed

BigDecimal loses precision on round trip #72

GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

* What steps will reproduce the problem?

Try executing the following:

    BigDecimal x = new BigDecimal("1.00");
    String json = new Gson().toJson(x);
    BigDecimal y = new Gson().fromJson(json, BigDecimal.class);
    assertEquals(x, y);


* What is the expected output? What do you see instead?

x and y should be equal, but they aren't.  The assertion fails with:

    expected: <1.00> but was: <1.0>

Precision is lost: x.precision() is 3, but y.precision() is 2.  This
seems to be happening because the JsonParser.JsonNumber() method is
trying to optimize the number representation -- see the clause beginning
with "if (exppart != null)".  Because there is no exponent, it chooses
a Double, which loses the information on how many digits of precision
are present.

This problem doesn't happen for numbers with exponents.  Parsing "1.00"
yields a BigDecimal with precision 2, but parsing "1.00E+5" does produce
a BigDecimal with precision 3.

Since BigDecimal is carefully designed to preserve information in
its string representation (see the Javadoc for BigDecimal.toString()),
and its equals() method considers "1.0" and "1.00" to be unequal,
it would be nice to carry this through.  I believe this would just be
a matter of always using BigDecimal as the representation in the parser,
regardless of whether "exppart" or "fracpart" are present, and converting
to another number type only when you know what kind of number is being
requested.


* What version of the product are you using? On what operating system?

I'm using Gson 1.2.3 on Linux.

Original issue reported on code.google.com by p...@zesty.ca on 18 Nov 2008 at 12:51

@GoogleCodeExporter
Copy link
Author

Fix submitted is r306.

Original comment by joel.leitch@gmail.com on 18 Nov 2008 at 8:26

  • Changed state: Fixed

@barrychapman
Copy link

Was there any further movement on this?

@Marcono1234
Copy link
Collaborator

@barrychapman, are you experiencing any specific issue with this? For me the example code provided in the issue description works correctly and returns that x equals y, as desired.

Please test with the latest Gson version and provide a short self-contained code example. It would probably also been good if you could create a separate GitHub issue for this, since this one seems to have been fixed already in 2008.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants