From: Bill Erickson Date: Mon, 5 May 2014 17:40:46 +0000 (-0400) Subject: LP#1316245: JS now uses browser-native JSON routines X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=6c3b1001c6183b2f633d0f43ccd3d28071c0c48d;p=working%2FOpenSRF.git LP#1316245: JS now uses browser-native JSON routines * Replace JSON2jsRaw() with JSON.parse() * Replace js2JSONRaw() with JSON.stringify() * Removed unit tests for JSON2jsRaw() and js2JSONRaw() * Removed jsonPretty() pretty-printing routine. Use JSON.stringify(json, null, ) instead. See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON Signed-off-by: Bill Erickson Signed-off-by: Galen Charlton --- diff --git a/src/javascript/JSON_v1.js b/src/javascript/JSON_v1.js index 37ff048..3148a50 100644 --- a/src/javascript/JSON_v1.js +++ b/src/javascript/JSON_v1.js @@ -6,18 +6,11 @@ var JSON_DATA_KEY = '__p'; function JSON_version() { return 'wrapper'; } function JSON2js(text) { - return decodeJS(JSON2jsRaw(text)); + return decodeJS(JSON.parse(text)); } JSON2js.fallbackObjectifier = null; -function JSON2jsRaw(text) { - var obj; - eval('obj = ' + text); - return obj; -} - - /* iterates over object, arrays, or fieldmapper objects */ function jsIterate( arg, callback ) { if( arg && typeof arg == 'object' ) { @@ -132,90 +125,5 @@ function encodeJS(arg) { /* turns a javascript object into a JSON string */ function js2JSON(arg) { - return js2JSONRaw(encodeJS(arg)); + return JSON.stringify(encodeJS(arg)); } - -function js2JSONRaw(arg) { - - if( arg == null ) - return 'null'; - - var o; - - switch (typeof arg) { - - case 'object': - - if (arg.constructor == Array) { - o = ''; - jsIterate( arg, - function(obj, i) { - if (o) o += ','; - o += js2JSONRaw(obj[i]); - } - ); - return '[' + o + ']'; - - } else if (typeof arg.toString != 'undefined') { - o = ''; - jsIterate( arg, - function(obj, i) { - if (o) o += ','; - o = o + js2JSONRaw(i) + ':' + js2JSONRaw(obj[i]); - } - ); - return '{' + o + '}'; - - } - - return 'null'; - - case 'number': return arg; - - case 'string': - var s = String(arg); - s = s.replace(/\\/g, '\\\\'); - s = s.replace(/"/g, '\\"'); - s = s.replace(/\t/g, "\\t"); - s = s.replace(/\n/g, "\\n"); - s = s.replace(/\r/g, "\\r"); - s = s.replace(/\f/g, "\\f"); - return '"' + s + '"'; - - case 'boolean': - return (arg) ? 'true' : 'false'; - - default: return 'null'; - } -} - - -function __tabs(c) { - var s = ''; - for( i = 0; i < c; i++ ) s += '\t'; - return s; -} - -function jsonPretty(str) { - if(!str) return ""; - var s = ''; - var d = 0; - for( var i = 0; i < str.length; i++ ) { - var c = str.charAt(i); - if( c == '{' || c == '[' ) { - s += c + '\n' + __tabs(++d); - } else if( c == '}' || c == ']' ) { - s += '\n' + __tabs(--d) + '\n'; - if( str.charAt(i+1) == ',' ) { - s += '\n' + __tabs(d); - } - } else if( c == ',' ) { - s += ',\n' + __tabs(d); - } else { - s += c; - } - } - return s; -} - - diff --git a/src/javascript/tests/testJSON_v1.js b/src/javascript/tests/testJSON_v1.js index 9530fa0..9039444 100644 --- a/src/javascript/tests/testJSON_v1.js +++ b/src/javascript/tests/testJSON_v1.js @@ -45,66 +45,6 @@ doh.register("JSONTests", [ // Order of object attributes is not guaranteed doh.assertTrue(js2JSON({"foo":{"one":[null,"two",2]}}) == '{"foo":{"one":[null,"two",2]}}'); }, - function test_js2JSONRaw_strict() { - // Solo nulls and booleans are stringified XXX - doh.assertTrue(js2JSONRaw(null) === "null"); - doh.assertTrue(js2JSONRaw(true) === "true"); - doh.assertTrue(js2JSONRaw(false) === "false"); - }, - function test_js2JSONRaw_numbers() { - doh.assertTrue(js2JSONRaw(0) === 0); - doh.assertTrue(js2JSONRaw(1.5) === 1.5); - doh.assertTrue(js2JSONRaw(.7) === .7); - }, - function test_js2JSONRaw_strings() { - doh.assertTrue(js2JSONRaw("") == '""'); - doh.assertTrue(js2JSONRaw("foo") == '"foo"'); - // Escape sequences - doh.assertTrue(js2JSONRaw("foo\n\t\n") == '"foo\\n\\t\\n"'); - }, - function test_js2JSONRaw_arrays() { - doh.assertTrue(js2JSONRaw([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]'); - }, - function test_js2JSONRaw_objects() { - doh.assertTrue(js2JSONRaw({"foo":"bar"}) == '{"foo":"bar"}'); - doh.assertTrue(js2JSONRaw({"foo":true}) == '{"foo":true}'); - doh.assertTrue(js2JSONRaw({"foo":0}) == '{"foo":0}'); - }, - function test_js2JSONRaw_objects_ordered() { - // Order of object attributes is not guaranteed - doh.assertTrue(js2JSONRaw({"foo":{"one":[null,"two",2]}}) == '{"foo":{"one":[null,"two",2]}}'); - }, - function test_JSON2jsRaw_strict() { - // Standalone quoted nulls and booleans are converted to primitives - doh.assertTrue(JSON2jsRaw(null) === null); - doh.assertTrue(JSON2jsRaw("null") === null); - doh.assertTrue(JSON2jsRaw(true) === true); - doh.assertTrue(JSON2jsRaw("true") === true); - doh.assertTrue(JSON2jsRaw(false) === false); - doh.assertTrue(JSON2jsRaw("false") === false); - }, - function test_JSON2jsRaw_numbers() { - // Zero is zero and only zero - doh.assertTrue(JSON2jsRaw(0) === 0); - doh.assertTrue(JSON2jsRaw(1.5) === 1.5); - doh.assertTrue(JSON2jsRaw(.5) === .5); - }, - function test_JSON2jsRaw_strings() { - // Empty string - doh.assertTrue(JSON2jsRaw('""') === ""); - // String - doh.assertTrue(JSON2jsRaw('"foo"') == "foo"); - }, - function test_JSON2jsRaw_arrays() { - // Array; access an index - doh.assertTrue(JSON2jsRaw('[0,1,2,3,4,5]')[1] == 1); - }, - function test_JSON2jsRaw_objects() { - // Object; access a key - doh.assertTrue(JSON2jsRaw('{"foo":"bar"}').foo == "bar"); - doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":null}}').foo.one === null); - doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":"null"}}').foo.one === "null"); - }, function test_JSON2js_strict() { // Standalone quoted nulls and booleans are converted to primitives doh.assertTrue(JSON2js(null) === null);