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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 } | 186 } |
187 if (!%PushIfAbsent(stack, value)) { | 187 if (!%PushIfAbsent(stack, value)) { |
188 throw MakeTypeError('circular_structure', []); | 188 throw MakeTypeError('circular_structure', []); |
189 } | 189 } |
190 builder.push("["); | 190 builder.push("["); |
191 var val = value[0]; | 191 var val = value[0]; |
192 if (IS_STRING(val)) { | 192 if (IS_STRING(val)) { |
193 // First entry is a string. Remaining entries are likely to be strings too. | 193 // First entry is a string. Remaining entries are likely to be strings too. |
194 builder.push(%QuoteJSONString(val)); | 194 builder.push(%QuoteJSONString(val)); |
195 for (var i = 1; i < len; i++) { | 195 for (var i = 1; i < len; i++) { |
196 builder.push(","); | |
197 val = value[i]; | 196 val = value[i]; |
198 if (IS_STRING(val)) { | 197 if (IS_STRING(val)) { |
199 builder.push(%QuoteJSONString(val)); | 198 builder.push(%QuoteJSONStringComma(val)); |
200 } else { | 199 } else { |
200 builder.push(","); | |
201 var before = builder.length; | 201 var before = builder.length; |
202 BasicJSONSerialize(i, value[i], stack, builder); | 202 BasicJSONSerialize(i, value[i], stack, builder); |
203 if (before == builder.length) builder.push("null"); | 203 if (before == builder.length) builder[before - 1] = ",null"; |
204 } | 204 } |
205 } | 205 } |
206 } else if (IS_NUMBER(val)) { | 206 } else if (IS_NUMBER(val)) { |
207 // First entry is a number. Remaining entries are likely to be numbers too. | 207 // First entry is a number. Remaining entries are likely to be numbers too. |
208 builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null"); | 208 builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null"); |
209 for (var i = 1; i < len; i++) { | 209 for (var i = 1; i < len; i++) { |
210 builder.push(","); | 210 builder.push(","); |
211 val = value[i]; | 211 val = value[i]; |
212 if (IS_NUMBER(val)) { | 212 if (IS_NUMBER(val)) { |
213 builder.push(NUMBER_IS_FINITE(val) | 213 builder.push(NUMBER_IS_FINITE(val) |
214 ? %_NumberToString(val) | 214 ? %_NumberToString(val) |
215 : "null"); | 215 : "null"); |
216 } else { | 216 } else { |
217 var before = builder.length; | 217 var before = builder.length; |
218 BasicJSONSerialize(i, value[i], stack, builder); | 218 BasicJSONSerialize(i, value[i], stack, builder); |
219 if (before == builder.length) builder.push("null"); | 219 if (before == builder.length) builder[before - 1] = ",null"; |
220 } | 220 } |
221 } | 221 } |
222 } else { | 222 } else { |
223 var before = builder.length; | 223 var before = builder.length; |
224 BasicJSONSerialize(0, val, stack, builder); | 224 BasicJSONSerialize(0, val, stack, builder); |
225 if (before == builder.length) builder.push("null"); | 225 if (before == builder.length) builder.push("null"); |
226 for (var i = 1; i < len; i++) { | 226 for (var i = 1; i < len; i++) { |
227 builder.push(","); | 227 builder.push(","); |
228 before = builder.length; | 228 before = builder.length; |
229 val = value[i]; | 229 val = value[i]; |
230 BasicJSONSerialize(i, val, stack, builder); | 230 BasicJSONSerialize(i, val, stack, builder); |
231 if (before == builder.length) builder.push("null"); | 231 if (before == builder.length) builder[before - 1] = ",null"; |
232 } | 232 } |
233 } | 233 } |
234 stack.pop(); | 234 stack.pop(); |
235 builder.push("]"); | 235 builder.push("]"); |
236 } | 236 } |
237 | 237 |
238 | 238 |
239 function BasicSerializeObject(value, stack, builder) { | 239 function BasicSerializeObject(value, stack, builder) { |
240 if (!%PushIfAbsent(stack, value)) { | 240 if (!%PushIfAbsent(stack, value)) { |
241 throw MakeTypeError('circular_structure', []); | 241 throw MakeTypeError('circular_structure', []); |
242 } | 242 } |
243 builder.push("{"); | 243 builder.push("{"); |
244 var not_first = false; | |
Erik Corry
2011/01/14 14:42:57
This variable should be called first and the boole
sandholm
2011/01/16 21:08:43
Done.
| |
244 for (var p in value) { | 245 for (var p in value) { |
245 if (%HasLocalProperty(value, p)) { | 246 if (%HasLocalProperty(value, p)) { |
246 builder.push(%QuoteJSONString(p)); | 247 if (not_first) builder.push(%QuoteJSONStringComma(p)); |
Erik Corry
2011/01/14 14:42:57
multi-line if statements should use {}
sandholm
2011/01/16 21:08:43
Done.
| |
248 else builder.push(%QuoteJSONString(p)); | |
247 builder.push(":"); | 249 builder.push(":"); |
248 var before = builder.length; | 250 var before = builder.length; |
249 BasicJSONSerialize(p, value[p], stack, builder); | 251 BasicJSONSerialize(p, value[p], stack, builder); |
250 if (before == builder.length) { | 252 if (before == builder.length) { |
251 builder.pop(); | 253 builder.pop(); |
252 builder.pop(); | 254 builder.pop(); |
253 } else { | 255 } else { |
254 builder.push(","); | 256 not_first = true; |
255 } | 257 } |
256 } | 258 } |
257 } | 259 } |
258 stack.pop(); | 260 stack.pop(); |
259 if (builder.pop() != ",") { | 261 builder.push("}"); |
260 builder.push("{}"); // Object has no own properties. Push "{" back on. | |
261 } else { | |
262 builder.push("}"); | |
263 } | |
264 } | 262 } |
265 | 263 |
266 | 264 |
267 function BasicJSONSerialize(key, value, stack, builder) { | 265 function BasicJSONSerialize(key, value, stack, builder) { |
268 if (IS_SPEC_OBJECT(value)) { | 266 if (IS_SPEC_OBJECT(value)) { |
269 var toJSON = value.toJSON; | 267 var toJSON = value.toJSON; |
270 if (IS_FUNCTION(toJSON)) { | 268 if (IS_FUNCTION(toJSON)) { |
271 value = %_CallFunction(value, ToString(key), toJSON); | 269 value = %_CallFunction(value, ToString(key), toJSON); |
272 } | 270 } |
273 } | 271 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 } | 330 } |
333 | 331 |
334 function SetupJSON() { | 332 function SetupJSON() { |
335 InstallFunctions($JSON, DONT_ENUM, $Array( | 333 InstallFunctions($JSON, DONT_ENUM, $Array( |
336 "parse", JSONParse, | 334 "parse", JSONParse, |
337 "stringify", JSONStringify | 335 "stringify", JSONStringify |
338 )); | 336 )); |
339 } | 337 } |
340 | 338 |
341 SetupJSON(); | 339 SetupJSON(); |
OLD | NEW |