Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/webroot/rsrc/externals/javelin/lib/JSON.js
12242 views
1
/**
2
* Simple JSON serializer.
3
*
4
* @requires javelin-install
5
* @provides javelin-json
6
* @javelin
7
*/
8
9
/**
10
* JSON serializer and parser. This class uses the native JSON parser if it is
11
* available; if not, it provides an eval-based parser and a simple serializer.
12
*
13
* NOTE: This class uses eval() on some systems, without sanitizing input. It is
14
* not safe to use with untrusted data. Javelin does not provide a library
15
* suitable for parsing untrusted JSON.
16
*
17
* Usage is straightforward:
18
*
19
* JX.JSON.stringify({"bees":"knees"}); // Returns string: '{"bees":"knees"}'
20
* JX.JSON.parse('{"bees":"knees"}'); // Returns object: {"bees":"knees"}
21
*
22
* @task json JSON Manipulation
23
* @task internal Internal
24
*/
25
JX.install('JSON', {
26
statics : {
27
28
29
/* -( JSON Manipulation )-------------------------------------------------- */
30
31
32
/**
33
* Parse a **trusted** JSON string into an object. Accepts a valid JSON
34
* string and returns the object it encodes.
35
*
36
* NOTE: This method does not sanitize input and uses an eval-based parser
37
* on some systems. It is **NOT SAFE** to use with untrusted inputs.
38
*
39
* @param string A valid, trusted JSON string.
40
* @return object The object encoded by the JSON string.
41
* @task json
42
*/
43
parse : function(data) {
44
if (typeof data != 'string') {
45
return null;
46
}
47
48
if (window.JSON && JSON.parse) {
49
var obj;
50
try {
51
obj = JSON.parse(data);
52
} catch (e) {}
53
return obj || null;
54
}
55
56
return eval('(' + data + ')');
57
},
58
59
/**
60
* Serialize an object into a JSON string. Accepts an object comprised of
61
* maps, lists and scalars and transforms it into a JSON representation.
62
* This method has undefined behavior if you pass in other complicated
63
* things, e.g. object graphs containing cycles, document.body, or Date
64
* objects.
65
*
66
* @param object An object comprised of maps, lists and scalars.
67
* @return string JSON representation of the object.
68
* @task json
69
*/
70
stringify : function(val) {
71
if (window.JSON && JSON.stringify) {
72
return JSON.stringify(val);
73
}
74
75
var out = [];
76
if (
77
val === null || val === true || val === false || typeof val == 'number'
78
) {
79
return '' + val;
80
}
81
82
if (val.push && val.pop) {
83
var v;
84
for (var ii = 0; ii < val.length; ii++) {
85
86
// For consistency with JSON.stringify(), encode undefined array
87
// indices as null.
88
v = (typeof val[ii] == 'undefined') ? null : val[ii];
89
90
out.push(JX.JSON.stringify(v));
91
}
92
return '[' + out.join(',') + ']';
93
}
94
95
if (typeof val == 'string') {
96
return JX.JSON._esc(val);
97
}
98
99
for (var k in val) {
100
out.push(JX.JSON._esc(k) + ':' + JX.JSON.stringify(val[k]));
101
}
102
return '{' + out.join(',') + '}';
103
},
104
105
106
/* -( Internal )----------------------------------------------------------- */
107
108
109
// Lifted more or less directly from Crockford's JSON2.
110
_escexp : /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
111
112
// List of control character escape codes.
113
_meta : {
114
'\b' : '\\b',
115
'\t' : '\\t',
116
'\n' : '\\n',
117
'\f' : '\\f',
118
'\r' : '\\r',
119
'"' : '\\"',
120
'\\' : '\\\\'
121
},
122
123
/**
124
* Quote and escape a string for inclusion in serialized JSON. Finds
125
* characters in the string which need to be escaped and uses
126
* @{method:_replace} to escape them.
127
*
128
* @param string Unescaped string.
129
* @return string Escaped string.
130
* @task internal
131
*/
132
_esc : function(str) {
133
JX.JSON._escexp.lastIndex = 0;
134
return JX.JSON._escexp.test(str) ?
135
'"' + str.replace(JX.JSON._escexp, JX.JSON._replace) + '"' :
136
'"' + str + '"';
137
},
138
139
/**
140
* Helper callback for @{method:_esc}, escapes characters which can't be
141
* represented normally in serialized JSON.
142
*
143
* @param string Unescaped character.
144
* @return string Escaped character.
145
* @task internal
146
*/
147
_replace : function(m) {
148
if (m in JX.JSON._meta) {
149
return JX.JSON._meta[m];
150
}
151
return '\\u' + (('0000' + m.charCodeAt(0).toString(16)).slice(-4));
152
}
153
}
154
});
155
156