-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathx15.12.html
1021 lines (1018 loc) · 35.8 KB
/
x15.12.html
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
<!DOCTYPE html>
<html class="split chapter"><head><meta charset="utf-8"><title>15.12 The JSON Object # Ⓣ Ⓔ ① Ⓐ — Annotated ES5</title><link rel="stylesheet" href="style.css"><link href="x15.11.html" title="15.11 Error Objects " rel="prev">
<link href="spec.html" title="TOC" rel="index">
<link href="x16.html" title="16 Errors " rel="next">
</head><body><div class="head">
<h2 id="top">Annotated ECMAScript 5.1 <span id="timestamp"></span></h2>
<div id="mascot-treehouse">
<img id="mascot" align="left" src="js-mascot.svg" alt=""><img id="bubble" src="bubble.svg" alt=""></div>
<p id="slogan">‟Ex igne vita”</p>
<div id="annotations"></div>
<script src="timestamp.js"></script></div>
<nav>
<a href="x15.11.html">← 15.11 Error Objects </a> –
<a href="spec.html" class="toc-nav">TOC</a> –
<a href="x16.html">16 Errors →</a>
<ol class="toc"><li><ol><li><a href="x15.12.html#x15.12" id="x15.12-toc">15.12 The JSON Object</a>
<b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b> <ol><li><a href="x15.12.html#x15.12.1" id="x15.12.1-toc">15.12.1 The JSON Grammar </a>
<b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b> <ol><li><a href="x15.12.html#x15.12.1.1" id="x15.12.1.1-toc">15.12.1.1 The JSON Lexical Grammar</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b> </li><li><a href="x15.12.html#x15.12.1.2" id="x15.12.1.2-toc">15.12.1.2 The JSON Syntactic Grammar</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b> </li></ol></li><li><a href="x15.12.html#x15.12.2" id="x15.12.2-toc">15.12.2 parse ( text [ , reviver ] )</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b> </li><li><a href="x15.12.html#x15.12.3" id="x15.12.3-toc">15.12.3 stringify ( value [ , replacer [ , space ] ] )</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b> </li></ol></li></ol></li></ol></nav>
<h3 id="x15.12" class="splitme">15.12 The JSON Object <a href="#x15.12">#</a> <a href="#x15.12-toc" class="bak">Ⓣ</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b></h3>
<p>
The
<b>JSON</b> object is a single object that contains two functions,
<b>parse </b>and <b>stringify</b>, that are used to parse and
construct JSON texts. The JSON Data Interchange Format is described
in RFC 4627 <<a href="http://www.ietf.org/rfc/rfc4627.txt">http://www.ietf.org/rfc/rfc4627.txt</a>>. The JSON
interchange format used in this specification is exactly that
described by RFC 4627 with two exceptions:</p>
<ul><li><p>
The
top level <i>JSONText</i>
production of the ECMAScript JSON grammar may consist of any
<i>JSONValue</i> rather
than being restricted to being a <i>JSONObject</i>
or a <i>JSONArray</i> as
specified by RFC 4627.
</p>
</li>
<li><p>
Conforming
implementations of <b>JSON.parse</b> and <b>JSON.stringify</b> must
support the exact interchange format described in this
specification without any deletions or extensions to the format.
This differs from RFC 4627 which permits a JSON parser to accept
non-JSON forms and extensions.</p>
</li></ul><p>
The
value of the [[Prototype]] internal property of the JSON object is
the standard built-in Object prototype object (<a href="x15.2.html#x15.2.4">15.2.4</a>). The value of
the [[Class]] internal property of the JSON object is <code><b>"JSON"</b></code>.
The value of the [[Extensible]] internal property of the JSON object
is set to <b>true</b>.</p>
<p>
The
JSON object does not have a [[Construct]] internal property; it is
not possible to use the JSON object as a constructor with the <code><b>new</b></code>
operator.</p>
<p>
The
JSON object does not have a [[Call]] internal property; it is not
possible to invoke the JSON object as a function.</p>
<h4 id="x15.12.1">15.12.1 The JSON Grammar <a href="#x15.12.1">#</a> <a href="#x15.12.1-toc" class="bak">Ⓣ</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b></h4>
<p>
JSON.stringify
produces a String that conforms to the following JSON grammar.
JSON.parse accepts a String that conforms to the JSON grammar.</p>
<h5 id="x15.12.1.1">15.12.1.1 The JSON Lexical Grammar <a href="#x15.12.1.1">#</a> <a href="#x15.12.1.1-toc" class="bak">Ⓣ</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b></h5>
<p>
JSON
is similar to ECMAScript source text in that it consists of a
sequence of characters conforming to the rules of <i>SourceCharacter</i>.
The JSON Lexical Grammar defines the tokens that make up a JSON text
similar to the manner that the ECMAScript lexical grammar defines
the tokens of an ECMAScript source test. The JSON Lexical grammar
only recognizes the white space character specified by the
production <i>JSONWhiteSpace</i>.
The JSON lexical grammar shares some productions with the
ECMAScript lexical grammar. All nonterminal symbols of the grammar
that do not begin with the characters “JSON” are defined by
productions of the ECMAScript lexical grammar.</p>
<p class="tiny-btm">
<b>Syntax</b></p>
<p class="keep">
<i>JSONWhiteSpace </i><b>::</b></p>
<p class="def1">
<TAB><br><CR><br><LF><br><SP></p>
<p class="keep">
<i>JSONString </i><b>::</b></p>
<p class="def1">
<code><b>"</b></code> <i>JSONStringCharacters</i><sub>opt</sub><code><b>"</b></code></p>
<p class="keep">
<i>JSONStringCharacters </i><b>::</b></p>
<p class="def1">
<i>JSONStringCharacter
JSONStringCharacters</i><sub>opt</sub></p>
<p>
<i>JSONStringCharacter </i><b>::</b></p>
<p class="def1">
<i>SourceCharacter </i><b>but
not</b><i> double-quote </i><code><b>"</b></code> <b>or</b><i>backslash </i><code><b>\
</b></code><b>or</b><i>U+0000 thru U+001F</i></p>
<p class="def1">
<code><b>\</b></code> <i>JSONEscapeSequence</i></p>
<p class="keep">
<i>JSONEscapeSequence </i><b>::</b></p>
<p class="def1">
<i>JSONEscapeCharacter</i></p>
<p class="def1">
<i>UnicodeEscapeSequence </i>
</p>
<p class="keep">
<i>JSONEscapeCharacter </i><b>::</b> <b>one
of</b></p>
<p class="def1">
<code><b>"
/ \ b f n r t</b></code></p>
<p class="keep">
<i>JSONNumber </i><b>::</b></p>
<p class="def1">
<code><b>-</b></code><sub>opt</sub><i>DecimalIntegerLiteral JSONFraction</i><sub>opt</sub><i>ExponentPart</i><sub>opt</sub></p>
<p class="keep">
<i>JSONFraction </i><b>::</b></p>
<p class="def1">
<code><b>.</b></code> <i>DecimalDigits</i></p>
<p class="keep">
<i>JSONNullLiteral </i><b>::</b></p>
<p class="def1">
<i>NullLiteral</i></p>
<p class="keep">
<i>JSONBooleanLiteral </i><b>::</b></p>
<p class="def1">
<i>BooleanLiteral</i></p>
<h5 id="x15.12.1.2">15.12.1.2 The JSON Syntactic Grammar <a href="#x15.12.1.2">#</a> <a href="#x15.12.1.2-toc" class="bak">Ⓣ</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b></h5>
<p>
The
JSON Syntactic Grammar defines a valid JSON text in terms of tokens
defined by the JSON lexical grammar. The goal symbol of the grammar
is <i>JSONText</i>.
</p>
<p class="tiny-btm">
<b>Syntax</b></p>
<p class="keep">
<i>JSONText </i><b>:</b><i> </i>
</p>
<p class="def1">
<i>JSONValue</i></p>
<p class="keep">
<i>JSONValue </i><b>:</b><i> </i>
</p>
<p class="def1">
<i>JSONNullLiteral<br>JSONBooleanLiteral<br>JSONObject<br>JSONArray<br>JSONString<br>JSONNumber</i></p>
<p class="keep">
<i>JSONObject </i><b>:</b></p>
<p class="def1">
<code><b>{
}</b></code> <i><br></i><code><b>{</b></code> <i>JSONMemberList </i><code><b>}</b></code></p>
<p class="keep">
<i>JSONMember </i><b>:</b></p>
<p class="def1">
<i>JSONString </i><code><b>:</b></code> <i>JSONValue</i></p>
<p class="keep">
<i>JSONMemberList </i><b>:</b></p>
<p class="def1">
<i>JSONMember
<br>JSONMemberList </i><code><b>,</b></code> <i><b>
</b>JSONMember </i>
</p>
<p class="keep">
<i>JSONArray </i><b>:</b></p>
<p class="def1">
<code><b>[
]</b></code> <i><br></i><code><b>[</b></code> <i>JSONElementList</i><code><b>
]</b></code></p>
<p class="keep">
<i>JSONElementList </i><b>:</b></p>
<p class="def1">
<i>JSONValue<br>JSONElementList</i><code><b>
,</b></code> <i> JSONValue</i></p>
<h4 id="x15.12.2">15.12.2 parse ( text [ , reviver ] ) <a href="#x15.12.2">#</a> <a href="#x15.12.2-toc" class="bak">Ⓣ</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b></h4>
<p>
The
<code><b>parse</b></code> function
parses a JSON text (a JSON-formatted String) and produces an
ECMAScript value. The JSON format is a restricted form of ECMAScript
literal. JSON objects are realized as ECMAScript objects. JSON
arrays are realized as ECMAScript arrays. JSON strings, numbers,
booleans, and null are realized as ECMAScript Strings, Numbers,
Booleans, and <b>null</b>. JSON uses a more limited set of white
space characters than <i><a href="x7.html#WhiteSpace">WhiteSpace</a></i>
and allows Unicode code points U+2028 and U+2029 to directly appear
in <i>JSONString</i>
literals without using an escape sequence. The process of parsing is
similar to <a href="x11.html#x11.1.4">11.1.4</a> and <a href="x11.html#x11.1.5">11.1.5</a> as constrained by the JSON grammar.</p>
<p>
The
optional <i>reviver</i> parameter is a function that takes two
parameters, (<i>key</i> and <i>value</i>). It can filter and
transform the results. It is called with each of the <i>key</i>/<i>value</i>
pairs produced by the parse, and its return value is used instead of
the original value. If it returns what it received, the structure is
not modified. If it returns <b>undefined</b> then the property is
deleted from the result.</p>
<ol><li><p>
Let
<i>JText</i> be <a href="x9.html#x9.8">ToString</a>(<i>text</i>).</p>
</li>
<li><p>
Parse
<i>JText</i> using the grammars in <a href="#x15.12.1">15.12.1</a>. Throw a <b><a href="x15.11.html#x15.11.6.4" class="term-ref">SyntaxError</a></b>
exception if <i>JText</i> did not conform to the JSON grammar for
the goal symbol <i>JSONText</i>.
</p>
</li>
<li><p>
Let
<i>unfiltered</i> be the result of parsing and evaluating <i>JText</i>
as if it was the source text of an ECMAScript <i><a href="x14.html#x14">Program</a></i> but
using<i> JSONString </i>in place<i> </i>of<i> <a href="x7.html#x7.8.4">StringLiteral</a></i>.
Note that since <i>JText</i> conforms to the JSON grammar this
result will be either a <a href="x4.html#primitive_value" class="term-ref">primitive value</a> or an object that is
defined by either an <i>ArrayLiteral</i> or an <i>ObjectLiteral</i>.</p>
</li>
<li><p>
If
<a href="x9.html#x9.11">IsCallable</a>(<i>reviver</i>) is <b>true</b>, then</p>
<ol><li><p>
Let
<i>root</i> be a new object created as if by the expression <code><b>new
Object()</b></code>, where <code><b>Object</b></code>
is the standard built-in constructor with that name.</p>
</li>
<li><p>
Call
the [[DefineOwnProperty]] internal method of <i>root</i> with the
empty String, the PropertyDescriptor {[[Value]]: <i>unfiltered</i>,
[[Writable]]: <b>true</b>, [[Enumerable]]: <b>true</b>,
[[Configurable]]: <b>true</b>}, and <b>false</b> as arguments.</p>
</li>
<li><p>
Return
the result of calling the abstract operation <a href="#Walk">Walk</a>, passing <i>root</i>
and the empty String. The abstract operation <a href="#Walk">Walk</a> is described
below.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Return
<i>unfiltered</i>.</p>
</li></ol></li></ol><p>
The
abstract operation <dfn id="Walk">Walk</dfn> is a recursive abstract operation that takes
two parameters: a <i>holder</i>
object and the String <i>name</i>
of a property in that object. Walk uses the value of <i>reviver</i>
that was originally passed to the above parse function.</p>
<ol><li><p>
Let
<i>val</i> be the result of calling the [[Get]] internal method of
<i>holder</i> with argument <i>name</i>.</p>
</li>
<li><p>
If
<i>val</i> is an object, then</p>
<ol><li><p>
If
the [[Class]] internal property of <i>val</i> is <code><b>"Array"</b></code></p>
<ol><li><p>
Set
<i>I</i> to 0.</p>
</li>
<li><p>
Let
<i>len</i> be the result of calling the [[Get]] internal method
of <i>val</i> with argument <code><b>"length"</b></code>.</p>
</li>
<li><p>
Repeat
while <i>I </i>< <i>len</i>,
</p>
<ol><li><p>
Let
<i>newElement</i> be the result of calling the abstract
operation <a href="#Walk">Walk</a>, passing <i>val</i> and <a href="x9.html#x9.8">ToString</a>(<i>I</i>).</p>
</li>
<li><p>
If
<i>newElement</i> is <b>undefined</b>, then</p>
<ol><li><p>
Call
the [[Delete]] internal method of <i>val</i> with <a href="x9.html#x9.8">ToString</a>(<i>I</i>)
and <b>false</b> as arguments.
</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Call
the [[DefineOwnProperty]] internal method of <i>val</i> with
arguments <a href="x9.html#x9.8">ToString</a>(<i>I</i>), the <a href="x8.html#x8.10">Property Descriptor</a>
{[[Value]]: <i>newElement</i>, [[Writable]]: true,
[[Enumerable]]: true, [[Configurable]]: true}, and <b>false</b>.</p>
</li></ol></li>
<li><p>
Add
1 to <i>I</i>.</p>
</li></ol></li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Let
<i>keys</i> be an internal <a href="x8.html#x8.8">List</a> of String values consisting of
the names of all the own properties of <i>val</i> whose
[[Enumerable]] attribute is <b>true</b>. The ordering of the
Strings should be the same as that used by the
<b><a href="x15.2.html#x15.2.3.14">Object.keys</a></b>
standard built-in function.</p>
</li>
<li><p>
For
each String <i>P</i> in <i>keys</i> do,
</p>
<ol><li><p>
Let
<i>newElement</i> be the result of calling the abstract
operation <a href="#Walk">Walk</a>, passing <i>val</i> and <i>P</i>.
</p>
</li>
<li><p>
If
<i>newElement</i> is <b>undefined</b>, then</p>
<ol><li><p>
Call
the [[Delete]] internal method of <i>val</i> with <i>P</i> and
<b>false</b> as arguments.
</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Call
the [[DefineOwnProperty]] internal method of <i>val</i> with
arguments <i>P</i>, the <a href="x8.html#x8.10">Property Descriptor</a> {[[Value]]:
<i>newElement</i>, [[Writable]]: <b>true</b>, [[Enumerable]]:
<b>true</b>, [[Configurable]]: <b>true</b>}, and <b>false</b>.</p>
</li></ol></li></ol></li></ol></li></ol></li>
<li><p>
Return
the result of calling the [[Call]] internal method of <i>reviver</i>
passing <i>holder</i> as the <b>this</b> value and with an argument
list consisting of <i>name </i>and <i>val</i>.</p>
</li></ol><p>
It is
not permitted for a conforming implementation of <code><b>JSON.parse</b></code>
to extend the JSON grammars. If an implementation wishes to support
a modified or extended JSON interchange format it must do so by
defining a different parse function.
</p>
<p><b class="note">NOTE</b> In
the case where there are duplicate name Strings within an object,
lexically preceding values for the same key shall be overwritten.</p>
<h4 id="x15.12.3">15.12.3 stringify ( value [ , replacer [ , space ] ] ) <a href="#x15.12.3">#</a> <a href="#x15.12.3-toc" class="bak">Ⓣ</a> <b class="erra">Ⓔ</b> <b class="rev1">①</b> <b class="anno">Ⓐ</b></h4>
<p>
The
<code><b>stringify</b></code> function
returns a String in JSON format representing an ECMAScript value. It
can take three parameters. The first parameter is required. The
<i>value</i> parameter is
an ECMAScript value, which is usually an object or array, although
it can also be a String, Boolean, Number or <b>null</b>. The
optional <i>replacer</i>
parameter is either a function that alters the way objects and
arrays are stringified, or an array of Strings and Numbers that acts
as a white list for selecting the object properties that will be
stringified. The optional <i>space</i>
parameter is a String or Number that allows the result to have white
space injected into it to improve human readability.</p>
<p>
These
are the steps in stringifying an object:</p>
<ol><li><p>
Let
<i>stack</i> be an empty <a href="x8.html#x8.8">List</a>.</p>
</li>
<li><p>
Let
<i>indent</i> be the empty String.</p>
</li>
<li><p>
Let
<i>PropertyList</i> and <i>ReplacerFunction</i> be <b>undefined</b>.</p>
</li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>replacer</i>) is Object, then</p>
<ol><li><p>
If
<a href="x9.html#x9.11">IsCallable</a>(<i>replacer</i>) is <b>true</b>, then</p>
<ol><li><p>
Let
<i>ReplacerFunction </i>be<i> replacer</i>.</p>
</li></ol></li>
<li><p>
Else
if the [[Class]] internal property of <i>replacer</i> is <code><b>"Array"</b></code>,
then</p>
<ol><li><p>
Let
<i>PropertyList </i> be an empty internal <a href="x8.html#x8.8">List</a></p>
</li>
<li><p>
For
each value <i>v</i> of a property of <i>replacer</i> that has an
array index property name. The properties are enumerated in the
ascending array index order of their names.</p>
<ol><li><p>
Let
<i>item</i> be <b>undefined</b>.</p>
</li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>v</i>) is String then let <i>item</i> be <i>v.</i></p>
</li>
<li><p>
Else
if <a href="x8.html#Type">Type</a>(<i>v</i>) is Number then let <i>item</i> be <a href="x9.html#x9.8">ToString</a>(<i>v</i>).</p>
</li>
<li><p>
Else
if <a href="x8.html#Type">Type</a>(<i>v</i>) is Object then,</p>
<ol><li><p>
If
the [[Class]] internal property of <i>v</i> is <code><b>"String"</b></code>
or <code><b>"Number"
</b></code>then let <i>item</i> be <a href="x9.html#x9.8">ToString</a>(<i>v</i>).</p>
</li></ol></li>
<li><p>
If
<i>item</i> is not undefined and <i>item</i> is not currently an
element of <i>PropertyList</i> then,</p>
<ol><li><p>
Append
<i>item</i> to the end of <i>PropertyList</i>.</p>
</li></ol></li></ol></li></ol></li></ol></li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>space</i>) is Object then,</p>
<ol><li><p>
If
the [[Class]] internal property of <i>space</i> is <code><b>"Number"</b></code>
then,</p>
<ol><li><p>
Let
<i>space</i> be <a href="x9.html#x9.3">ToNumber</a>(<i>space</i>).</p>
</li></ol></li>
<li><p>
Else
if the [[Class]] internal property of <i>space</i> is <code><b>"String"</b></code>
then,</p>
<ol><li><p>
Let
<i>space</i> be <a href="x9.html#x9.8">ToString</a>(<i>space</i>).</p>
</li></ol></li></ol></li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>space</i>) is Number</p>
<ol><li><p>
Let
<i>space</i> be min(10, <a href="x9.html#x9.4">ToInteger</a>(<i>space</i>)).</p>
</li>
<li><p>
Set
<i>gap</i> to a String containing <i>space</i> space characters.
This will be the empty String if <i>space</i> is less than 1.</p>
</li></ol></li>
<li><p>
Else
if <a href="x8.html#Type">Type</a>(<i>space)</i> is String</p>
<ol><li><p>
If
the number of characters in <i>space</i> is 10 or less, set <i>gap</i>
to <i>space</i> otherwise set <i>gap</i> to a String consisting of
the first 10 characters of <i>space</i>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Set
<i>gap</i> to the empty String.</p>
</li></ol></li>
<li><p>
Let
<i>wrapper</i> be a new object created as if by the expression <code><b>new
Object()</b></code>, where <code><b>Object</b></code>
is the standard built-in constructor with that name.</p>
</li>
<li><p>
Call
the [[DefineOwnProperty]] internal method of <i>wrapper</i> with
arguments the empty String, the <a href="x8.html#x8.10">Property Descriptor</a> {[[Value]]:
<i>value</i>, [[Writable]]: <b>true</b>, [[Enumerable]]: <b>true</b>,
[[Configurable]]: <b>true</b>}, and <b>false</b>.</p>
</li>
<li><p>
Return
the result of calling the abstract operation <a href="#Str"><i>Str</i></a> with the
empty String and <i>wrapper</i>.</p>
</li></ol><p>
The
abstract operation <dfn id="Str"><i>Str</i>(<i>key</i>,
<i>holder</i>)</dfn>
has access to <i>ReplacerFunction </i>from the invocation of the <code><b>stringify</b></code>
method. Its algorithm is as follows:</p>
<ol><li><p>
Let
<i>value</i> be the result of calling the [[Get]] internal method
of <i>holder</i> with argument <i>key</i>.</p>
</li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>value</i>) is Object, then</p>
<ol><li><p>
Let
<i>toJSON</i> be the result of calling the [[Get]] internal method
of <i>value</i> with argument <code><b>"toJSON"</b></code>.</p>
</li>
<li><p>
If
<a href="x9.html#x9.11">IsCallable</a>(<i>toJSON</i>) is <b>true</b></p>
<ol><li><p>
Let
<i>value</i> be the result of calling the [[Call]] internal
method of <i>toJSON </i>passing <i>value</i> as the <b>this</b>
value and with an argument list consisting of <i>key</i>.</p>
</li></ol></li></ol></li>
<li><p>
If
<i>ReplacerFunction</i> is not <b>undefined</b>, then</p>
<ol><li><p>
Let
<i>value</i> be the result of calling the [[Call]] internal method
of <i>ReplacerFunction </i>passing <i>holder</i> as the <b>this</b>
value and with an argument list consisting of <i>key</i> and
<i>value</i>.</p>
</li></ol></li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>value</i>) is Object then,</p>
<ol><li><p>
If
the [[Class]] internal property of <i>value</i> is <code><b>"Number"</b></code>
then,</p>
<ol><li><p>
Let
<i>value</i> be <a href="x9.html#x9.3">ToNumber</a>(<i>value</i>).</p>
</li></ol></li>
<li><p>
Else
if the [[Class]] internal property of <i>value</i> is <code><b>"String"</b></code>
then,</p>
<ol><li><p>
Let
<i>value</i> be <a href="x9.html#x9.8">ToString</a>(<i>value</i>).</p>
</li></ol></li>
<li><p>
Else
if the [[Class]] internal property of <i>value</i> is <code><b>"Boolean"</b></code>
then,</p>
<ol><li><p>
Let
<i>value</i> be the value of the [[PrimitiveValue]] internal
property of <i>value</i>.</p>
</li></ol></li></ol></li>
<li><p>
If
<i>value</i> is <b>null</b> then return <code><b>"null"</b></code>.</p>
</li>
<li><p>
If
<i>value</i> is <b>true</b> then return <code><b>"true"</b></code>.</p>
</li>
<li><p>
If
<i>value</i> is <b>false</b> then return <code><b>"false"</b></code>.</p>
</li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>value</i>) is String, then return the result of calling the
abstract operation <i><a href="#Quote">Quote</a></i> with argument <i>value</i>.</p>
</li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>value</i>) is Number</p>
<ol><li><p>
If
<i>value </i>is finite then return <a href="x9.html#x9.8">ToString</a>(<i>value</i>).</p>
</li>
<li><p>
Else,
return <code><b>"null"</b></code>.</p>
</li></ol></li>
<li><p>
If
<a href="x8.html#Type">Type</a>(<i>value</i>) is Object, and <a href="x9.html#x9.11">IsCallable</a>(<i>value</i>) is <b>false</b></p>
<ol><li><p>
If
the [[Class]] internal property of <i>value</i> is <code><b>"Array"
</b></code>then</p>
<ol><li><p>
Return
the result of calling the abstract operation <i><a href="#JA">JA</a></i> with
argument <i>value</i>.</p>
</li></ol></li>
<li><p>
Else,
return the result of calling the abstract operation <i><a href="#JO">JO</a></i> with
argument <i>value</i>.</p>
</li></ol></li>
<li><p>
Return
<b>undefined</b>.</p>
</li></ol><p>
The
abstract operation <dfn id="Quote"><i>Quote</i>(<i>value</i>)</dfn>
wraps a String value in double quotes and escapes characters within
it.
</p>
<ol><li><p>
Let
<i>product</i> be the double quote character.</p>
</li>
<li><p>
For
each character <i>C</i> in <i>value</i></p>
<ol><li><p>
If
<i>C</i> is the double quote character or the backslash character</p>
<ol><li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and the
backslash character.</p>
</li>
<li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and <i>C</i>.</p>
</li></ol></li>
<li><p>
Else
if <i>C</i> is backspace, formfeed, newline, carriage return, or
tab</p>
<ol><li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and the
backslash character.</p>
</li>
<li><p>
Let
<i>abbrev</i> be the character corresponding to the value of <i>C</i>
as follows:</p>
<p class="pre">backspace <code><b>"b"</b></code></p>
<p class="pre">formfeed <code><b>"f"</b></code></p>
<p class="pre">newline <code><b>"n"</b></code></p>
<p class="pre">carriage return <code><b>"r"</b></code></p>
<p class="pre">tab <code><b>"t"</b></code></p>
</li>
<li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and <i>abbrev</i>.</p>
</li></ol></li>
<li><p>
Else
if <i>C</i> is a control character having a code unit value less
than the space character</p>
<ol><li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and the
backslash character.</p>
</li>
<li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and <code><b>"u"</b></code>.</p>
</li>
<li><p>
Let
<i>hex</i> be the result of converting the numeric code unit
value of <i>C</i> to a String of four hexadecimal digits.</p>
</li>
<li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and <i>hex</i>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and <i>C</i>.</p>
</li></ol></li></ol></li>
<li><p>
Let
<i>product</i> be the concatenation of <i>product</i> and the
double quote character.</p>
</li>
<li><p>
Return
<i>product</i>.</p>
</li></ol><p>
The
abstract operation <dfn id="JO"><i>JO</i>(<i>value</i>)</dfn>
serializes an object. It has access to the <i>stack</i>,
<i>indent</i>, <i>gap</i>,
<i>PropertyList</i>,
<i>ReplacerFunction</i>,
and <i>space</i> of the
invocation of the stringify method.</p>
<ol><li><p>
If
<i>stack</i> contains <i>value</i> then throw a <b><a href="x15.11.html#x15.11.6.5" class="term-ref">TypeError</a></b>
exception because the structure is cyclical.</p>
</li>
<li><p>
Append
<i>value</i> to <i>stack</i>.</p>
</li>
<li><p>
Let
<i>stepback</i> be <i>indent</i>.</p>
</li>
<li><p>
Let
<i>indent</i> be the concatenation of <i>indent</i> and <i>gap</i>.</p>
</li>
<li><p>
If
<i>PropertyList </i>is not <b>undefined</b>, then</p>
<ol><li><p>
Let
<i>K</i> be <i>PropertyList</i>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Let
<i>K</i> be an internal <a href="x8.html#x8.8">List</a> of Strings consisting of the names of
all the own properties of <i>value</i> whose [[Enumerable]]
attribute is <b>true</b>. The ordering of the Strings should be
the same as that used by the <b><a href="x15.2.html#x15.2.3.14">Object.keys</a></b> standard built-in
function.</p>
</li></ol></li>
<li><p>
Let
<i>partial</i> be an empty <a href="x8.html#x8.8">List</a>.</p>
</li>
<li><p>
For
each element <i>P</i> of <i>K</i>.</p>
<ol><li><p>
Let
<i>strP</i> be the result of calling the abstract operation <i>Str</i>
with arguments <i>P</i> and <i>value</i>.</p>
</li>
<li><p>
If
<i>strP</i> is not <b>undefined</b></p>
<ol><li><p>
Let
<i>member</i> be the result of calling the abstract operation
<i><a href="#Quote">Quote</a></i> with argument <i>P</i>.</p>
</li>
<li><p>
Let
<i>member</i> be the concatenation of <i>member</i> and the colon
character.</p>
</li>
<li><p>
If
<i>gap</i> is not the empty String</p>
<ol><li><p>
Let
<i>member</i> be the concatenation of <i>member</i> and the
<i>space</i> character.</p>
</li></ol></li>
<li><p>
Let
<i>member</i> be the concatenation of <i>member</i> and <i>strP</i>.</p>
</li>
<li><p>
Append
<i>member</i> to <i>partial</i>.</p>
</li></ol></li></ol></li>
<li><p>
If
<i>partial</i> is empty, then</p>
<ol><li><p>
Let
<i>final</i> be <code><b>"{}"</b></code>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
If
<i>gap</i> is the empty String</p>
<ol><li><p>
Let
<i>properties</i> be a String formed by concatenating all the
element Strings of <i>partial</i> with each adjacent pair of
Strings separated with the comma character. A comma is not
inserted either before the first String or after the last String.
</p>
</li>
<li><p>
Let
<i>final</i> be the result of concatenating <code><b>"{"</b></code><b>,</b>
<i>properties</i>, and <code><b>"}"</b></code>.</p>
</li></ol></li>
<li><p>
Else
<i>gap</i> is not the empty String</p>
<ol><li><p>
Let
<i>separator</i> be the result of concatenating the comma
character, the line feed character, and <i>indent</i>.</p>
</li>
<li><p>
Let
<i>properties</i> be a String formed by concatenating all the
element Strings of <i>partial</i> with each adjacent pair of
Strings separated with <i>separator</i>. The <i>separator</i>
String is not inserted either before the first String or after
the last String.</p>
</li>
<li><p>
Let
<i>final</i> be the result of concatenating <code><b>"{"</b></code>,
the line feed character, <i>indent</i>, <i>properties</i>, the
line feed character, <i>stepback</i>, and <code><b>"}</b></code>".</p>
</li></ol></li></ol></li>
<li><p>
Remove
the last element of <i>stack</i>.</p>
</li>
<li><p>
Let
<i>indent</i> be <i>stepback</i>.</p>
</li>
<li><p>
Return
<i>final</i>.</p>
</li></ol><p>
The
abstract operation <dfn id="JA"><i>JA</i>(<i>value</i>)</dfn>
serializes an array. It has access to the <i>stack</i>,
<i>indent</i>, <i>gap</i>,
and <i>space</i> of the
invocation of the stringify method. The representation of arrays
includes only the elements between zero and <code><b>array.length</b></code>
– 1 inclusive. Named
properties are excluded from the stringification. An array is
stringified as an open left bracket, elements separated by comma,
and a closing right bracket.</p>
<ol><li><p>
If
<i>stack</i> contains <i>value</i> then throw a <b><a href="x15.11.html#x15.11.6.5" class="term-ref">TypeError</a></b>
exception because the structure is cyclical.</p>
</li>
<li><p>
Append
<i>value</i> to <i>stack</i>.</p>
</li>
<li><p>
Let
<i>stepback</i> be <i>indent</i>.</p>
</li>
<li><p>
Let
<i>indent</i> be the concatenation of <i>indent</i> and <i>gap</i>.</p>
</li>
<li><p>
Let
<i>partial</i> be an empty <a href="x8.html#x8.8">List</a>.</p>
</li>
<li><p>
Let
<i>len</i> be the result of calling the [[Get]] internal method of
value with argument <code><b>"length"</b></code>.</p>
</li>
<li><p>
Let
<i>index</i> be 0.</p>
</li>
<li><p>
Repeat
while <i>index</i> < <i>len</i></p>
<ol><li><p>
Let
<i>strP</i> be the result of calling the abstract operation <i>Str</i>
with arguments <a href="x9.html#x9.8">ToString</a>(<i>index</i>) and <i>value</i>.
</p>
</li>
<li><p>
If
<i>strP</i> is <b>undefined</b></p>
<ol><li><p>
Append
<code><b>"null"</b></code>
to <i>partial</i>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Append
<i>strP</i> to <i>partial</i>.</p>
</li></ol></li>
<li><p>
Increment
<i>index</i> by 1.</p>
</li></ol></li>
<li><p>
If
<i>partial</i> is empty ,then</p>
<ol><li><p>
Let
<i>final</i> be <code><b>"[]"</b></code>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
If
<i>gap</i> is the empty String</p>
<ol><li><p>
Let
<i>properties</i> be a String formed by concatenating all the
element Strings of <i>partial</i> with each adjacent pair of
Strings separated with the comma character. A comma is not
inserted either before the first String or after the last String.
</p>
</li>
<li><p>
Let
<i>final</i> be the result of concatenating <code><b>"["</b></code><b>,</b>
<i>properties</i>, and <code><b>"]"</b></code>.</p>
</li></ol></li>
<li><p>
Else</p>
<ol><li><p>
Let
<i>separator</i> be the result of concatenating the comma
character, the line feed character, and <i>indent</i>.</p>
</li>
<li><p>
Let
<i>properties</i> be a String formed by concatenating all the
element Strings of <i>partial</i> with each adjacent pair of
Strings separated with <i>separator</i>. The <i>separator</i>
String is not inserted either before the first String or after
the last String.</p>
</li>
<li><p>
Let
<i>final</i> be the result of concatenating "<code><b>[</b></code>",
the line feed character, <i>indent</i>, <i>properties</i>, the
line feed character, <i>stepback</i>, and "<code><b>]</b></code>".</p>
</li></ol></li></ol></li>
<li><p>
Remove
the last element of <i>stack</i>.</p>
</li>
<li><p>
Let
<i>indent</i> be <i>stepback</i>.</p>
</li>
<li><p>
Return
<i>final</i>.</p>
</li></ol><p><b class="note">NOTE 1</b> JSON structures are allowed to be nested to any depth, but they
must be acyclic. If <i>value</i>
is or contains a cyclic structure, then the stringify function must
throw a <b><a href="x15.11.html#x15.11.6.5" class="term-ref">TypeError</a></b> exception. This is an example of a value
that cannot be stringified:</p>
<p class="code-example"><code><b>a = [];</b></code></p>
<p class="code-example"><code><b>a[0] = a;</b></code></p>
<p class="code-example"><code><b>my_text = JSON.stringify(a); // This must throw an TypeError.</b></code></p>
<br><p><b class="note">NOTE 2</b> Symbolic <a href="x4.html#primitive_value" class="term-ref">primitive value</a>s are rendered as follows:</p>
<ul><li><p>The
<b>null</b> value is rendered in JSON text as the String <code>null</code>.</p>
</li>
<li><p>The
<b>undefined</b> value is not rendered.</p>
</li>
<li><p>The
<b>true</b> value is rendered in JSON text as the String <code>true</code>.</p>
</li>
<li><p class="sp">The
<b>false</b> value is rendered in JSON text as the String <code>false</code>.</p>
</li></ul><p class="sp"><b class="note">NOTE 3</b> String values are wrapped in double quotes. The characters <code><b>"</b></code>
and <code><b>\</b></code> are escaped
with <code><b>\</b></code> prefixes.
Control characters are replaced with escape sequences <code><b>\u</b></code>HHHH,
or with the shorter forms, <code><b>\b</b></code>