Philip_Baird
11 years agoCommunity Expert
Script Extension corrupts native JScript Array
Hi, I have written a Script Extension that wraps the json2.js library.
I have encountered a problem where passing a JScript object that contains a native JScript Array from a Script Unit to the Script Extension to be parsed to JSON, the Array is being incorrectly cast to an object which causes json2 to produce incorrect JSON.
For example, in the following code (where RJSON.JSON is a Script Unit in which json2.js is wrapped):
function testToJSON() {
var obj = {
"arr": [ 123, 456 ]
}
Log.Message( RJSON.JSON.toJSON( obj ) ); // Correctly Logs {"arr":[123,456]}
Log.Message( JSON2.toJSON( obj ) ); // Incorrectly Logs {"arr":{"0":123,"1":456}}
}
RJSON.JSON.toJSON( obj ) produces the correct JSON {"arr":[123,456]}
whereas the call to the Script Extension JSON2 JSON2.toJSON( obj ) produces incorrect JSON {"arr":{"0":123,"1":456}}
In both cases, the parsing code json2 is identical, the only difference is one is in a Script Unit and the other a Script Extension.
Is this expected behaviour? I would not have expected JScript objects to be corrupted in this way.
Regards,
Phil Baird