How are you handling 64bit int / long values in JSON targeting javascript in browser?

3,989 views
Skip to first unread message

RanUser

unread,
Nov 3, 2012, 1:16:31 PM11/3/12
to spray...@googlegroups.com
Curious, how are you all dealing with Long (64bit) values? 
 
We have 64bit primary keys and since javascript does not support 64bit ints, apparently have two options when sending JSON:
 
a. Send id as string.  However in the future we'll have non-javascript clients consuming our REST API, and would have liked to have the correct data type being provided without needing to do type conversion.
 
b. send an extra string field (myId gets an extra myIdStr).  Another practical solution.  Pollutes the model with these extra properties unfortunately.
 
Not sure which approach to take yet. 
 
In addition, does spray-json have any built in facilities to perform a or b above?
 
Thanks!

Mathias

unread,
Nov 5, 2012, 6:32:35 AM11/5/12
to spray...@googlegroups.com
Even though JSONs syntax allows for number literals to have an arbitrary length the clients of your REST API will have different limitations as to the maximum size of a number parsed from JSON source produced by your API. Browsers running ECMAScript for example cannot deal with the full range of 64 bit integers (their limit being all integers with a magnitude of 2^53 or less).

If you need to support browsers as well as other clients with IDs greater than 2^53 I'd probably move away from decimal ID representations and resort to corresponding string values, either in hex or base64 encoding.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
>
>

Reply all
Reply to author
Forward
0 new messages