-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathjamJSON.jsxinc
More file actions
692 lines (688 loc) · 27.2 KB
/
jamJSON.jsxinc
File metadata and controls
692 lines (688 loc) · 27.2 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//------------------------------------------------------------------------------
// File: jamJSON.jsxinc
// Version: 4.5
// Release Date: 2016-09-29
// Copyright: © 2011-2016 Michel MARIANI <http://www.tonton-pixel.com/blog/>
// Licence: GPL <http://www.gnu.org/licenses/gpl.html>
//------------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
// Version History:
// 4.5:
// - Incremented version number to keep in sync with other modules.
// 4.4:
// - Normalized error messages.
// 4.0:
// - Removed reference to 'this' for main global object.
// 3.6:
// - Incremented version number to keep in sync with other modules.
// 3.5:
// - Incremented version number to keep in sync with other modules.
// 3.4:
// - Incremented version number to keep in sync with other modules.
// 3.3:
// - Incremented version number to keep in sync with other modules.
// 3.2:
// - Incremented version number to keep in sync with other modules.
// 3.1:
// - Incremented version number to keep in sync with other modules.
// 3.0:
// - Incremented version number to keep in sync with other modules.
// 2.0:
// - Renamed jamJSON.js to jamJSON.jsxinc.
// 1.0:
// - Initial release.
//------------------------------------------------------------------------------
/**
* @fileOverview
* @name jamJSON.jsxinc
* @author Michel MARIANI
*/
if (typeof jamJSON !== 'object')
{
/**
* @description Global object (used to simulate a namespace in JavaScript) containing customized JSON methods
* for translating a JavaScript data structure to and from a JSON text string; can be used in scripts written with
* the <a href="http://www.tonton-pixel.com/blog/json-photoshop-scripting/json-action-manager/">JSON Action Manager</a> engine.
* <ul>
* <li>
* Adapted from: <a href="https://github.com/douglascrockford/JSON-js/blob/master/json_parse_state.js">json_parse_state.js</a>
* and
* <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">json2.js</a>
* by Douglas CrockFord.
* </li>
* <li>
* Spotted and fixed a few problems in the Photoshop implementation of the JavaScript interpreter:
* <ul>
* <li>
* In regular expressions, hexadecimal escape sequences (both \x and \u) must be in capital letters (A-F).
* </li>
* <li>
* The precedence of nested ternary operators ( ? : ) is handled differently, extra parentheses must be used.
* </li>
* <li>
* The test (state instanceof SyntaxError) returns true even if state is a ReferenceError; in fact, it seems that all error
* types: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, are just synonyms for each other;
* therefore, one must check beforehand if a function is missing in the action table before calling it, so that no
* ReferenceError gets ever thrown...
* </li>
* </ul>
* </li>
* </ul>
* @author Michel MARIANI
* @version 4.5
* @namespace
*/
var jamJSON = { };
//
(function ()
{
// The state of the parser, one of
// 'go' The starting state
// 'ok' The final, accepting state
// 'firstokey' Ready for the first key of the object or the closing of an empty object
// 'okey' Ready for the next key of the object
// 'colon' Ready for the colon
// 'ovalue' Ready for the value half of a key/value pair
// 'ocomma' Ready for a comma or closing }
// 'firstavalue' Ready for the first value of an array or an empty array
// 'avalue' Ready for the next value of an array
// 'acomma' Ready for a comma or closing ]
var state;
var stack; // The stack, for controlling nesting.
var container; // The current container object or array
var key; // The current key
var value; // The current value
// Escapement translation table
var escapes =
{
'\\': '\\',
'"': '"',
'/': '/',
't': '\t',
'n': '\n',
'r': '\r',
'f': '\f',
'b': '\b'
};
// The action table describes the behavior of the machine. It contains an object for each token.
// Each object contains a method that is called when a token is matched in a state.
// An object will lack a method for illegal states.
var action =
{
'{':
{
go: function ()
{
stack.push ({ state: 'ok' });
container = { };
state = 'firstokey';
},
ovalue: function ()
{
stack.push ({ container: container, state: 'ocomma', key: key });
container = { };
state = 'firstokey';
},
firstavalue: function ()
{
stack.push ({ container: container, state: 'acomma' });
container = { };
state = 'firstokey';
},
avalue: function ()
{
stack.push ({ container: container, state: 'acomma' });
container = { };
state = 'firstokey';
}
},
'}':
{
firstokey: function ()
{
var pop = stack.pop ();
value = container;
container = pop.container;
key = pop.key;
state = pop.state;
},
ocomma: function ()
{
var pop = stack.pop ();
container[key] = value;
value = container;
container = pop.container;
key = pop.key;
state = pop.state;
}
},
'[':
{
go: function ()
{
stack.push ({ state: 'ok' });
container = [ ];
state = 'firstavalue';
},
ovalue: function ()
{
stack.push ({ container: container, state: 'ocomma', key: key });
container = [ ];
state = 'firstavalue';
},
firstavalue: function ()
{
stack.push ({ container: container, state: 'acomma' });
container = [ ];
state = 'firstavalue';
},
avalue: function ()
{
stack.push ({ container: container, state: 'acomma' });
container = [ ];
state = 'firstavalue';
}
},
']':
{
firstavalue: function ()
{
var pop = stack.pop ();
value = container;
container = pop.container;
key = pop.key;
state = pop.state;
},
acomma: function ()
{
var pop = stack.pop ();
container.push (value);
value = container;
container = pop.container;
key = pop.key;
state = pop.state;
}
},
':':
{
colon: function ()
{
if (container.hasOwnProperty (key))
{
throw new SyntaxError ("[jamJSON.parse] Duplicate key: “" + key + "”");
}
state = 'ovalue';
}
},
',':
{
ocomma: function ()
{
container[key] = value;
state = 'okey';
},
acomma: function ()
{
container.push (value);
state = 'avalue';
}
},
'true':
{
go: function ()
{
value = true;
state = 'ok';
},
ovalue: function ()
{
value = true;
state = 'ocomma';
},
firstavalue: function ()
{
value = true;
state = 'acomma';
},
avalue: function ()
{
value = true;
state = 'acomma';
}
},
'false':
{
go: function ()
{
value = false;
state = 'ok';
},
ovalue: function ()
{
value = false;
state = 'ocomma';
},
firstavalue: function ()
{
value = false;
state = 'acomma';
},
avalue: function ()
{
value = false;
state = 'acomma';
}
},
'null':
{
go: function ()
{
value = null;
state = 'ok';
},
ovalue: function ()
{
value = null;
state = 'ocomma';
},
firstavalue: function ()
{
value = null;
state = 'acomma';
},
avalue: function ()
{
value = null;
state = 'acomma';
}
}
};
// The actions for number tokens
var number =
{
go: function ()
{
state = 'ok';
},
ovalue: function ()
{
state = 'ocomma';
},
firstavalue: function ()
{
state = 'acomma';
},
avalue: function ()
{
state = 'acomma';
}
};
// The actions for string tokens
var string =
{
go: function ()
{
state = 'ok';
},
firstokey: function ()
{
key = value;
state = 'colon';
},
okey: function ()
{
key = value;
state = 'colon';
},
ovalue: function ()
{
state = 'ocomma';
},
firstavalue: function ()
{
state = 'acomma';
},
avalue: function ()
{
state = 'acomma';
}
};
//
var commentFunc = function () { }; // No state change
//
function debackslashify (text)
{
// Remove and replace any backslash escapement.
return text.replace (/\\(?:u(.{4})|([^u]))/g, function (a, b, c) { return (b) ? String.fromCharCode (parseInt (b, 16)) : escapes[c]; });
}
//
/**
* @description Convert a JSON text string into a JavaScript data structure.<br />
* <ul>
* <li>
* Adapted from <a href="https://github.com/douglascrockford/JSON-js/blob/master/json_parse_state.js">json_parse_state.js</a> by Douglas Crockford:
* <ul>
* <li>
* Removed the reviver parameter.
* </li>
* <li>
* Added an extra validate parameter; if false (or undefined), a simple eval is performed.
* </li>
* <li>
* Added an extra allowComments parameter; if false (or undefined), validation does not allow comments (strict JSON).
* </li>
* <li>
* Corrected the regular expression for numbers to conform to the JSON grammar;
* cf. <a href="http://www.json.org/">Introducing JSON</a> and <a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>.
* </li>
* </ul>
* </li>
* </ul>
* @param {String} text JSON text string
* @param {Boolean} [validate] validate JSON syntax while parsing
* @param {Boolean} [allowComments] validate comments too
* @returns {Object|Array|String|Number|Boolean|Null} JavaScript data structure (usually an object or array)
* @see jamJSON.stringify
* @example
* var jsonText = '{ "Last Name": "Einstein", "First Name": "Albert" }';
* try
* {
* var jsObj = jamJSON.<strong>parse</strong> (jsonText, true);
* alert (jsObj["First Name"] + " " + jsObj["Last Name"]); // -> Albert Einstein
* }
* catch (e)
* {
* alert ("E≠mc2!");
* }
*/
jamJSON.parse = function (text, validate, allowComments)
{
if (validate)
{
// Use a state machine rather than the dangerous eval function to parse a JSON text.
// A regular expression is used to extract tokens from the JSON text.
var tx = /^[\x20\t\n\r]*(?:([,:\[\]{}]|true|false|null)|(-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+\-]?[0-9]+)?)|"((?:[^\r\n\t\\\"]|\\(?:["\\\/trnfb]|u[0-9a-fA-F]{4}))*)")/;
var txc = /^[\x20\t\n\r]*(?:(\/(?:\/.*|\*(?:.|[\r\n])*?\*\/))|([,:\[\]{}]|true|false|null)|(-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+\-]?[0-9]+)?)|"((?:[^\r\n\t\\\"]|\\(?:["\\\/trnfb]|u[0-9a-fA-F]{4}))*)")/;
// The extraction process is cautious.
var r; // The result of the exec method.
var i; // The index shift in result array
var actionFunc; // The current action function
// Set the starting state.
state = 'go';
// The stack records the container, key, and state for each object or array
// that contains another object or array while processing nested structures.
stack = [ ];
// If any error occurs, we will catch it and ultimately throw a syntax error.
try
{
// For each token...
while (true)
{
i = (allowComments) ? 1 : 0;
r = (allowComments) ? txc.exec (text) : tx.exec (text);
if (!r)
{
break;
}
// r is the result array from matching the tokenizing regular expression.
// r[0] contains everything that matched, including any initial whitespace.
// r[1] contains any punctuation that was matched, or true, false, or null.
// r[2] contains a matched number, still in string form.
// r[3] contains a matched string, without quotes but with escapement.
if (allowComments && r[1])
{
// Comment: just do nothing...
actionFunc = commentFunc;
}
else if (r[i + 1])
{
// Token: execute the action for this state and token.
actionFunc = action[r[i + 1]][state];
}
else if (r[i + 2])
{
// Number token: convert the number string into a number value and execute
// the action for this state and number.
value = +r[i + 2];
actionFunc = number[state];
}
else // Do not test r[i + 3] explicitely since a string can be empty
{
// String token: replace the escapement sequences and execute the action for
// this state and string.
value = debackslashify (r[i + 3]);
actionFunc = string[state];
}
//
if (actionFunc)
{
actionFunc ();
// Remove the token from the string. The loop will continue as long as there
// are tokens. This is a slow process, but it allows the use of ^ matching,
// which assures that no illegal tokens slip through.
text = text.slice (r[0].length);
}
else
{
break;
}
}
}
catch (e)
{
// If we find a state/token combination that is illegal, then the action will
// cause an error. We handle the error by simply changing the state.
state = e;
}
// The parsing is finished. If we are not in the final 'ok' state, or if the
// remaining source text contains anything except whitespace, then we did not have
// a well-formed JSON text.
if (state !== 'ok' || /[^\x20\t\n\r]/.test (text))
{
throw state instanceof SyntaxError ? state : new SyntaxError ("[jamJSON.parse] Invalid JSON");
}
return value;
}
else
{
// Let's live dangerously (but so fast)! ;-)
return eval ('(' + text + ')');
}
};
//
var escapable = /[\\\"\x00-\x1F\x7F-\x9F\u00AD\u0600-\u0604\u070F\u17B4\u17B5\u200C-\u200F\u2028-\u202F\u2060-\u206F\uFEFF\uFFF0-\uFFFF]/g;
var meta = // table of character substitutions
{
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
var gap;
var indent;
var prefixIndent;
//
function quote (string)
{
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
escapable.lastIndex = 0;
return escapable.test (string) ?
'"' + string.replace (escapable, function (a) {
var c = meta[a];
return (typeof c === 'string') ? c : '\\u' + ('0000' + a.charCodeAt (0).toString (16).toUpperCase ()).slice (-4);
}) + '"' : '"' + string + '"';
}
//
function str (value) // Produce a string from value.
{
var i; // The loop counter.
var k; // The member key.
var v; // The member value.
var mind = gap;
var partial;
// What happens next depends on the value's type.
switch (typeof value)
{
case 'string':
return quote (value);
case 'number':
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite (value) ? String (value) : 'null';
case 'boolean':
case 'null':
// If the value is a boolean or null, convert it to a string. Note:
// typeof null does not produce 'null'. The case is included here in
// the remote chance that this gets fixed someday.
return String (value);
case 'object': // If the type is 'object', we might be dealing with an object or an array or null.
// Due to a specification blunder in ECMAScript, typeof null is 'object',
// so watch out for that case.
if (!value)
{
return 'null';
}
// Make an array to hold the partial results of stringifying this object value.
gap += indent;
partial = [ ];
// Is the value an array?
if (value.constructor === Array)
{
// The value is an array. Stringify every element.
for (i = 0; i < value.length; i++)
{
partial[i] = str (value[i]);
}
// Join all of the elements together, separated with commas, and wrap them in brackets.
v = (partial.length === 0) ?
(gap ? '[\n' + prefixIndent + mind + ']' : '[ ]') :
(gap ? '[\n' + prefixIndent + gap + partial.join (',\n' + prefixIndent + gap) + '\n' + prefixIndent + mind + ']' : '[ ' + partial.join (', ') + ' ]');
gap = mind;
return v;
}
else
{
// Iterate through all of the keys in the object.
for (k in value)
{
if (value.hasOwnProperty (k))
{
v = str (value[k]);
if (v) // Useless ?
{
partial.push (quote (k) + (gap && ((v.charAt (0) === '{') || (v.charAt (0) === '[')) ? ':\n' + prefixIndent + gap : ': ') + v);
}
}
}
// Join all of the member texts together, separated with commas, and wrap them in braces.
v = (partial.length === 0) ?
(gap ? '{\n' + prefixIndent + mind + '}' : '{ }') :
(gap ? '{\n' + prefixIndent + gap + partial.join (',\n' + prefixIndent + gap) + '\n' + prefixIndent + mind + '}' : '{ ' + partial.join (', ') + ' }');
gap = mind;
return v;
}
default:
throw new SyntaxError ("[jamJSON.stringify] Invalid JSON");
}
}
//
/**
* @description Convert a JavaScript data structure into a JSON text string.<br />
* <ul>
* <li>
* Adapted from <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">json2.js</a> by Douglas Crockford:
* <ul>
* <li>
* Removed the replacer parameter.
* </li>
* <li>
* No handling of toJSON methods whatsoever.
* </li>
* <li>
* Added an extra prefix parameter to allow the insertion of the resulting text into already-indented code.
* </li>
* <li>
* Improved indenting so that pairs of brackets { } and [ ] are always aligned on the same vertical position.
* </li>
* <li>
* Single spaces are systematically inserted for better readability when indenting is off.
* </li>
* <li>
* A syntax error is thrown for any invalid JSON element (undefined, function, etc.).
* </li>
* </ul>
* </li>
* </ul>
* @param {Object|Array|String|Number|Boolean|Null} value JavaScript data structure (usually an object or array)
* @param {String|Number} [space] Indent space string (e.g. "\t") or number of spaces
* @param {String|Number} [prefix] Prefix space string (e.g. "\t") or number of spaces
* @returns {String} JSON text string
* @see jamJSON.parse
* @example
* var dummy = null;
* var jsArr =
* [
* 3.14E0,
* 'Hello ' + 'JSON!',
* { on: (0 === 0) },
* [ 1 + 1, dummy ]
* ];
* alert (jamJSON.<strong>stringify</strong> (jsArr)); // -> [ 3.14, "Hello JSON!", { "on": true }, [ 2, null ] ]
*/
jamJSON.stringify = function (value, space, prefix)
{
// The stringify method takes a value, two optional parameters: space and prefix, and returns a JSON text.
// Use of the space parameter can produce text that is more easily readable.
// Use of the prefix parameter allows the insertion of the resulting text into some existing code already indented.
var i;
gap = '';
indent = '';
prefixIndent = '';
//
// If the space parameter is a number, make an indent string containing that many spaces.
if (typeof space === 'number')
{
for (i = 0; i < space; i++)
{
indent += ' ';
}
}
else if (typeof space === 'string') // If the space parameter is a string, it will be used as the indent string.
{
indent = space;
}
// If the prefix parameter is a number, make a prefix indent string containing that many spaces.
if (typeof prefix === 'number')
{
for (i = 0; i < prefix; i++)
{
prefixIndent += ' ';
}
}
else if (typeof prefix === 'string') // If the prefix parameter is a string, it will be used as the prefix indent string.
{
prefixIndent = prefix;
}
// Return the result of stringifying the value.
return prefixIndent + str (value);
};
} ());
}
//------------------------------------------------------------------------------