OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 function JSONParse(text, reviver) { | 60 function JSONParse(text, reviver) { |
61 var unfiltered = ParseJSONUnfiltered(text); | 61 var unfiltered = ParseJSONUnfiltered(text); |
62 if (IS_FUNCTION(reviver)) { | 62 if (IS_FUNCTION(reviver)) { |
63 return Revive({'': unfiltered}, '', reviver); | 63 return Revive({'': unfiltered}, '', reviver); |
64 } else { | 64 } else { |
65 return unfiltered; | 65 return unfiltered; |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 function StackContains(stack, val) { | |
70 var length = stack.length; | |
71 for (var i = 0; i < length; i++) { | |
72 if (stack[i] === val) { | |
73 return true; | |
74 } | |
75 } | |
76 return false; | |
77 } | |
78 | |
79 function SerializeArray(value, replacer, stack, indent, gap) { | 69 function SerializeArray(value, replacer, stack, indent, gap) { |
80 if (StackContains(stack, value)) { | 70 if (!%PushIfAbsent(stack, value)) { |
81 throw MakeTypeError('circular_structure', []); | 71 throw MakeTypeError('circular_structure', []); |
82 } | 72 } |
83 stack.push(value); | |
84 var stepback = indent; | 73 var stepback = indent; |
85 indent += gap; | 74 indent += gap; |
86 var partial = []; | 75 var partial = []; |
87 var len = value.length; | 76 var len = value.length; |
88 for (var i = 0; i < len; i++) { | 77 for (var i = 0; i < len; i++) { |
89 var strP = JSONSerialize($String(i), value, replacer, stack, | 78 var strP = JSONSerialize($String(i), value, replacer, stack, |
90 indent, gap); | 79 indent, gap); |
91 if (IS_UNDEFINED(strP)) { | 80 if (IS_UNDEFINED(strP)) { |
92 strP = "null"; | 81 strP = "null"; |
93 } | 82 } |
94 partial.push(strP); | 83 partial.push(strP); |
95 } | 84 } |
96 var final; | 85 var final; |
97 if (gap == "") { | 86 if (gap == "") { |
98 final = "[" + partial.join(",") + "]"; | 87 final = "[" + partial.join(",") + "]"; |
99 } else if (partial.length > 0) { | 88 } else if (partial.length > 0) { |
100 var separator = ",\n" + indent; | 89 var separator = ",\n" + indent; |
101 final = "[\n" + indent + partial.join(separator) + "\n" + | 90 final = "[\n" + indent + partial.join(separator) + "\n" + |
102 stepback + "]"; | 91 stepback + "]"; |
103 } else { | 92 } else { |
104 final = "[]"; | 93 final = "[]"; |
105 } | 94 } |
106 stack.pop(); | 95 stack.pop(); |
107 return final; | 96 return final; |
108 } | 97 } |
109 | 98 |
110 function SerializeObject(value, replacer, stack, indent, gap) { | 99 function SerializeObject(value, replacer, stack, indent, gap) { |
111 if (StackContains(stack, value)) { | 100 if (!%PushIfAbsent(stack, value)) { |
112 throw MakeTypeError('circular_structure', []); | 101 throw MakeTypeError('circular_structure', []); |
113 } | 102 } |
114 stack.push(value); | |
115 var stepback = indent; | 103 var stepback = indent; |
116 indent += gap; | 104 indent += gap; |
117 var partial = []; | 105 var partial = []; |
118 if (IS_ARRAY(replacer)) { | 106 if (IS_ARRAY(replacer)) { |
119 var length = replacer.length; | 107 var length = replacer.length; |
120 for (var i = 0; i < length; i++) { | 108 for (var i = 0; i < length; i++) { |
121 if (ObjectHasOwnProperty.call(replacer, i)) { | 109 if (ObjectHasOwnProperty.call(replacer, i)) { |
122 var p = replacer[i]; | 110 var p = replacer[i]; |
123 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 111 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
124 if (!IS_UNDEFINED(strP)) { | 112 if (!IS_UNDEFINED(strP)) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 } | 178 } |
191 case "number": | 179 case "number": |
192 return $isFinite(value) ? $String(value) : "null"; | 180 return $isFinite(value) ? $String(value) : "null"; |
193 case "boolean": | 181 case "boolean": |
194 return value ? "true" : "false"; | 182 return value ? "true" : "false"; |
195 } | 183 } |
196 } | 184 } |
197 | 185 |
198 | 186 |
199 function BasicSerializeArray(value, stack, builder) { | 187 function BasicSerializeArray(value, stack, builder) { |
200 if (StackContains(stack, value)) { | 188 if (!%PushIfAbsent(stack, value)) { |
201 throw MakeTypeError('circular_structure', []); | 189 throw MakeTypeError('circular_structure', []); |
202 } | 190 } |
203 stack.push(value); | |
204 builder.push("["); | 191 builder.push("["); |
205 var len = value.length; | 192 var len = value.length; |
206 for (var i = 0; i < len; i++) { | 193 for (var i = 0; i < len; i++) { |
207 var before = builder.length; | 194 var before = builder.length; |
208 BasicJSONSerialize(i, value, stack, builder); | 195 BasicJSONSerialize(i, value, stack, builder); |
209 if (before == builder.length) builder.push("null"); | 196 if (before == builder.length) builder.push("null"); |
210 builder.push(","); | 197 builder.push(","); |
211 } | 198 } |
212 stack.pop(); | 199 stack.pop(); |
213 if (builder.pop() != ",") { | 200 if (builder.pop() != ",") { |
214 builder.push("[]"); // Zero length array. Push "[" back on. | 201 builder.push("[]"); // Zero length array. Push "[" back on. |
215 } else { | 202 } else { |
216 builder.push("]"); | 203 builder.push("]"); |
217 } | 204 } |
218 | 205 |
219 } | 206 } |
220 | 207 |
221 | 208 |
222 function BasicSerializeObject(value, stack, builder) { | 209 function BasicSerializeObject(value, stack, builder) { |
223 if (StackContains(stack, value)) { | 210 if (!%PushIfAbsent(stack, value)) { |
224 throw MakeTypeError('circular_structure', []); | 211 throw MakeTypeError('circular_structure', []); |
225 } | 212 } |
226 stack.push(value); | |
227 builder.push("{"); | 213 builder.push("{"); |
228 for (var p in value) { | 214 for (var p in value) { |
229 if (%HasLocalProperty(value, p)) { | 215 if (%HasLocalProperty(value, p)) { |
230 builder.push(%QuoteJSONString(p)); | 216 builder.push(%QuoteJSONString(p)); |
231 builder.push(":"); | 217 builder.push(":"); |
232 var before = builder.length; | 218 var before = builder.length; |
233 BasicJSONSerialize(p, value, stack, builder); | 219 BasicJSONSerialize(p, value, stack, builder); |
234 if (before == builder.length) { | 220 if (before == builder.length) { |
235 builder.pop(); | 221 builder.pop(); |
236 builder.pop(); | 222 builder.pop(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 } | 305 } |
320 | 306 |
321 function SetupJSON() { | 307 function SetupJSON() { |
322 InstallFunctions($JSON, DONT_ENUM, $Array( | 308 InstallFunctions($JSON, DONT_ENUM, $Array( |
323 "parse", JSONParse, | 309 "parse", JSONParse, |
324 "stringify", JSONStringify | 310 "stringify", JSONStringify |
325 )); | 311 )); |
326 } | 312 } |
327 | 313 |
328 SetupJSON(); | 314 SetupJSON(); |
OLD | NEW |