alecgregory

Long integer primary keys and JSON

Discussion created by alecgregory on May 10, 2018
Latest reply on Oct 20, 2018 by taylorsharpe

TLDR: If you use long integers as primary keys then encode them as JSONString (not JSONNumber) to prevent a loss of precision.

 

I have found that if you encode long integers (I believe more than 15 digits) as JSON they are converted to exponential notation as expected. The problem is that when they are decoded they will no longer match the original value.

 

For Example

 

JSONGetElement (

     JSONSetElement (

          "";

          "key";

          12063660878882855000013426248015578834577;

          JSONNumber

     );

     "key:

) // = 1.20636608788829e+40

     ≠ 12063660878882855000013426248015578834577

 

This is particularly problematic if you pass long numeric primary keys in JSON objects encoded as JSONNumber. Encoding them as JSONString gets round the problem, but if you aren't putting the resulting string in a number field you'll need to use GetAsNumber to convert the result back to a number afterwards.

Outcomes