Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojo._base.json"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojo._base.json"] = true;
3
dojo.provide("dojo._base.json");
4
 
5
dojo.fromJson = function(/*String*/ json){
6
	// summary:
7
	// 		evaluates the passed string-form of a JSON object
8
	// json:
9
	//		a string literal of a JSON item, for instance:
10
	//			'{ "foo": [ "bar", 1, { "baz": "thud" } ] }'
11
	// return:
12
	//		An object, the result of the evaluation
13
 
14
	// FIXME: should this accept mozilla's optional second arg?
15
	try {
16
		return eval("(" + json + ")");
17
	}catch(e){
18
		console.debug(e);
19
		return json;
20
	}
21
}
22
 
23
dojo._escapeString = function(/*String*/str){
24
	//summary:
25
	//		Adds escape sequences for non-visual characters, double quote and
26
	//		backslash and surrounds with double quotes to form a valid string
27
	//		literal.
28
	return ('"' + str.replace(/(["\\])/g, '\\$1') + '"'
29
		).replace(/[\f]/g, "\\f"
30
		).replace(/[\b]/g, "\\b"
31
		).replace(/[\n]/g, "\\n"
32
		).replace(/[\t]/g, "\\t"
33
		).replace(/[\r]/g, "\\r"); // string
34
}
35
 
36
dojo.toJsonIndentStr = "\t";
37
dojo.toJson = function(/*Object*/ it, /*Boolean?*/ prettyPrint, /*String?*/ _indentStr){
38
	// summary:
39
	//		Create a JSON serialization of an object.
40
	//		Note that this doesn't check for infinite recursion, so don't do that!
41
	//
42
	// it:
43
	//		an object to be serialized. Objects may define their own
44
	//		serialization via a special "__json__" or "json" function
45
	//		property. If a specialized serializer has been defined, it will
46
	//		be used as a fallback.
47
	//
48
	// prettyPrint:
49
	//		if true, we indent objects and arrays to make the output prettier.
50
	//		The variable dojo.toJsonIndentStr is used as the indent string
51
	//		-- to use something other than the default (tab),
52
	//		change that variable before calling dojo.toJson().
53
	//
54
	// _indentStr:
55
	//		private variable for recursive calls when pretty printing, do not use.
56
	//
57
	// return:
58
	//		a String representing the serialized version of the passed object.
59
 
60
	_indentStr = _indentStr || "";
61
	var nextIndent = (prettyPrint ? _indentStr + dojo.toJsonIndentStr : "");
62
	var newLine = (prettyPrint ? "\n" : "");
63
	var objtype = typeof(it);
64
	if(objtype == "undefined"){
65
		return "undefined";
66
	}else if((objtype == "number")||(objtype == "boolean")){
67
		return it + "";
68
	}else if(it === null){
69
		return "null";
70
	}
71
	if(dojo.isString(it)){
72
		return dojo._escapeString(it);
73
	}
74
	if(it.nodeType && it.cloneNode){ // isNode
75
		return ""; // FIXME: would something like outerHTML be better here?
76
	}
77
	// recurse
78
	var recurse = arguments.callee;
79
	// short-circuit for objects that support "json" serialization
80
	// if they return "self" then just pass-through...
81
	var newObj;
82
	if(typeof it.__json__ == "function"){
83
		newObj = it.__json__();
84
		if(it !== newObj){
85
			return recurse(newObj, prettyPrint, nextIndent);
86
		}
87
	}
88
	if(typeof it.json == "function"){
89
		newObj = it.json();
90
		if(it !== newObj){
91
			return recurse(newObj, prettyPrint, nextIndent);
92
		}
93
	}
94
	// array
95
	if(dojo.isArray(it)){
96
		var res = [];
97
		for(var i = 0; i < it.length; i++){
98
			var val = recurse(it[i], prettyPrint, nextIndent);
99
			if(typeof(val) != "string"){
100
				val = "undefined";
101
			}
102
			res.push(newLine + nextIndent + val);
103
		}
104
		return "[" + res.join(", ") + newLine + _indentStr + "]";
105
	}
106
	/*
107
	// look in the registry
108
	try {
109
		window.o = it;
110
		newObj = dojo.json.jsonRegistry.match(it);
111
		return recurse(newObj, prettyPrint, nextIndent);
112
	}catch(e){
113
		// console.debug(e);
114
	}
115
	// it's a function with no adapter, skip it
116
	*/
117
	if(objtype == "function"){
118
		return null;
119
	}
120
	// generic object code path
121
	var output = [];
122
	for(var key in it){
123
		var keyStr;
124
		if(typeof(key) == "number"){
125
			keyStr = '"' + key + '"';
126
		}else if(typeof(key) == "string"){
127
			keyStr = dojo._escapeString(key);
128
		}else{
129
			// skip non-string or number keys
130
			continue;
131
		}
132
		val = recurse(it[key], prettyPrint, nextIndent);
133
		if(typeof(val) != "string"){
134
			// skip non-serializable values
135
			continue;
136
		}
137
		// FIXME: use += on Moz!!
138
		//	 MOW NOTE: using += is a pain because you have to account for the dangling comma...
139
		output.push(newLine + nextIndent + keyStr + ": " + val);
140
	}
141
	return "{" + output.join(", ") + newLine + _indentStr + "}";
142
}
143
 
144
}