forked from graniteds/jsonr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonr.js
371 lines (288 loc) · 11.4 KB
/
jsonr.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
jsonr.js
2013-07-23
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
Author: Franck WOLFF (http://www.graniteds.org)
Based on the work of Douglas Crockford
See http://www.JSON.org/js.html
This code should be minified before deployment.
See http://javascript.crockford.com/jsmin.html
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
NOT CONTROL.
This file creates a global JSONR object containing four methods: stringify,
isReference, revealReferences and parse:
- JSONR.stringify(value, replacer, space)
value any JavaScript value, usually an object or array.
replacer an optional parameter that determines how object
values are stringified for objects. It can be a
function or an array of strings.
space an optional parameter that specifies the indentation
of nested structures. If it is omitted, the text will
be packed without extra whitespace. If it is a number,
it will specify the number of spaces to indent at each
level. If it is a string (such as '\t' or ' '),
it contains the characters used to indent at each level.
This method produces a JSON text from a JavaScript value. If it encounters
multiple references to the same object instance (even circular references),
it only encodes the first occurrence and put a reference for all others in
the form of: "\uXXXX" (with XXXX = (index of instance + 0xE000)).
See implementation for detail.
- JSONR.isReference(value)
value any JavaScript value.
This method returns true if the value is a String of length 1 with a
unicode character in the range \uE000 - \uF8FF (ie. UTF8 private usage area).
- JSONR.revealReferences(text)
text a JSON text encoded by the JSONR.stringify method.
This method returns its parameter with all references replaced by human
readable text (eg. "\uE000" -> "^0", "\uE001" -> "^1", ...,
"\uE00A" -> "^10", etc.)
- JSONR.parse(text, reviver)
text a JSON encoded text, such as those returned by
stringify.
reviver an optional parameter that can filter and
transform the results. It receives each of the keys and values,
and its return value is used instead of the original value.
If it returns what it received, then the structure is not
modified. If it returns undefined then the member is deleted.
This method parses a JSON text to produce a JavaScript object. If the text
parameter was obtained through a JSONR stringify method (or compatible),
this method also resolve all references (see above).
This is a reference implementation. You are free to copy, modify, or redistribute.
*/
/*jslint regexp: true*/
/*global JSONR:true*/
if (typeof JSONR !== 'object') {
JSONR = {};
(function () {
'use strict';
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent,
meta = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
rep,
dy,
maxi = 0xF8FF - 0xE000;
// JSONR.stringify
function esc(a) {
var c = meta[a];
return typeof c === 'string'
? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}
function quote(string) {
var c;
escapable.lastIndex = 0;
if (escapable.test(string)) {
string = string.replace(escapable, esc);
}
if (string.length > 0) {
c = string.charCodeAt(0);
if (c >= 0xE000 && c <= 0xF8FF) {
string = '\uE000' + string;
}
}
return '"' + string + '"';
}
function str(key, holder) {
var i,
k,
ks,
v,
length,
mind = gap,
partial,
value = holder[key];
if (value !== null && typeof value === 'object' &&
typeof value.toJSON === 'function') {
value = value.toJSON(key);
}
if (typeof rep === 'function') {
value = rep.call(holder, key, value);
}
switch (typeof value) {
case 'string':
return quote(value);
case 'number':
return isFinite(value) ? String(value) : 'null';
case 'boolean':
case 'null':
return String(value);
case 'object':
if (!value) {
return 'null';
}
i = dy.indexOf(value);
if (i !== -1) {
return '"' + String.fromCharCode(0xE000 + i) + '"';
}
if (dy.length > maxi) {
throw new Error('JSONR.stringify: too many references');
}
dy.push(value);
gap += indent;
partial = [];
if (Object.prototype.toString.apply(value) === '[object Array]') {
length = value.length;
for (i = 0; i < length; i += 1) {
partial[i] = str(i, value) || 'null';
}
v = partial.length === 0
? '[]'
: gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']';
gap = mind;
return v;
}
if (rep && typeof rep === 'object') {
length = rep.length;
for (i = 0; i < length; i += 1) {
k = rep[i];
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
} else {
ks = Object.keys(value);
length = ks.length;
for (i = 0; i < length; i += 1) {
k = ks[i];
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
v = partial.length === 0
? '{}'
: gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}';
gap = mind;
return v;
}
}
JSONR.stringify = function (value, replacer, space) {
var i, s;
indent = '';
if (typeof space === 'number') {
for (i = 0; i < space; i += 1) {
indent += ' ';
}
} else if (typeof space === 'string') {
indent = space;
}
rep = null;
if (replacer) {
if (typeof replacer === 'function') {
rep = replacer;
} else if (Object.prototype.toString.apply(replacer) === '[object Array]') {
for (i = 0; i < replacer.length; i += 1) {
if (typeof replacer[i] !== 'string') {
throw new Error('JSONR.stringify: illegal replacer');
}
}
rep = replacer.concat().sort();
} else {
throw new Error('JSONR.stringify: illegal replacer');
}
}
gap = '';
dy = [];
try {
s = str('', {'': value});
} finally {
gap = null;
indent = null;
rep = null;
dy = null;
}
return s;
};
// JSONR.isReference
JSONR.isReference = function (value) {
var c;
if (typeof value === 'string' && value.length === 1) {
c = value.charCodeAt(0);
return c >= 0xE000 && c <= 0xF8FF;
}
return false;
};
// JSONR.revealReferences
JSONR.revealReferences = function (text) {
var begins = /"(?:[^"\\]|\\.)*"|[\[\{]/g,
indexes = /\"[\uE000-\uF8FF]\"/g,
index = 0;
text = begins.test(text) ? text.replace(begins, function (s) {
if (s.charAt(0) !== '"') {
s = '@' + index + s;
index += 1;
}
return s;
}) : text;
return indexes.test(text) ? text.replace(indexes, function (i) {
return '"@' + (i.charCodeAt(1) - 0xE000) + '"';
}) : text;
};
// JSONR.parse
function unref(value) {
var c, i, k, ks, length;
switch (typeof value) {
case 'string':
if (value.length > 0) {
c = value.charCodeAt(0);
if (c >= 0xE000 && c <= 0xF8FF) {
if (value.length === 1) {
value = dy[c - 0xE000];
} else if (c === 0xE000) {
value = value.substr(1);
}
}
}
break;
case 'object':
if (value !== null) {
i = dy.indexOf(value);
if (i === -1) {
dy.push(value);
}
if (Object.prototype.toString.apply(value) === '[object Array]') {
length = value.length;
for (i = 0; i < length; i += 1) {
value[i] = unref(value[i]);
}
} else {
ks = Object.keys(value).sort();
length = ks.length;
for (i = 0; i < length; i += 1) {
k = ks[i];
value[k] = unref(value[k]);
}
}
}
break;
}
return value;
}
JSONR.parse = function (text, reviver) {
var value = JSON.parse(text, reviver);
dy = [];
try {
value = unref(value);
} finally {
dy = null;
}
return value;
};
}());
}