| 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 |
| 69 function SerializeArray(value, replacer, stack, indent, gap) { | 79 function SerializeArray(value, replacer, stack, indent, gap) { |
| 70 if (!%PushIfAbsent(stack, value)) { | 80 if (StackContains(stack, value)) { |
| 71 throw MakeTypeError('circular_structure', []); | 81 throw MakeTypeError('circular_structure', []); |
| 72 } | 82 } |
| 83 stack.push(value); |
| 73 var stepback = indent; | 84 var stepback = indent; |
| 74 indent += gap; | 85 indent += gap; |
| 75 var partial = []; | 86 var partial = []; |
| 76 var len = value.length; | 87 var len = value.length; |
| 77 for (var i = 0; i < len; i++) { | 88 for (var i = 0; i < len; i++) { |
| 78 var strP = JSONSerialize($String(i), value, replacer, stack, | 89 var strP = JSONSerialize($String(i), value, replacer, stack, |
| 79 indent, gap); | 90 indent, gap); |
| 80 if (IS_UNDEFINED(strP)) { | 91 if (IS_UNDEFINED(strP)) { |
| 81 strP = "null"; | 92 strP = "null"; |
| 82 } | 93 } |
| 83 partial.push(strP); | 94 partial.push(strP); |
| 84 } | 95 } |
| 85 var final; | 96 var final; |
| 86 if (gap == "") { | 97 if (gap == "") { |
| 87 final = "[" + partial.join(",") + "]"; | 98 final = "[" + partial.join(",") + "]"; |
| 88 } else if (partial.length > 0) { | 99 } else if (partial.length > 0) { |
| 89 var separator = ",\n" + indent; | 100 var separator = ",\n" + indent; |
| 90 final = "[\n" + indent + partial.join(separator) + "\n" + | 101 final = "[\n" + indent + partial.join(separator) + "\n" + |
| 91 stepback + "]"; | 102 stepback + "]"; |
| 92 } else { | 103 } else { |
| 93 final = "[]"; | 104 final = "[]"; |
| 94 } | 105 } |
| 95 stack.pop(); | 106 stack.pop(); |
| 96 return final; | 107 return final; |
| 97 } | 108 } |
| 98 | 109 |
| 99 function SerializeObject(value, replacer, stack, indent, gap) { | 110 function SerializeObject(value, replacer, stack, indent, gap) { |
| 100 if (!%PushIfAbsent(stack, value)) { | 111 if (StackContains(stack, value)) { |
| 101 throw MakeTypeError('circular_structure', []); | 112 throw MakeTypeError('circular_structure', []); |
| 102 } | 113 } |
| 114 stack.push(value); |
| 103 var stepback = indent; | 115 var stepback = indent; |
| 104 indent += gap; | 116 indent += gap; |
| 105 var partial = []; | 117 var partial = []; |
| 106 if (IS_ARRAY(replacer)) { | 118 if (IS_ARRAY(replacer)) { |
| 107 var length = replacer.length; | 119 var length = replacer.length; |
| 108 for (var i = 0; i < length; i++) { | 120 for (var i = 0; i < length; i++) { |
| 109 if (ObjectHasOwnProperty.call(replacer, i)) { | 121 if (ObjectHasOwnProperty.call(replacer, i)) { |
| 110 var p = replacer[i]; | 122 var p = replacer[i]; |
| 111 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 123 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
| 112 if (!IS_UNDEFINED(strP)) { | 124 if (!IS_UNDEFINED(strP)) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 190 } |
| 179 case "number": | 191 case "number": |
| 180 return $isFinite(value) ? $String(value) : "null"; | 192 return $isFinite(value) ? $String(value) : "null"; |
| 181 case "boolean": | 193 case "boolean": |
| 182 return value ? "true" : "false"; | 194 return value ? "true" : "false"; |
| 183 } | 195 } |
| 184 } | 196 } |
| 185 | 197 |
| 186 | 198 |
| 187 function BasicSerializeArray(value, stack, builder) { | 199 function BasicSerializeArray(value, stack, builder) { |
| 188 if (!%PushIfAbsent(stack, value)) { | 200 if (StackContains(stack, value)) { |
| 189 throw MakeTypeError('circular_structure', []); | 201 throw MakeTypeError('circular_structure', []); |
| 190 } | 202 } |
| 203 stack.push(value); |
| 191 builder.push("["); | 204 builder.push("["); |
| 192 var len = value.length; | 205 var len = value.length; |
| 193 for (var i = 0; i < len; i++) { | 206 for (var i = 0; i < len; i++) { |
| 194 var before = builder.length; | 207 var before = builder.length; |
| 195 BasicJSONSerialize(i, value, stack, builder); | 208 BasicJSONSerialize(i, value, stack, builder); |
| 196 if (before == builder.length) builder.push("null"); | 209 if (before == builder.length) builder.push("null"); |
| 197 builder.push(","); | 210 builder.push(","); |
| 198 } | 211 } |
| 199 stack.pop(); | 212 stack.pop(); |
| 200 if (builder.pop() != ",") { | 213 if (builder.pop() != ",") { |
| 201 builder.push("[]"); // Zero length array. Push "[" back on. | 214 builder.push("[]"); // Zero length array. Push "[" back on. |
| 202 } else { | 215 } else { |
| 203 builder.push("]"); | 216 builder.push("]"); |
| 204 } | 217 } |
| 205 | 218 |
| 206 } | 219 } |
| 207 | 220 |
| 208 | 221 |
| 209 function BasicSerializeObject(value, stack, builder) { | 222 function BasicSerializeObject(value, stack, builder) { |
| 210 if (!%PushIfAbsent(stack, value)) { | 223 if (StackContains(stack, value)) { |
| 211 throw MakeTypeError('circular_structure', []); | 224 throw MakeTypeError('circular_structure', []); |
| 212 } | 225 } |
| 226 stack.push(value); |
| 213 builder.push("{"); | 227 builder.push("{"); |
| 214 for (var p in value) { | 228 for (var p in value) { |
| 215 if (%HasLocalProperty(value, p)) { | 229 if (%HasLocalProperty(value, p)) { |
| 216 builder.push(%QuoteJSONString(p)); | 230 builder.push(%QuoteJSONString(p)); |
| 217 builder.push(":"); | 231 builder.push(":"); |
| 218 var before = builder.length; | 232 var before = builder.length; |
| 219 BasicJSONSerialize(p, value, stack, builder); | 233 BasicJSONSerialize(p, value, stack, builder); |
| 220 if (before == builder.length) { | 234 if (before == builder.length) { |
| 221 builder.pop(); | 235 builder.pop(); |
| 222 builder.pop(); | 236 builder.pop(); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 } | 319 } |
| 306 | 320 |
| 307 function SetupJSON() { | 321 function SetupJSON() { |
| 308 InstallFunctions($JSON, DONT_ENUM, $Array( | 322 InstallFunctions($JSON, DONT_ENUM, $Array( |
| 309 "parse", JSONParse, | 323 "parse", JSONParse, |
| 310 "stringify", JSONStringify | 324 "stringify", JSONStringify |
| 311 )); | 325 )); |
| 312 } | 326 } |
| 313 | 327 |
| 314 SetupJSON(); | 328 SetupJSON(); |
| OLD | NEW |