providing option to connect to memcache at general connect time
[OpenSRF.git] / src / javascript / JSON_v0.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                         debug("Error building JSON object with string " + E + "\nString:\n" + json );
27                         return null;
28                 }
29         }
30         return obj;
31 }
32
33
34 function object2Array(obj) {
35         if( obj == null ) return null;
36
37         var arr = new Array();
38         for( var i  = 0; i < obj.length; i++ ) {
39                 arr[i] = obj[i];
40         }
41         return arr;
42 }
43
44
45 function js2JSON(arg) {
46         return _js2JSON(arg);
47 }
48
49 function _js2JSON(arg) {
50         var i, o, u, v;
51
52                 switch (typeof arg) {
53                         case 'object':
54         
55                                 if(arg) {
56         
57                                         if (arg._isfieldmapper) { /* magi-c-ast for fieldmapper objects */
58         
59                                                 if( arg.a.constructor != Array ) {
60                                                         var arr = new Array();
61                                                         for( var i  = 0; i < arg.a.length; i++ ) {
62                                                                 if( arg.a[i] == null ) {
63                                                                         arr[i] = null; continue;
64                                                                 }
65         
66                                                                 if( typeof arg.a[i] != 'object' ) { 
67                                                                         arr[i] = arg.a[i];
68         
69                                                                 } else if( typeof arg.a[i] == 'object' 
70                                                                                         && arg.a[i]._isfieldmapper) {
71         
72                                                                         arr[i] = arg.a[i];
73         
74                                                                 } else {
75                                                                         arr[i] = object2Array(arg.a[i]);                
76                                                                 }
77                                                         }
78                                                         arg.a = arr;
79                                                 }
80         
81                                                 return "/*--S " + arg.classname + " --*/" + js2JSON(arg.a) + "/*--E " + arg.classname + " --*/";
82         
83                                         } else {
84         
85                                                 if (arg.constructor == Array) {
86                                                         o = '';
87                                                         for (i = 0; i < arg.length; ++i) {
88                                                                 v = js2JSON(arg[i]);
89                                                                 if (o) {
90                                                                         o += ',';
91                                                                 }
92                                                                 if (v !== u) {
93                                                                         o += v;
94                                                                 } else {
95                                                                         o += 'null';
96                                                                 }
97                                                         }
98                                                         return '[' + o + ']';
99         
100                                                 } else if (typeof arg.toString != 'undefined') {
101                                                         o = '';
102                                                         for (i in arg) {
103                                                                 v = js2JSON(arg[i]);
104                                                                 if (v !== u) {
105                                                                         if (o) {
106                                                                                 o += ',';
107                                                                         }
108                                                                         o += js2JSON(i) + ':' + v;
109                                                                 }
110                                                         }
111         
112                                                         o = '{' + o + '}';
113                                                         return o;
114         
115                                                 } else {
116                                                         return;
117                                                 }
118                                         }
119                                 }
120                                 return 'null';
121         
122                         case 'unknown':
123                         case 'number':
124                                 return arg;
125         
126                         case 'undefined':
127                         case 'function':
128                                 return u;
129         
130                         case 'string':
131                         default:
132                                 return '"' + String(arg).replace(/(["\\])/g, '\\$1') + '"';
133                 }
134
135 }