debugging the sort order stuff, changed some terminology, only
[Evergreen.git] / Open-ILS / web / opac / common / js / JSON.js
1 // in case we run on an implimentation that doesn't have "undefined";
2 var undefined;
3
4 function Cast (obj, class_constructor) {
5         try {
6                 if (eval(class_constructor + '["_isfieldmapper"]')) {
7                         obj = eval("new " + class_constructor + "(obj)");
8                 }
9         } catch( E ) {
10                 alert( E + "\n");
11         } finally {
12                 return obj;
13         }
14 }
15
16 function JSON2js (json) {
17
18         json = String(json).replace( /\/\*--\s*S\w*?\s*?\s+\w+\s*--\*\//g, 'Cast(');
19         json = String(json).replace( /\/\*--\s*E\w*?\s*?\s+(\w+)\s*--\*\//g, ', "$1")');
20
21         var obj;
22         if (json != '') {
23                 try {
24                         eval( 'obj = ' + json );
25                 } catch(E) {
26                         alert("* Fix Me *\nError parsing JSON [" + E + "]\n\n" + json );
27                         return null;
28                 }
29         }
30         return obj;
31 }
32
33
34 function object2Array(obj) {
35         if( obj == null ) return null;
36         var arr = new Array();
37         for( var i  = 0; i < obj.length; i++ ) 
38                 arr[i] = obj[i];
39         return arr;
40 }
41
42
43 function js2JSON(arg) {
44         return _js2JSON(arg);
45 }
46
47 function _js2JSON(arg) {
48         var i, o, u, v;
49
50                 switch (typeof arg) {
51                         case 'object':
52         
53                                 if(arg) {
54         
55                                         if (arg._isfieldmapper) { /* magi-c-ast for fieldmapper objects */
56         
57                                                 if( arg.a.constructor != Array ) {
58                                                         var arr = new Array();
59                                                         for( var i  = 0; i < arg.a.length; i++ ) {
60                                                                 if( arg.a[i] == null ) {
61                                                                         arr[i] = null; continue;
62                                                                 }
63         
64                                                                 if( typeof arg.a[i] != 'object' ) { 
65                                                                         arr[i] = arg.a[i];
66         
67                                                                 } else if( typeof arg.a[i] == 'object' 
68                                                                                         && arg.a[i]._isfieldmapper) {
69         
70                                                                         arr[i] = arg.a[i];
71         
72                                                                 } else {
73                                                                         arr[i] = object2Array(arg.a[i]);                
74                                                                 }
75                                                         }
76                                                         arg.a = arr;
77                                                 }
78         
79                                                 return "/*--S " + arg.classname + " --*/" + js2JSON(arg.a) + "/*--E " + arg.classname + " --*/";
80         
81                                         } else {
82         
83                                                 if (arg.constructor == Array) {
84                                                         o = '';
85                                                         for (i = 0; i < arg.length; ++i) {
86                                                                 v = js2JSON(arg[i]);
87                                                                 if (o) {
88                                                                         o += ',';
89                                                                 }
90                                                                 if (v !== u) {
91                                                                         o += v;
92                                                                 } else {
93                                                                         o += 'null';
94                                                                 }
95                                                         }
96                                                         return '[' + o + ']';
97         
98                                                 } else if (typeof arg.toString != 'undefined') {
99                                                         o = '';
100                                                         for (i in arg) {
101                                                                 v = js2JSON(arg[i]);
102                                                                 if (v !== u) {
103                                                                         if (o) {
104                                                                                 o += ',';
105                                                                         }
106                                                                         o += js2JSON(i) + ':' + v;
107                                                                 }
108                                                         }
109         
110                                                         o = '{' + o + '}';
111                                                         return o;
112         
113                                                 } else {
114                                                         return;
115                                                 }
116                                         }
117                                 }
118                                 return 'null';
119         
120                         case 'unknown':
121                         case 'number':
122                                 if( isNaN(arg) ) throw "JSON.js encountered NaN in js2JSON()";
123                                 return arg;
124         
125                         case 'undefined':
126                         case 'function':
127                                 return u;
128         
129                         case 'string':
130                         default:
131                                 return '"' + String(arg).replace(/(["\\])/g, '\\$1') + '"';
132                 }
133
134 }