From fde07260bb8470bb4f8e48cca8a5293bf23095bb Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 31 Mar 2005 19:12:18 +0000 Subject: [PATCH] changed to fit fieldmapper stuff magic casting and to_stringing git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@259 9efc2488-bf62-4759-914b-345cdb29e865 --- src/javascript/JSON.js | 114 ++++++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/src/javascript/JSON.js b/src/javascript/JSON.js index 72f457e..9cdb9fb 100644 --- a/src/javascript/JSON.js +++ b/src/javascript/JSON.js @@ -3,87 +3,103 @@ var undefined; function Cast (obj, class_constructor) { try { - if (eval(class_constructor + '.prototype["class_name"]')) { - var template = eval("new " + class_constructor + "()"); - // copy the methods over to 'obj' - for (var m in obj) { - if (typeof(obj[m]) != 'undefined') { - template[m] = obj[m]; - } - } - obj = template; + if (eval(class_constructor + '["_isfieldmapper"]')) { + debug("Casting object to class " + class_constructor + "\n"); + obj = eval("new " + class_constructor + "(obj)"); + debug("My Classname: " + obj.classname); } } catch( E ) { - obj['class_name'] = function () { return class_constructor }; - //dump( super_dump(E) + "\n"); + alert( E + "\n"); } finally { return obj; } } function JSON2js (json) { - //json = String(json).replace( /\/\*--\s*S\w*?\s*?\s+\w+\s*--\*\//g, 'Cast('); - //json = String(json).replace( /\/\*--\s*E\w*?\s*?\s+(\w+)\s*--\*\//g, ', "$1")'); + + json = String(json).replace( /\/\*--\s*S\w*?\s*?\s+\w+\s*--\*\//g, 'Cast('); + json = String(json).replace( /\/\*--\s*E\w*?\s*?\s+(\w+)\s*--\*\//g, ', "$1")'); + + debug("=======================\n" + json); + var obj; if (json != '') { - eval( 'obj = ' + json ); + try { + eval( 'obj = ' + json ); + } catch(E) { + debug("Error building JSON object with string " + json ); + return null; + } } - obj.toString = function () { return js2JSON(this) }; return obj; } + function js2JSON(arg) { -var i, o, u, v; + var i, o, u, v; + + debug( "Running js2JSON on " + arg ); switch (typeof arg) { case 'object': - if (arg) { - if (arg.constructor == Array) { - o = ''; - for (i = 0; i < arg.length; ++i) { - v = js2JSON(arg[i]); - if (o) { - o += ','; - } - if (v !== u) { - o += v; - } else { - o += 'null,'; - } - } - return '[' + o + ']'; - } else if (typeof arg.toString != 'undefined') { - o = ''; - for (i in arg) { - v = js2JSON(arg[i]); - if (v !== u) { + + if(arg) { + + if (arg._isfieldmapper) { + return "/*--S acp*/" + js2JSON(arg.array) + "/*--E acp*/"; + + } else { + + if (arg.constructor == Array) { + o = ''; + for (i = 0; i < arg.length; ++i) { + v = js2JSON(arg[i]); if (o) { o += ','; } - o += js2JSON(i) + ':' + v; + if (v !== u) { + o += v; + } else { + o += 'null,'; + } } - } - var obj_start = '{'; - var obj_end = '}'; - try { - if ( arg.class_name() ) { - obj_start = '/*--S ' + arg.class_name() + '--*/{'; - obj_end = '}/*--E ' + arg.class_name() + '--*/'; + return '[' + o + ']'; + } else if (typeof arg.toString != 'undefined') { + o = ''; + for (i in arg) { + v = js2JSON(arg[i]); + if (v !== u) { + if (o) { + o += ','; + } + o += js2JSON(i) + ':' + v; + } } - } catch( E ) {} - o = obj_start + o + obj_end; - return o; - } else { - return; + var obj_start = '{'; + var obj_end = '}'; + try { + if ( arg.class_name() ) { + obj_start = '/*--S ' + arg.class_name() + '--*/{'; + obj_end = '}/*--E ' + arg.class_name() + '--*/'; + } + } catch( E ) {} + o = obj_start + o + obj_end; + return o; + } else { + return; + } } } return 'null'; + case 'unknown': case 'number': return arg; + case 'undefined': case 'function': return u; + case 'string': default: return '"' + String(arg).replace(/(["\\])/g, '\\$1') + '"'; -- 2.11.0