Subversion Repositories Applications.papyrus

Rev

Rev 1371 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.json");
12
dojo.require("dojo.lang.func");
13
dojo.require("dojo.string.extras");
14
dojo.require("dojo.AdapterRegistry");
15
dojo.json = {jsonRegistry:new dojo.AdapterRegistry(), register:function (name, check, wrap, override) {
16
	dojo.json.jsonRegistry.register(name, check, wrap, override);
17
}, evalJson:function (json) {
18
	try {
19
		return eval("(" + json + ")");
20
	}
21
	catch (e) {
22
		dojo.debug(e);
23
		return json;
24
	}
25
}, serialize:function (o) {
26
	var objtype = typeof (o);
27
	if (objtype == "undefined") {
28
		return "undefined";
29
	} else {
30
		if ((objtype == "number") || (objtype == "boolean")) {
31
			return o + "";
32
		} else {
33
			if (o === null) {
34
				return "null";
35
			}
36
		}
37
	}
38
	if (objtype == "string") {
39
		return dojo.string.escapeString(o);
40
	}
41
	var me = arguments.callee;
42
	var newObj;
43
	if (typeof (o.__json__) == "function") {
44
		newObj = o.__json__();
45
		if (o !== newObj) {
46
			return me(newObj);
47
		}
48
	}
49
	if (typeof (o.json) == "function") {
50
		newObj = o.json();
51
		if (o !== newObj) {
52
			return me(newObj);
53
		}
54
	}
55
	if (objtype != "function" && typeof (o.length) == "number") {
56
		var res = [];
57
		for (var i = 0; i < o.length; i++) {
58
			var val = me(o[i]);
59
			if (typeof (val) != "string") {
60
				val = "undefined";
61
			}
62
			res.push(val);
63
		}
64
		return "[" + res.join(",") + "]";
65
	}
66
	try {
67
		window.o = o;
68
		newObj = dojo.json.jsonRegistry.match(o);
69
		return me(newObj);
70
	}
71
	catch (e) {
72
	}
73
	if (objtype == "function") {
74
		return null;
75
	}
76
	res = [];
77
	for (var k in o) {
78
		var useKey;
79
		if (typeof (k) == "number") {
80
			useKey = "\"" + k + "\"";
81
		} else {
82
			if (typeof (k) == "string") {
83
				useKey = dojo.string.escapeString(k);
84
			} else {
85
				continue;
86
			}
87
		}
88
		val = me(o[k]);
89
		if (typeof (val) != "string") {
90
			continue;
91
		}
92
		res.push(useKey + ":" + val);
93
	}
94
	return "{" + res.join(",") + "}";
95
}};
96