Initial revision
[OpenSRF.git] / src / javascript / 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 + '.prototype["class_name"]')) {
7                         var template = eval("new " + class_constructor + "()");
8                         // copy the methods over to 'obj'
9                         for (var m in obj) {
10                                 if (typeof(obj[m]) != 'undefined') {
11                                         template[m] = obj[m];
12                                 }
13                         }
14                         obj = template;
15                 }
16         } catch( E ) {
17                 obj['class_name'] = function () { return class_constructor };
18                 //dump( super_dump(E) + "\n");
19         } finally {
20                 return obj;
21         }
22 }
23
24 function JSON2js (json) {
25         json = json.replace( /\/\*--\s*S\w*?\s*?\s+\w+\s*--\*\//g, 'Cast(');
26         json = json.replace( /\/\*--\s*E\w*?\s*?\s+(\w+)\s*--\*\//g, ', "$1")');
27         var obj;
28         if (json != '') {
29                 eval( 'obj = ' + json );
30         }
31         obj.toString = function () { return js2JSON(this) };
32         return obj;
33 }
34
35 function js2JSON(arg) {
36 var i, o, u, v;
37
38         switch (typeof arg) {
39                 case 'object':
40                         if (arg) {
41                                 if (arg.constructor == Array) {
42                                         o = '';
43                                         for (i = 0; i < arg.length; ++i) {
44                                                 v = js2JSON(arg[i]);
45                                                 if (o) {
46                                                         o += ',';
47                                                 }
48                                                 if (v !== u) {
49                                                         o += v;
50                                                 } else {
51                                                         o += 'null,';
52                                                 }
53                                         }
54                                         return '[' + o + ']';
55                                 } else if (typeof arg.toString != 'undefined') {
56                                         o = '';
57                                         for (i in arg) {
58                                                 v = js2JSON(arg[i]);
59                                                 if (v !== u) {
60                                                         if (o) {
61                                                                 o += ',';
62                                                         }
63                                                         o += js2JSON(i) + ':' + v;
64                                                 }
65                                         }
66                                         var obj_start = '{';
67                                         var obj_end = '}';
68                                         try {
69                                                 if ( arg.class_name() ) {
70                                                         obj_start = '/*--S ' + arg.class_name() + '--*/{';
71                                                         obj_end   = '}/*--E ' + arg.class_name() + '--*/';
72                                                 }
73                                         } catch( E ) {}
74                                         o = obj_start + o + obj_end;
75                                         return o;
76                                 } else {
77                                         return;
78                                 }
79                         }
80                         return 'null';
81                 case 'unknown':
82                 case 'undefined':
83                 case 'function':
84                         return u;
85                 case 'string':
86                 default:
87                         return '"' + String(arg).replace(/(["\\])/g, '\\$1') + '"';
88         }
89 }