remote.Request json and big numbers

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

remote.Request json and big numbers

Defero
Hi,

i have a question about requests with the server side via json. For local testing i'm using jsonp request and on server i use rpc.
The problem comes from large numbers and javascript.

Imagine this json:
{
value : 123456789012.123456789012
}

On my, qooxdoo side i'm using strings as a representation of numbers. All numbers are always strings. That solves the problem while working with numbers on my side.
But the server doesn't communicate numbers as strings, and i can't make it to, since it is not in my control.

To solve this problem, i decided to change all the numbers to strings before they my side has a chance to round them up. So if the server sends me back:
"{ value : 123456789012.123456789012 }"
i want to do, BEFORE it is represented as a javascript object this:
"{value : \"123456789012.123456789012\" }"

and in that case i won't lose big numbers and update the database with wrong data.

That was the plan but now i'm unsure where to change this so it will work in such a way. I need to change the response in the "raw string form" before it is assigned as a js object of the jsonp and rpc requests. And right before it is sent to the server (i have to remove the " before i send it to the server also).

Is this possible or are there any better solutions? (I can't change what the server send/expects)

Regards,
Defero
Reply | Threaded
Open this post in threaded view
|

Re: remote.Request json and big numbers

Derrell Lipman
The second argument to JSON.stringify is a "replacer" function or array. In your case, you'd want a function that looks at the value, ascertains if it is a number, and returns the number converted to a string, e.g., value + "".


Cheers,

Derrell



On Wed, May 7, 2014 at 4:25 AM, Defero <[hidden email]> wrote:
Hi,

i have a question about requests with the server side via json. For local
testing i'm using jsonp request and on server i use rpc.
The problem comes from large numbers and javascript.

Imagine this json:
{
value : 123456789012.123456789012
}

On my, qooxdoo side i'm using strings as a representation of numbers. All
numbers are always strings. That solves the problem while working with
numbers on my side.
But the server doesn't communicate numbers as strings, and i can't make it
to, since it is not in my control.

To solve this problem, i decided to change all the numbers to strings before
they my side has a chance to round them up. So if the server sends me back:
"{ value : 123456789012.123456789012 }"
i want to do, BEFORE it is represented as a javascript object this:
"{value : \"123456789012.123456789012\" }"

and in that case i won't lose big numbers and update the database with wrong
data.

That was the plan but now i'm unsure where to change this so it will work in
such a way. I need to change the response in the "raw string form" before it
is assigned as a js object of the jsonp and rpc requests. And right before
it is sent to the server (i have to remove the " before i send it to the
server also).

Is this possible or are there any better solutions? (I can't change what the
server send/expects)

Regards,
Defero



--
View this message in context: http://qooxdoo.678.n2.nabble.com/remote-Request-json-and-big-numbers-tp7585656.html
Sent from the qooxdoo mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel


------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: remote.Request json and big numbers

Defero
Hi,

i tryed using the reviver function of the Json.parse. Here is what i was playing with in the qx.lang.Json.js class.

statics.parse = function(data){return JSON.parse(data, function(key, value){
            if(typeof value == "number"){
                return value+"";
            }else
                return value;
        });
 };


The fact that "value" is already an JS object. The big number is already lost. So it's not the way to go i think.


Meanwhile if i check the response of the request. I get the correct value.

Server response:
{
bigNum : 123456789012345678
}

But the problem occurs even sooner. If i do just this:
statics.parse = function(data){console.log(data);}

//outputs: "{bigNum : 123456789012345680 }"

The response "data" is still a string, but the number is already changed. That is very odd.


Regards,
Defero