6

While getting a JSON response from my restful service implemented in JAVA, I am observing that the long data type values ending with 01 are rounded off to 00. For example:

the long values,

12345123459876501 is returned as 12345123459876500 (last digit rounded to 0)

12345123459876502 is returned as 12345123459876502

12345123451234501 is returned as 12345123451234500 (last digit rounded to 0)

12345123451234502 is returned as 12345123451234502

Could anybody help me understand why only the values ending with 01 are getting rounded to 00?

T.Rob
  • 31,522
  • 9
  • 59
  • 103
BalajiBabu
  • 61
  • 1
  • 2
  • 2
    Nope, not without you showing how the service is implemented or how you are parsing the JSON. – Sotirios Delimanolis Apr 09 '14 at 16:46
  • i am using POSTMAN to check my JSON response from my service. – BalajiBabu Apr 09 '14 at 16:52
  • regarding the implementation, my response is sent via the javax.ws.rs.core.Response.ResponseBuilder object as below
    ResponseBuilder responseBuilder = Response.status(ResponseStatus.SUCCESS.getHttpStatus());
    responseBuilder.entity(entity);
    return responseBuilder.build();
    The entity is an object which has the long value as one of the element
    – BalajiBabu Apr 09 '14 at 16:54
  • maybe a bit of code help. – Marco Acierno Apr 09 '14 at 16:54
  • regarding the implementation, my response is sent via the javax.ws.rs.core.Response.ResponseBuilder object as below ResponseBuilder responseBuilder = Response.status(ResponseStatus.SUCCESS.getHttpStatus()); responseBuilder.entity(entity); return responseBuilder.build(); The entity is an object which has the long value as one of the element – BalajiBabu Apr 09 '14 at 17:01
  • Since this is the first time I am trying to use this forum, I am not aware of how to format my code snippet. hence i have copy pasted as above – BalajiBabu Apr 09 '14 at 17:02
  • Could be related to http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t - the precision is lost on the client-side javascript, the java code receives the result of converting the floating-point number to integer, which looks like rounding of the lowest digits. – Oleg Estekhin Apr 09 '14 at 17:16
  • @BalajiBabu [SO comment formatting help](http://meta.stackexchange.com/q/24793/213583) – ajp15243 Apr 09 '14 at 18:05
  • Note that you don't get `long` (or `double`) values from a JSON parser, you get `Long` or `Double` or one of the `Big...` classes. You can get a valid type and then bungle it by using the wrong `xxxValue` method to access it, among other possibilities. – Hot Licks Apr 09 '14 at 21:02
  • Copy/paste a bit of the JSON source containing the values in question. – Hot Licks Apr 09 '14 at 21:03

1 Answers1

7

JSON as defined at json.org has just a single numeric type called "number". So many JSON parsers for Java will map that to double type regardless of whether it is used for integer, long, of floating-point numbers. However, a double can only hold 15-16 significant digits while a long can store more. So if you have a long value with more digits than that, precision is lost when the JSON parser converts between long and double, which changes your 01 suffix to 00. If you want to be sure all digits are preserved, you must change your field type to String and handle parsing to long by yourself.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
  • 1
    But note that good JSON parsers will avoid any loss of precision by choosing an appropriate "Big" data type if need be. – Hot Licks Apr 09 '14 at 21:04
  • 1
    @HotLicks Maybe some do, but at least Jackson collapses all numeric types to `double` internally. – Michał Kosmulski Apr 10 '14 at 08:03
  • 1
    Um, no, Jackson does not convert numeric types to `double` internally. I don't know where the idea came from, but it is completely false. With data-mapping, resulting number is determined by POJO structure, but intermediate representation fully preserves accuracy using (if need be) `BigInteger` or `BigDecimal` – StaxMan Nov 29 '14 at 03:20