-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZstringifyWork - Copy.js
1322 lines (1211 loc) · 41 KB
/
EZstringifyWork - Copy.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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------------------------------
Dreamweaver LINT global references and definitions not used here
--------------------------------------------------------------------------------------------------*/
/*global
EZ, DWfile,
e:true, g:true, dw:true, f:true
*/
var e; //global var for try/catch
(function() {[ //global variables and functions defined but not used
e, f, g, dw, DWfile ]});
//__________________________________________________________________________________________________
EZ.stringifyWork = function EZstringifyWork(value, replacer, spaces)
{
if (value == null) return '"' + value + '"';
try
{
//----- if replacer argument omited, use as spaces argument
if (arguments.length == 2 && typeof(replacer) == 'number')
{
spaces = replacer;
replacer = null;
}
//----- define global varibles
var options = replacer,
include = [], //properties included in addition to extract list
exclude = [], //properties ignored -- include has precedence
extract = [], //only extract these properties unless included
nestedKeys = ['$'], //nested key name -- not used but retained for future
objectKeys = typeof(value) == 'object' ? [Object.keys(value)] : [],
objectStack = [null],
processedObj = [],
processedKey = [],
// repeatedObj = [],
replacerFunctions = [],
pad, padding,
// errorObj,
// topKeys = [],
//escape codes
QUOTE = '@@~@@',
QUOTE_EMBEDDED = '@@!@@',
EOL = '@@eol@@',
NEWLINE = '@@EOL@@',
BACKSLASH = '@@/@@',
NAN = '@@NaN@@',
// TAB = '@@tab@@',
REGEXP = '@@RegExp@@',
UNDEFINED = '@@undefined@@',
INFINITY_PLUS = '@@Infinity@@',
INFINITY_NEG = '@@-Infinity@@',
PREFIX = '@@=@',
SUFFIX = '@=@@',
WRAPPER = RegExp('("' + PREFIX + '|' + SUFFIX + '")', 'g'); //("@@=@|@=@@")
if (this != EZ.json.stringify) //process arguments and create options
{
jsonFormatOptions();
EZ.json.repeatList = [];
}
void(0)
if (EZ.quit) return '"quit"';
pad = isNaN(options.SPACES) ? options.SPACES
: ' '.substr(0,options.SPACES)
pad = pad.substr(0,10);
padding = '\n' + pad;
/*
//----- if object is circular, returns Object of the form:
// {"Object.keys()": "id, name ...", "TypeError": "Converting circular structure to JSON"}
if (!options.CIRCULAR && (errorObj = EZ.isObjectCircular(value)))
{
value = errorObj;
jsonReplacer = null;
}
*/
//-------------------------------------------------------------
var json = JSON.stringify(value, jsonReplacer, options.SPACES);
//-------------------------------------------------------------
if (json != EZ.undefined)
json = json.replace(WRAPPER, ''); //remove outer quotes
if (this == EZ.json.stringify) //internal call from jsonReplacer()
return json;
//=======================
return jsonFinalize(json);
//========================
}
catch (e)
{
//e.format();
EZ.techSupport(e, arguments, this);
var errorObj = { 'Object.keys()': Object.keys(value).join(', ') }
errorObj[e.constructor.name] = e.message;
json = JSON.stringify(errorObj, null, options.SPACES);
throw (json);
}
//______________________________________________________________________________
/**
* finalize -- convert or remove escape codes from final json
*/
function jsonFinalize(json)
{
if (options.ROOT)
json = options.ROOT + ' = ' + (json || '""');
else if (json === undefined)
return '';
json = json.replace(RegExp(QUOTE, 'g'), '"');
// must follow un-escape QUOTE to remove nested outer quotes
json = json.replace(WRAPPER, '');
json = json.replace(RegExp(NEWLINE, 'g'), '\n'); //NEWLINE --> \n
json = json.replace(RegExp(EOL, 'g'), '\\n'); // EOL --> \\n
json = json.replace(RegExp(BACKSLASH, 'g'), '\\'); //BACKSLASH --> \
json = json.replace(RegExp('"' + UNDEFINED + '"','g'), 'undefined');
json = json.replace(RegExp('"' + REGEXP + '(.*)"','g'), function(all,regex)
{
return '/' + regex.replace(/\\\\/g, '\\') //do not need backslash
}); //escaped for pattern
json = json.replace(RegExp(QUOTE_EMBEDDED ,'g'), '\\"');
json = json.replace(/\\t/g, '\t'); // \\t --> \t
// NAN, UNDEFINED, INFINITY_PLUS, INFINITY_NEG
json = json.replace(/"@@(.*?)@@"/g , '$1');
//json = json.replace(RegExp('"(@@' + NAN + '"','g'), 'NaN');
//----- un-quote Object keys when valid variable name i.e. alphaumeric
// json = EZ.json.unquoteKeys(json);
//if (options.SCRIPT && !options.KEYS)
if (!options.KEYS)
{
json = json.replace(/([{,]\s*)"([\w_]+?)":/gi,
function(all, sep, key)
{
if ('null undefined'.indexOf(key) != -1) return all;
return sep + key + ":"
});
}
return json;
}
//______________________________________________________________________________
/**
* escape embedded newlines, quotes and backslashes from json String so they
* do not get escaped upon return to native JSON.stringify() from jsonReplacer()
*/
function jsonEscape(json, wrap)
{
if (wrap) //wrap with escape codes to discard...
json = PREFIX + json + SUFFIX; //...outer quotes wrapped upon return
if (typeof(json) != 'string')
return json;
json = json.replace(/"/g, QUOTE);
json = json.replace(/\n/g, NEWLINE);
json = json.replace(/\\/g, BACKSLASH);
return json;
}
//______________________________________________________________________________
/**
* JSON replacer: WORKHORSE for extended json suporrted for EZ.json.stringify()
*
* First calls any replacers specified as EZ.json.stringify() replacer arguments
*
* Then process NaN, undefined, multiline String, Array, Function or RegExp
* based on EZ.json.stringify() options.
*/
function jsonReplacer(key, value)
{
var ctx = this; //development convenience
[ctx]
console.log({key:key, value:value, ctx:this})
/*
var replacers = replacerFunctions.slice();
while (replacer.length)
{ //TODO: not tested
value = replacers.shift.call(this, key, value, options.SPACES);
}
*/
//---------------------------------------------------------------------------\\
//----- do EZ.json.stringify() replacements after specified replacer functions -----\\
//-------------------------------------------------------------------------------\\
var idx, isArray, json, padHere, pre, script,
obj = {},
json = '';
switch (getType(value))
{
case 'Null': //------------------------------------
case 'Boolean': // null, boolean -- return value as-is
return value; //------------------------------------
case 'Array': //------------------------------------------------
case 'Object': // Array, Object -- processed by code after switch
break; //------------------------------------------------
//----------------------------------------------
case 'Undefined': // undefined -- return escaped String if enabled
{ //----------------------------------------------
return options.UNDEFINED ? UNDEFINED
: value; //value as-is if not enabled
}
//--------------------------------------------------
case 'Number': // NaN, Infinity -- return escaped String if enabled
{ //---------------------------------------------------
return isNaN(value) && options.NAN ? NAN
: value === Infinity ? (options.INFINITY ? INFINITY_PLUS : null)
: value === -Infinity ? (options.INFINITY ? INFINITY_NEG : null)
: value;
}
//-----------------------------------------------
case 'Element': // html element -- extract or EZ.format.Element()
{ //-----------------------------------------------
if (extract.length)
break;
else if (!EZ.format || true)
{
value = _json_htmlExtract(value);
break;
}
else if (EZ.format)
{
value = EZ.format.Element(value, options.htmlFormatter);
if (typeof(value) != 'string')
break;
}
/* jshint ignore:start*/ //FALL-thru
}
//-----------------------------------------------------
case 'String': // multiline String -- separate lines if SCRIPT enabled
{ //-----------------------------------------------------
/* jshint ignore:end */
value = value.replace(/"/g, QUOTE_EMBEDDED);
if (options.SCRIPT && value.indexOf('\n') != -1)
{ //breakup into multiple lines
padHere = (objectStack.length > 1) ? padding : '\n';
value = QUOTE + padHere + '+ ' + QUOTE
+ value.replace(/\n/g, EOL + QUOTE + padHere + '+ ' + QUOTE)
}
else
value = value.replace(/\n/g, EOL);
value = jsonEscape(value);
return value;
}
//-------------------------------------------
case 'RegExp': // RegExp -- return escaped String if enabled
{ //-------------------------------------------
if (options.REGEXP)
{
value = REGEXP
+ value.source + '/'
+ (value.global ? 'g' : '')
+ (value.ignoreCase ? 'i' : '')
+ (value.multiline ? 'm' : '')
return jsonEscape(value);
}
return value;
}
/* jshint ignore:start*/ //FALL-thru
case 'Function':
/* jshint ignore:end */
default:
{
switch (typeof value)
{ //----------------------------------------
case 'function': // Function constructor or typeof function
{ //----------------------------------------
if (!options.FUNCTIONKEYS && !options.FUNCTIONSCRIPT && !options.FUNCTIONTYPE)
{ //if typeof function not enabled...
return {}; //...return empty Object
}
script = untab(value+'');
if (options.FUNCTIONSCRIPT)
void(0);
else if (options.FUNCTIONTYPE) //empty script if keeping typeof function
script = 'function() {}';
else
script = '';
if (options.SCRIPT) //if SCRIPT format . . .
{
if (options.FUNCTIONKEYS)
{ //copy enumerable properties if any
Object.keys(value).forEach(function(key) {obj[key] = value[key]});
json = EZ.json.stringify.call(EZ.json.stringify, obj, options);
if (json == '{}')
json = '';
}
if (!json)
{ //if no properties...
if (!options.FUNCTIONSCRIPT && !options.FUNCTIONTYPE)
return {}; //...return empty object if NOT keeping script
json = script; //...if keeping, return function declaration json
}
else //if properties, use closure function with bit of
{ //javascript so json parses as typeof function
pre = '(function()\n'
+ '{';
if (script)
script = padding + 'var ____function____ = ' + script + ';';
if (json)
json = 'var ____properties____ = '
+ indent(json) + ';\n'
+ pad + 'for (var key in ____properties____)\n'
+ pad + '{____function____[key] = ____properties____[key];\n'
json += pad + 'return ____function____;\n'
+ '})()'
if (objectStack.length > 1)
{
pre = indent(pre);
json = indent(json);
}
json = pre + script + padding + json;
}
return jsonEscape(json,true);
}
else //otherwise clone as Object to stringify enumerable properties
{
if (script) //if script...
{ //add '____function____' property
key = '____function____'; //for all script from toString()
obj[key] = script.replace(/\n/g, EOL);
}
if (options.FUNCTIONKEYS) //keep enumerable properties if enabled
{
Object.keys(value).forEach(function(key)
{
if (value.hasOwnProperty(key))
obj[key] = value[key];
});
}
value = obj; //set value to cloned Object
break; //then stringify as Object
}
}
//-------------------------------------------
/* jshint ignore:start*/ //FALL-thru
case 'object': // Object not created with Object constructor
/* jshint ignore:end */
{ //-------------------------------------------
//TODO: html object incomplete -- see work in EZ.toString()
// for HTML object, clone as standard Object with all attributes
// and acessible properties if replacer argument does not exclude
if (options.HTML && value.constructor.name.substr(0,4) == 'HTML')
{
[].forEach.call(value.attributes,function(key)
{ //for all attributes . . .
obj.attributes = obj.attributes || {};
obj.attributes[key.name] = key.value;
});
obj.attributes = jsonExtractFilter(obj.attributes);
/* jshint ignore:start*/
if (value.__proto__ != EZ.undefined)
{ //for all acessible properties
value = jsonExtractFilter(value.__proto__);
Object.keys(value).forEach(function(key)
{
var e;
try
{ //TODO: keep if not circular
if (!value[key] instanceof Object)
return;
if (!(key in obj)) //if not already added e.g. attributes
obj[key] = value[key];
}
catch(e)
{
void(0);
}
});
}
/* jshint ignore:end */
value = obj; //stringify cloned obj
}
break; //break to complete object processing
}
//-------------------------------------------
/* jshint ignore:start*/ //FALL-thru
default: // process value as-is for any other types
/* jshint ignore:end */
return value; //-------------------------------------------
}
}
}
//------------------------------------\\
//----- Array or Object processing -----\\
//----------------------------------------\\
isArray = getType(value) == 'Array';
if (value == objectStack[0])
{
if (key === '')
return value;
var repeatIdx = processedObj.indexOf(value);
if (repeatIdx == -1)
return value; //currently processing Object
else
{
return _processCircular(repeatIdx);
}
}
if (!isArray)
{
value = jsonExtractFilter(value, key);
if (value === undefined)
return value;
}
//-----
if (!key)
{
if (this[key] == value)
key = nestedKeys.pop();
else
{
var obj = objectStack[0];
if (!obj)
key = '$';
else
{
objectKeys[0].every(function(p,idx)
{
if (obj[p] != value) return true;
key = p;
objectKeys[0].splice(idx,1);
});
}
}
}
if (this[key] != value)
void(0); //debugger breakpoint
objectStack.unshift(value); //push Object onto process stack
objectKeys.unshift(Object.keys(value));
nestedKeys.push(key);
if (!isArray)
{
json = jsonValue(value);
if (objectStack.length > 2)
json = indent(json);
}
//_______________________________________________________________________________
/**
* return dotName from nestedKeys
*/
function getDotName()
{
return nestedKeys.join('.').replace(/\.\[/g, '[');
}
//_______________________________________________________________________________
/**
* Get json for value if object already processed return the following String:
*
* "{$.results.args[0]=$.results.arguments[0]}"
*
* where:
* $.results.args[0] is dotName of property from root
* $.results..arguments[0] refers to Object already Strinified
*
* The placeholders will be replaced in final json returned by EZ.json.stringify()
* if not circular references such as {obj: {o:obj}}
*
* If SCRIPT option is specified JavaScript similar to the following is used
* when circular references are found:
*
* (function()
* {
* $ = "json ...";
* $.results.args[0]={$.results.arguments[0];
* . . .
* return $;
* })();
*/
function _processCircular(repeatIdx)
{
var repeatKey = processedKey[repeatIdx];
var repeatValue = EZ.isArray(processedObj[repeatIdx]) ? ["_[repeatKey]:" + repeatKey]
: {"_[repeatKey]": repeatKey};
var item = {};
item[getDotName(key)] = repeatKey;
EZ.json.repeatList.push(item);
if (options.CIRCULAR)
{
}
//return jsonEscape(json, true);
return EZ.isArray(value) ? [repeatValue]
: {'_[repeat]': repeatValue};
}
//_______________________________________________________________________________
/**
*
*/
function jsonValue(value, idx)
{
if (idx != null) //Array ??
nestedKeys.push(key);
else if (typeof(value) == 'object')
{
processedObj.push(value);
processedKey.push( getDotName() );
}
var sp = spaces === 0 ? 0 : options.SPACES; //11-01-2016
//var sp = options.SPACES; // ''
//=========================================================================
var json = JSON.stringify.call(EZ.json.stringify, value, jsonReplacer, sp);
//=========================================================================
if (idx != null)
nestedKeys.pop();
if (json !== undefined)
json = json.replace(WRAPPER, '');
//==========
return json;
//==========
}
//-------------------------------\\
//----- create json for Array -----\\
//-----------------------------------\\
if (isArray)
{
var delim = '',
useSepLine,
itemCount = 0,
needIndent = false,
jsonForItem = '',
jsonForLine = '',
jsonForItems = '',
jsonForKeys = '',
properties = null;
// Create new Object() for any non-numeric enumerable Array properties
if (options.ARRAYKEYS)
{
Object.keys(value).forEach(function(key)
{
if (!isNaN(key) || !value.hasOwnProperty(key)) return;
properties = properties || {};
properties[key] = value[key];
});
if (properties && !options.SCRIPT) //named Array properties found AND
{ //not script format, clone Array items
value = value.slice(); //then add pseudo item for properties
value.push({"____properties____": properties});
}
}
//--------------------------
// for each Array item . . .
//--------------------------
for (idx=0; idx<value.length; idx++) //TODO: why not EZ.json.stringify() ??
{ //get deep json for each item
var item = value[idx];
var jsonForItem = jsonValue(item, idx);
if (jsonForItem === undefined)
continue;
if (typeof(item) != 'object' //probably not used
&& jsonForItem.indexOf('{') === 0) //remove spaces and newlines
jsonForItem = jsonForItem.replace(/\s/g, '');
if (idx == (value.length-1) && properties && jsonForItem == '{}')
continue; //no named properties extracted
useSepLine = false;
jsonForLine += delim + jsonForItem;
if (typeof(item) == 'string'
&& jsonForItem.indexOf('"' + QUOTE + NEWLINE) === 0)
{
useSepLine = true; //separate lines for multiline String
json += delim
delim = '';
}
if (item instanceof Object && !/(String|Number|Boolean)/.test(getType(item)))
{
useSepLine = true; //separate line for Object json
if (item.constructor == Array)
jsonForItem = jsonForItem.replace(/\n/g, padding);
}
else if (jsonForItem.length > options.ARRAYMAXLINELENGTH)
{ //TODO: split into multiple lines??
useSepLine = true; //separate line for long String
}
else if (itemCount >= options.ARRAYITEMSPERLINE
|| jsonForItem.length > options.ARRAYMAXLINELENGTH)
{ //start newline after too many items
json += delim.replace(/ /, '\n')
delim = '';
itemCount = 0;
jsonForLine = jsonForItem;
needIndent = true;
}
if (useSepLine)
{ //start newline
json += delim.replace(/ /, '\n') + jsonForItem;
delim = ',\n';
itemCount = 0;
needIndent = true;
jsonForLine = '';
}
else //append to current line
{
json += delim + jsonForItem;
// delim = ', '; //11-01-2016
delim = ',' + (spaces === 0 ? '' : ' ');
itemCount++;
}
}
//-----------------------------------------------------------
// all Array items processed -- indent and/or wrap with [...]
//-----------------------------------------------------------
jsonForItems = json;
padHere = '';
if (needIndent) //indent Array ITEMS
{
json = padding + json.replace(/\n/g, padding);
if (typeof objectStack[1] == 'object' && objectStack.length > 2)
json = indent(json, true);
else if (key)
padHere = padding;
else
padHere = '\n';
}
json = '[' + json + padHere + ']';
//----------------------------------------
// SCRIPT format of named Array properties
//----------------------------------------
while (properties && options.SCRIPT)
{
jsonForKeys = EZ.json.stringify.call(EZ.json.stringify, properties, options);
if (jsonForKeys == '{}') break;
// encapulate Array json in closure function with named property
// json and bit of javascript to restore values when json parsed
json = ''
+ '(function()\n'
+ '{' + padding
+ 'var ____array____ = ['
+ padding
+ indent(pad+jsonForItems)
+ padding
+ '];' + padding
+ 'var ____properties____ = ' + indent(jsonForKeys) + ';\n'
+ pad + 'Object.keys(____properties____).forEach(function(key)\n'
+ pad + '{____array____[key] = ____properties____[key]});\n'
+ pad + 'return ____array____;\n'
+ '})()'
if (objectStack.length > 2)
json = indent(json);
break;
}
}
//-----------------------------------------------------\\
//----- return escaped String as value to stringify -----\\
//---------------------------------------------------------\\
obj = objectStack.shift();
objectKeys.shift();
nestedKeys.pop();
//============================
return jsonEscape(json, true);
//============================
//______________________________________________________________________________
/**
*
*/
function untab(all)
{
return all;
/*TODO:
all = all.replace(/(['"])(.*)\1/gm, function(all)
{
return all.replace(/\t/g, '##tab##')
});
all = all.replace(/\t/gm, ' ');
all = all.replace(/##tab##/gm, '\t');
return all;
*/
}
//______________________________________________________________________________
/**
*
*/
function indent(json, more)
{
var regex = RegExp('(' + NEWLINE + ')', 'g');
json = json.replace(regex, '$1' + padding.substr(1));
json = json.replace(/\n/g, padding) + (more ? padding : '');
return json;
}
//______________________________________________________________________________
/**
* use options.include, options.exclude, options.extract to limit or expand
* Object properties stringified.
*/
function jsonExtractFilter(value)
{
var includeKeys = [];
var excludeKeys = [];
if (extract.length > 0 || exclude.length > 0)
{ //if extracted keys specified
for (var key in value) //for enumerated keys...
{
if (key.substr(0,4) == '____' && key.substr(-4) == '____')
{
if (exclude.indexOf(key) != -1)
excludeKeys.push(key); //...explicit omit
}
else if (include.indexOf(key) != -1)
includeKeys.push(key); //...explicit keep
else if (exclude.indexOf(key) != -1)
excludeKeys.push(key); //...explicit omit
else if (extract.indexOf(key) == -1
&& extract.length > 0)
excludeKeys.push(key); //...implicit omit
}
}
extract.concat(include).forEach(function(key)
{ //for all specified keys...
if (!(key in value) //not enumerable
&& typeof value[idx] != EZ.undefined) //but exists
includeKeys.push(key); //...add
});
if (!includeKeys.length && !excludeKeys.length)
return value; //bail if value good "as is"
var extactKeys = Object.keys(value);
excludeKeys.forEach(function(key)
{ //remove excluded from enumerated
var idxKey = extactKeys.indexOf(key);
if (idxKey != -1 && includeKeys.indexOf(key) == -1)
extactKeys.splice(idxKey,1);
})
var isEmpty = true, clone = {};
extactKeys.concat(includeKeys).forEach(function(key)
{ //clone value from enumerated list
try //plus included keys
{
clone[key] = value[key];
if (key != '____properties____')
isEmpty = false;
}
catch(e)
{
void(0);
}
});
return !isEmpty ? clone //return clone if not empty
: undefined; //otherwise, return undefined
}
}
//______________________________________________________________________________________
/**
* extract html element default properties -- avoids circular
*/
function _json_htmlExtract(value)
{
var extract = 'tagName id name type className'.split(/\s+/);
var json = JSON.stringify(value, extract);
return JSON.parse(json);
}
//______________________________________________________________________________
/**
* setup global varibles including options based on EZ.json.stringify() arguments.
* returns options Object.
*/
function jsonFormatOptions(/* key, value */)
{
options = {};
options.SPACES = (spaces || options.SPACES);
//----------------------------------------------\\
//----- process replacer specified as object -----\\
//--------------------------------------------------\\
if (getType(replacer) == 'Object')
{
var optionsString = '';
Object.keys(replacer).forEach(function(key)
{ //add directly to options
if (/(include|exclude|extract)/.test(key))
options[key] = EZ.toArray(replacer[key], ', ');
else if (!replacer[key]) //add key to string
optionsString += ' *' + key;
else //add key=value to string
optionsString += ' *' + key + '=' + replacer[key];
});
replacer = optionsString.trim();
}
//----------------------------------------------------------------\\
//----- process replacer argument as String, Array or Function -----\\
//--------------------------------------------------------------------\\
switch (getType(replacer))
{
case 'Function':
replacerFunctions = [replacer];
/* jshint ignore:start*/ //FALL-thru
case 'String': //convert comma/space delimited String to Array
/* jshint ignore:end */
replacer = replacer.split(/\s*[, ]\s*/);
/* jshint ignore:start*/ //FALL-thru
case 'Array': //process extract properties and EZ options
/* jshint ignore:end */
{ //for each Array item . . .
replacer.forEach(function(item)
{
if (typeof(item) == 'function')
replacerFunctions.push(item); //replacer function
else if (typeof(item) != 'string' || !item)
return;
//extract property or EZ option
var results = item.match(/([+-]?)(\*?)(\w*)=?(\w*)/);
if (!results) return;
var pre = results[1] || '';
var star = results[2] || '';
var key = results[3] || 'SCRIPT'; //blank key
var text = results[4] || '';
var number = results[4] || 0;
if (star) //----- EZ options start with * -----\\
{
key = key.toUpperCase();
var defaultValue = EZ.json.options[key];
options[key] = typeof(defaultValue) == 'boolean' ? pre != '-'
: typeof(defaultValue) == 'number' ? Number(number)
: typeof(defaultValue) == 'string' ? text
: '';
}
//----- extract property key name -----\\
else if (pre == '+')
{
include = include || [];
include.push(key); //...explicit include if startes with "+"
}
else if (pre == '-')
{
exclude = exclude || [];
exclude.push(key); //...explicit exclude if starts with "-"
}
else if (pre === '')
{
extract = extract || [];
extract.push(key); //...add to extract list if no +/- prefix
}
});
}
}
options.SPACES = (spaces || EZ.json.options.SPACES); //non-zero non-blank required
if (!isNaN(options.SPACES) && options.SPACES <= 0)
options.SPACES = EZ.json.options.SPACES
EZ.json.stringify.options = 'format options:';
options.SCRIPT = options.SCRIPT || false;
Object.keys(EZ.json.options).forEach(function(key)
{
if (!(key in options)) //set default for any non-specified options
{
var defVal = EZ.json.options[key]; //for boolean, true if option can be implicitly
if (typeof(defVal) == 'boolean') //... enabled -and- ALL or SCRIPT is true
defVal = defVal && (options.ALL || options.SCRIPT)
else if (typeof(defVal) == 'number' && options.ALL === false)
defVal = 0; //for numeric, 0 if ALL explictly set false
options[key] = defVal;
}
if (options[key] || options[key] === 0)
EZ.json.stringify.options += ' ' + key + '=' + options[key];
});
}
//______________________________________________________________________________
/**
* return type as constructor: Undefined, Null for undefined or null respectively
* else Array, Boolean, Function, Number, Object, RegExp
*
*/
function getType(value)
{
if (value instanceof Element)
return 'Element';
var type = Object.prototype.toString.call(value);
return type.substring(8,type.length-1);
}
}
/*--------------------------------------------------------------------------------------------------
EZ.json.unquoteKeys(json)
Uses RegExp to find quoted keys ---
As safety, returns json unchanged if initial -or- converted json does not eval() without exceptions.
ARGUMENTS:
json String containg json returned with Object keys unquoted if valid variable name.
i.e. not reserved word, no spaces and startsi with "_" or alphabetic character
RETURNS:
json with unquoted key -- valid JavaScript / eval() but not recognized by JSON.parse()
if (regex.reserved.includes(arg)) //reserved word
--------------------------------------------------------------------------------------------------*/
EZ.json.unquoteKeys = function unquoteKeys(json)
{
if (!json || typeof(json) != 'string') return json;
var options = {json: json};
// if (EZ.capture.check(this,options)) {return EZ.capture()} else if (EZ.test.debug()) debugger;
var phase = 'validating input';
try
{
var value = eval('value='+json);
var unquoted = json.replace(/([{,]\s*)"([\w_]+?)":/gi, function(all, sep, key)
{
if (EZ.json.INVALID_KEYS.indexOf(key) != -1) return all;
return sep + key + ":";
});
phase = 'validating unquoted';
var val = eval('val=' + unquoted);
if (EZ.equals(val, value))
json = unquoted;
else
throw new EZ.error('unable to unquote keys');
}
catch (e)
{
EZ.json.fault = EZ.techSupport(e, '-' + phase, this, options);
}
return json;
}
/*---------------------------------------------------------------------------------------------
EZ.json.parse(value)
Extend navive JSON.parse to support extended json created by EZ.json.stringify()
ARGUMENTS:
value (String) json parsed to create Array(s), Object(s), RegExp(s) or Function(s)
(Object) create
---------------------------------------------------------------------------------------------*/
EZ.parse = function EZjson_parse(value)
{
var json, obj, pattern;
//----- if called with OBJECT (not String) . . .
// create named Array properties if any stored as last item of Array
// convert any pseudo function Objects to typeof 'function' Objects
if (value instanceof Object)
{
// use EZ.json.stringify() to create pseudo json then parse via eval()
json = EZ.json.stringify(value,'*script');
if (json != EZ.undefined) value = json;
return value; //return converted Object
}
//----- return value "as-is" if not String