1

I have to return a DTO from service which looks like this:

ResponseDTO
{
  Long id;
  String name;
  //getter and setter etc.
}

Service returns response in json format, and I am using org.codehaus.jackson.jaxrs.JacksonJsonProvider for conversion but at client side when I get response then id value gets changed automatically.

for e.g:- from service side I set value of id as Long.MAX_VALUE but client side json response shows me value "9223372036854776000" which is not the value which I am sending from service.

Am I missing something here?

mabi
  • 5,279
  • 2
  • 43
  • 78
naveen
  • 33
  • 4
  • So to clarify, you set `ResponseDTO.id = 9223372036854775807` and receive `{ "id": "922337203685477000", ... }`? This seems wrong for several reasons. – mabi Oct 01 '14 at 16:03
  • Yes, It seems to be wrong, but happening. – naveen Oct 01 '14 at 16:27

1 Answers1

1

The thing is that Javascript handles all Numbers as 64-bit IEEE 754 floating point numbers. These can't represent 9223372036854775807 (the value of Long.MAX_VALUE) exactly.

That's the reason why Feature.WRITE_NUMBERS_AS_STRINGS exists. You need to enable this feature to receive the actual number. Note that converting this to a Number will still result in 9223372036854776000 (round up). See this answer for how rounding works in Javascript.

Community
  • 1
  • 1
mabi
  • 5,279
  • 2
  • 43
  • 78