Deliberately break applications relying on pre-OpenSRF 1.0 behaviour.
authordbs <dbs@9efc2488-bf62-4759-914b-345cdb29e865>
Thu, 18 Oct 2007 03:08:38 +0000 (03:08 +0000)
committerdbs <dbs@9efc2488-bf62-4759-914b-345cdb29e865>
Thu, 18 Oct 2007 03:08:38 +0000 (03:08 +0000)
JSON_v0.js represents "legacy" JSON format, while JSON_v1.js represents "modern" JSON format.

git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1110 9efc2488-bf62-4759-914b-345cdb29e865

examples/math_xul_client/math/content/math.xul
src/javascript/JSON.js [deleted file]
src/javascript/JSON_v0.js [new file with mode: 0644]
src/javascript/JSON_v1.js [new file with mode: 0644]
src/javascript/JSON_wrapper.js [deleted file]

index 1396a0b..d6a1371 100644 (file)
@@ -16,7 +16,7 @@
 
        <!-- OpenSRF -->
        <script>var myPackageDir = "math";</script>
-       <script src="OpenSRF/JSON.js" />
+       <script src="OpenSRF/JSON_v1.js" />
        <script src="OpenSRF/md5.js" />
        <script src="OpenSRF/opensrf_utils.js" />
        <script src="OpenSRF/opensrf_config.js" />
diff --git a/src/javascript/JSON.js b/src/javascript/JSON.js
deleted file mode 100644 (file)
index e52a439..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-// in case we run on an implimentation that doesn't have "undefined";
-var undefined;
-
-function Cast (obj, class_constructor) {
-       try {
-               if (eval(class_constructor + '["_isfieldmapper"]')) {
-                       obj = eval("new " + class_constructor + "(obj)");
-               }
-       } catch( E ) {
-               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")');
-
-       var obj;
-       if (json != '') {
-               try {
-                       eval( 'obj = ' + json );
-               } catch(E) {
-                       debug("Error building JSON object with string " + E + "\nString:\n" + json );
-                       return null;
-               }
-       }
-       return obj;
-}
-
-
-function object2Array(obj) {
-       if( obj == null ) return null;
-
-       var arr = new Array();
-       for( var i  = 0; i < obj.length; i++ ) {
-               arr[i] = obj[i];
-       }
-       return arr;
-}
-
-
-function js2JSON(arg) {
-       return _js2JSON(arg);
-}
-
-function _js2JSON(arg) {
-       var i, o, u, v;
-
-               switch (typeof arg) {
-                       case 'object':
-       
-                               if(arg) {
-       
-                                       if (arg._isfieldmapper) { /* magi-c-ast for fieldmapper objects */
-       
-                                               if( arg.a.constructor != Array ) {
-                                                       var arr = new Array();
-                                                       for( var i  = 0; i < arg.a.length; i++ ) {
-                                                               if( arg.a[i] == null ) {
-                                                                       arr[i] = null; continue;
-                                                               }
-       
-                                                               if( typeof arg.a[i] != 'object' ) { 
-                                                                       arr[i] = arg.a[i];
-       
-                                                               } else if( typeof arg.a[i] == 'object' 
-                                                                                       && arg.a[i]._isfieldmapper) {
-       
-                                                                       arr[i] = arg.a[i];
-       
-                                                               } else {
-                                                                       arr[i] = object2Array(arg.a[i]);                
-                                                               }
-                                                       }
-                                                       arg.a = arr;
-                                               }
-       
-                                               return "/*--S " + arg.classname + " --*/" + js2JSON(arg.a) + "/*--E " + arg.classname + " --*/";
-       
-                                       } else {
-       
-                                               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 (o) {
-                                                                               o += ',';
-                                                                       }
-                                                                       o += js2JSON(i) + ':' + v;
-                                                               }
-                                                       }
-       
-                                                       o = '{' + o + '}';
-                                                       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') + '"';
-               }
-
-}
diff --git a/src/javascript/JSON_v0.js b/src/javascript/JSON_v0.js
new file mode 100644 (file)
index 0000000..e52a439
--- /dev/null
@@ -0,0 +1,135 @@
+// in case we run on an implimentation that doesn't have "undefined";
+var undefined;
+
+function Cast (obj, class_constructor) {
+       try {
+               if (eval(class_constructor + '["_isfieldmapper"]')) {
+                       obj = eval("new " + class_constructor + "(obj)");
+               }
+       } catch( E ) {
+               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")');
+
+       var obj;
+       if (json != '') {
+               try {
+                       eval( 'obj = ' + json );
+               } catch(E) {
+                       debug("Error building JSON object with string " + E + "\nString:\n" + json );
+                       return null;
+               }
+       }
+       return obj;
+}
+
+
+function object2Array(obj) {
+       if( obj == null ) return null;
+
+       var arr = new Array();
+       for( var i  = 0; i < obj.length; i++ ) {
+               arr[i] = obj[i];
+       }
+       return arr;
+}
+
+
+function js2JSON(arg) {
+       return _js2JSON(arg);
+}
+
+function _js2JSON(arg) {
+       var i, o, u, v;
+
+               switch (typeof arg) {
+                       case 'object':
+       
+                               if(arg) {
+       
+                                       if (arg._isfieldmapper) { /* magi-c-ast for fieldmapper objects */
+       
+                                               if( arg.a.constructor != Array ) {
+                                                       var arr = new Array();
+                                                       for( var i  = 0; i < arg.a.length; i++ ) {
+                                                               if( arg.a[i] == null ) {
+                                                                       arr[i] = null; continue;
+                                                               }
+       
+                                                               if( typeof arg.a[i] != 'object' ) { 
+                                                                       arr[i] = arg.a[i];
+       
+                                                               } else if( typeof arg.a[i] == 'object' 
+                                                                                       && arg.a[i]._isfieldmapper) {
+       
+                                                                       arr[i] = arg.a[i];
+       
+                                                               } else {
+                                                                       arr[i] = object2Array(arg.a[i]);                
+                                                               }
+                                                       }
+                                                       arg.a = arr;
+                                               }
+       
+                                               return "/*--S " + arg.classname + " --*/" + js2JSON(arg.a) + "/*--E " + arg.classname + " --*/";
+       
+                                       } else {
+       
+                                               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 (o) {
+                                                                               o += ',';
+                                                                       }
+                                                                       o += js2JSON(i) + ':' + v;
+                                                               }
+                                                       }
+       
+                                                       o = '{' + o + '}';
+                                                       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') + '"';
+               }
+
+}
diff --git a/src/javascript/JSON_v1.js b/src/javascript/JSON_v1.js
new file mode 100644 (file)
index 0000000..cfc0a36
--- /dev/null
@@ -0,0 +1,201 @@
+var JSON_CLASS_KEY     = '__c';
+var JSON_DATA_KEY      = '__p';
+
+
+
+function JSON_version() { return 'wrapper' }
+
+function JSON2js(text) {
+       return decodeJS(JSON2jsRaw(text));
+}
+
+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' ) {
+               if( arg.constructor == Array ) {
+                       for( var i = 0; i < arg.length; i++ ) 
+                               callback(arg, i);
+
+               }  else if( arg.constructor == Object ) {
+                               for( var i in arg ) 
+                                       callback(arg, i);
+
+               } else if( arg._isfieldmapper && arg.a ) {
+                       for( var i = 0; i < arg.a.length; i++ ) 
+                               callback(arg.a, i);
+               }
+       }
+}
+
+
+/* removes the class/paylod wrapper objects */
+function decodeJS(arg) {
+
+       if(arg == null) return null;
+
+       if(     arg && typeof arg == 'object' &&
+                       arg.constructor == Object &&
+                       arg[JSON_CLASS_KEY] ) {
+               eval('arg = new ' + arg[JSON_CLASS_KEY] + '(arg[JSON_DATA_KEY])');      
+       }
+
+       jsIterate( arg, 
+               function(o, i) {
+                       o[i] = decodeJS(o[i]);
+               }
+       );
+
+       return arg;
+}
+
+
+function jsClone(obj) {
+       if( obj == null ) return null;
+       if( typeof obj != 'object' ) return obj;
+
+       var newobj;
+       if (obj.constructor == Array) {
+               newobj = [];
+               for( var i = 0; i < obj.length; i++ ) 
+                       newobj[i] = jsClone(obj[i]);
+
+       } else if( obj.constructor == Object ) {
+               newobj = {};
+               for( var i in obj )
+                       newobj[i] = jsClone(obj[i]);
+
+       } else if( obj._isfieldmapper && obj.a ) {
+               eval('newobj = new '+obj.classname + '();');
+               for( var i = 0; i < obj.a.length; i++ ) 
+                       newobj.a[i] = jsClone(obj.a[i]);
+       }
+
+       return newobj;
+}
+       
+
+/* adds the class/paylod wrapper objects */
+function encodeJS(arg) {
+       if( arg == null ) return null;  
+       if( typeof arg != 'object' ) return arg;
+
+       if( arg._isfieldmapper ) {
+      var newarr = []
+      if(!arg.a) arg.a = [];
+               for( var i = 0; i < arg.a.length; i++ ) 
+                       newarr[i] = encodeJS(arg.a[i]);
+
+               var a = {};
+               a[JSON_CLASS_KEY] = arg.classname;
+               a[JSON_DATA_KEY] = newarr;
+      return a;
+       }
+
+       var newobj;
+
+       if(arg.length != undefined) {
+               newobj = [];
+               for( var i = 0; i < arg.length; i++ ) 
+         newobj.push(encodeJS(arg[i]));
+      return newobj;
+       } 
+   
+       newobj = {};
+       for( var i in arg )
+               newobj[i] = encodeJS(arg[i]);
+       return newobj;
+}
+
+/* turns a javascript object into a JSON string */
+function js2JSON(arg) {
+       return js2JSONRaw(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 + '}';
+
+                       } else {
+                               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 + '"';
+
+               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/JSON_wrapper.js b/src/javascript/JSON_wrapper.js
deleted file mode 100644 (file)
index cfc0a36..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-var JSON_CLASS_KEY     = '__c';
-var JSON_DATA_KEY      = '__p';
-
-
-
-function JSON_version() { return 'wrapper' }
-
-function JSON2js(text) {
-       return decodeJS(JSON2jsRaw(text));
-}
-
-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' ) {
-               if( arg.constructor == Array ) {
-                       for( var i = 0; i < arg.length; i++ ) 
-                               callback(arg, i);
-
-               }  else if( arg.constructor == Object ) {
-                               for( var i in arg ) 
-                                       callback(arg, i);
-
-               } else if( arg._isfieldmapper && arg.a ) {
-                       for( var i = 0; i < arg.a.length; i++ ) 
-                               callback(arg.a, i);
-               }
-       }
-}
-
-
-/* removes the class/paylod wrapper objects */
-function decodeJS(arg) {
-
-       if(arg == null) return null;
-
-       if(     arg && typeof arg == 'object' &&
-                       arg.constructor == Object &&
-                       arg[JSON_CLASS_KEY] ) {
-               eval('arg = new ' + arg[JSON_CLASS_KEY] + '(arg[JSON_DATA_KEY])');      
-       }
-
-       jsIterate( arg, 
-               function(o, i) {
-                       o[i] = decodeJS(o[i]);
-               }
-       );
-
-       return arg;
-}
-
-
-function jsClone(obj) {
-       if( obj == null ) return null;
-       if( typeof obj != 'object' ) return obj;
-
-       var newobj;
-       if (obj.constructor == Array) {
-               newobj = [];
-               for( var i = 0; i < obj.length; i++ ) 
-                       newobj[i] = jsClone(obj[i]);
-
-       } else if( obj.constructor == Object ) {
-               newobj = {};
-               for( var i in obj )
-                       newobj[i] = jsClone(obj[i]);
-
-       } else if( obj._isfieldmapper && obj.a ) {
-               eval('newobj = new '+obj.classname + '();');
-               for( var i = 0; i < obj.a.length; i++ ) 
-                       newobj.a[i] = jsClone(obj.a[i]);
-       }
-
-       return newobj;
-}
-       
-
-/* adds the class/paylod wrapper objects */
-function encodeJS(arg) {
-       if( arg == null ) return null;  
-       if( typeof arg != 'object' ) return arg;
-
-       if( arg._isfieldmapper ) {
-      var newarr = []
-      if(!arg.a) arg.a = [];
-               for( var i = 0; i < arg.a.length; i++ ) 
-                       newarr[i] = encodeJS(arg.a[i]);
-
-               var a = {};
-               a[JSON_CLASS_KEY] = arg.classname;
-               a[JSON_DATA_KEY] = newarr;
-      return a;
-       }
-
-       var newobj;
-
-       if(arg.length != undefined) {
-               newobj = [];
-               for( var i = 0; i < arg.length; i++ ) 
-         newobj.push(encodeJS(arg[i]));
-      return newobj;
-       } 
-   
-       newobj = {};
-       for( var i in arg )
-               newobj[i] = encodeJS(arg[i]);
-       return newobj;
-}
-
-/* turns a javascript object into a JSON string */
-function js2JSON(arg) {
-       return js2JSONRaw(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 + '}';
-
-                       } else {
-                               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 + '"';
-
-               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;
-}
-
-