users@jersey.java.net

[Jersey] Issues with long serialisation

From: Derry O Sullivan <derry_at_heystaks.com>
Date: Wed, 09 May 2012 18:02:42 +0100

Hi all,

We're using Jersey/Json for a JAX-RS web service and we output longs as
part of some simple JAXB object marshalling. We spotted something today
and i've had a look around to see if it was a general issue but had no
luck debugging.

One of the objects we have uses a long as an unique identifier
(according to java specs, 2^63-1 ) which is ~ 9223372036854775807 (19
digits or so).

If we try and output a long (17 or less digits), we get it output as
intended (correctly).

If we try and output a long (18 digits up until 2^63-1), we get
incorrect output - it seems to round the last 2/3 significant digits e.g.:

     @GET
     @Path("/longTest")
     @Produces("application/json")
     public JSONObject testLong() {
         JSONObject myObject = new JSONObject();
         try {
             myObject.put("1", 1);
             myObject.put("13365766603759910L", 13365766603759910L);
             myObject.put("133614582656610538L", 133614582656610538L);
             myObject.put("9133614582656610538L", 9133614582656610538L);
         } catch (JSONException e) {
             e.printStackTrace();
         }
         return myObject;
     }


returns:

{

  *
    1: 1,
  *
    13365766603759910L: 13365766603759910,
  *
    *_133614582656610538L: 133614582656610540_*
    ,
  *
    913361458265661*_0538_*L: 913361458265661*_0000_*
      o

}

Interestingly enough, when i do the same with the long object itself, it
works fine:
@GET
     @Path("/longTest3")
     @Produces("application/json")
     public long testLong3() {
         return 133614582656610538L;
     }

outputs:
133614582656610538L

I've added POJO JSON support on initialisation of my webserver.

Should i be using a different JSONConfiguration or JAXBContextResolver
for this?

Thanks in advance for the help!