json_wev8.js
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.json");
dojo.require("dojo.lang");
dojo.json = {
jsonRegistry: new dojo.AdapterRegistry(),
register: function(name, check, wrap, /*optional*/ override){
/***
Register a JSON serialization function. JSON serialization
functions should take one argument and return an object
suitable for JSON serialization:
- string
- number
- boolean
- undefined
- object
- null
- Array-like (length property that is a number)
- Objects with a "json" method will have this method called
- Any other object will be used as {key:value, ...} pairs
If override is given, it is used as the highest priority
JSON serialization, otherwise it will be used as the lowest.
***/
dojo.json.jsonRegistry.register(name, check, wrap, override);
},
evalJSON: function(){
// FIXME: should this accept mozilla's optional second arg?
return eval("(" + arguments[0] + ")");
},
serialize: function(o){
/***
Create a JSON serialization of an object, note that this doesn't
check for infinite recursion, so don't do that!
***/
var objtype = typeof(o);
if(objtype == "undefined"){
return "undefined";
}else if((objtype == "number")||(objtype == "boolean")){
return o + "";
}else if(o === null){
return "null";
}
var m = dojo.lang;
if(objtype == "string"){
return m.reprString(o);
}
// recurse
var me = arguments.callee;
// short-circuit for objects that support "json" serialization
// if they return "self" then just pass-through...
var newObj;
if(typeof(o.__json__) == "function"){
newObj = o.__json__();
if(o !== newObj){
return me(newObj);
}
}
if(typeof(o.json) == "function"){
newObj = o.json();
if (o !== newObj) {
return me(newObj);
}
}
// array
if(objtype != "function" && typeof(o.length) == "number"){
var res = [];
for(var i = 0; i < o.length; i++){
var val = me(o[i]);
if(typeof(val) != "string"){
val = "undefined";
}
res.push(val);
}
return "[" + res.join(",") + "]";
}
// look in the registry
try {
newObj = dojo.json.jsonRegistry.match(o);
return me(newObj);
}catch(e){
dojo.debug(e);
}
// it's a function with no adapter, bad
if(objtype == "function"){
return null;
}
// generic object code path
res = [];
for (var k in o){
var useKey;
if (typeof(k) == "number"){
useKey = '"' + k + '"';
}else if (typeof(k) == "string"){
useKey = m.reprString(k);
}else{
// skip non-string or number keys
continue;
}
val = me(o[k]);
if(typeof(val) != "string"){
// skip non-serializable values
continue;
}
res.push(useKey + ":" + val);
}
return "{" + res.join(",") + "}";
}
};