user_unknown user_unknown - 1 year ago 277
Ajax Question

How to handle big integer numbers in JSON response in Javascript

I am developing a web application using Javascript & ExtJS framework.

I need to find a generic solution to following problem -

In my application to bring data for a Employee i give call to server api which gives me json response of the Employee data -

SERVER RESPONSE : {"id":10765432100123456789,"id_str":"10765432100123456789", "name" : "Employee1"}

Now on client side i am using ExtJS ajax call which gives call to server and gives the response by converting the response text to an object.

But when i inspect the object then i noticed that value of id key is changed to a different number and value of id_str key remains same.

When i tried to find root cause for this then i came to know about javascript number restriction(Javascript supports at most 53 bits for integer).

Now in my complete application i am using id key to refer employee id. Now i dont want to go to each place and change code to refer id_str for id value.

Note - In my application Employee is one object that i have refered but there are around 20+ object for which i have written code on the id key.

Can some one please guide me what is the best possible solution to handle this kind of scenario.

I am thinking of overriding the JSON.parse(ExtJS internally uses this function to conver json string to an object) function in following way -

var oldFN = JSON.parse;

JSON.parse = function(){
var jsonStrParam = arguments[0];
//TODO Read jsonStrParam and write custom logic to convert id integer values to string values
var reviverFuncParam = null;
if(arguments.length == 2)
reviverFuncParam = arguments[1];
return oldFN(jsonStrParam,reviverFuncParam);

In my above approach i have to write code to convert id integer values to string values and then give it to parse function.

I think this will cause performance issues as i have parse the string and iterate over it and change integer values of each key to string values.

Can someone help to figure out what is the best solution that i need to write to take care of big integer numbers at javascript side.

Whether this should be handled at client(browser javascript) side or server side(Java DAO layer) ?

Answer Source

I had the same problem. In my application, the ID was passed to the client side and only ever used to reference data (e.g. I never increased or decreased it or in any other way rely on its being a number).

So what I did was to send a modified ID in the JSON:

    "id" : "id#10765432100123456789",
    "name" : "Employee1"

and then on the input side I set up a filter that, if a parameter value began with "id#", stripped the first three characters and converted the rest to int64_t.

Since the Javascript side's ID now begins with id#, it is always treated as a string. Problem solved.

If you use it in a selector, you may want to use a prefix such as __id__ instead of id#:

$('#__id__10765432100123456789').css({ backgroundColor: "red" });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download