| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 '/': '\\/', | 73 '/': '\\/', |
| 74 '\b': '\\b', | 74 '\b': '\\b', |
| 75 '\f': '\\f', | 75 '\f': '\\f', |
| 76 '\n': '\\n', | 76 '\n': '\\n', |
| 77 '\r': '\\r', | 77 '\r': '\\r', |
| 78 '\t': '\\t', | 78 '\t': '\\t', |
| 79 '\x0B': '\\u000b' | 79 '\x0B': '\\u000b' |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 function QuoteSingleJSONCharacter(c) { | 82 function QuoteSingleJSONCharacter(c) { |
| 83 if (c in characterQuoteCache) | 83 if (c in characterQuoteCache) { |
| 84 return characterQuoteCache[c]; | 84 return characterQuoteCache[c]; |
| 85 } |
| 85 var charCode = c.charCodeAt(0); | 86 var charCode = c.charCodeAt(0); |
| 86 var result; | 87 var result; |
| 87 if (charCode < 16) result = '\\u000'; | 88 if (charCode < 16) result = '\\u000'; |
| 88 else if (charCode < 256) result = '\\u00'; | 89 else if (charCode < 256) result = '\\u00'; |
| 89 else if (charCode < 4096) result = '\\u0'; | 90 else if (charCode < 4096) result = '\\u0'; |
| 90 else result = '\\u'; | 91 else result = '\\u'; |
| 91 result += charCode.toString(16); | 92 result += charCode.toString(16); |
| 92 characterQuoteCache[c] = result; | 93 characterQuoteCache[c] = result; |
| 93 return result; | 94 return result; |
| 94 } | 95 } |
| 95 | 96 |
| 96 function QuoteJSONString(str) { | 97 function QuoteJSONString(str) { |
| 97 var quotable = /[\\\"\x00-\x1f\x80-\uffff]/g; | 98 var quotable = /[\\\"\x00-\x1f\x80-\uffff]/g; |
| 98 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; | 99 return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; |
| 99 } | 100 } |
| 100 | 101 |
| 101 function StackContains(stack, val) { | 102 function StackContains(stack, val) { |
| 102 var length = stack.length; | 103 var length = stack.length; |
| 103 for (var i = 0; i < length; i++) { | 104 for (var i = 0; i < length; i++) { |
| 104 if (stack[i] === val) | 105 if (stack[i] === val) { |
| 105 return true; | 106 return true; |
| 107 } |
| 106 } | 108 } |
| 107 return false; | 109 return false; |
| 108 } | 110 } |
| 109 | 111 |
| 110 function SerializeArray(value, replacer, stack, indent, gap) { | 112 function SerializeArray(value, replacer, stack, indent, gap) { |
| 111 if (StackContains(stack, value)) | 113 if (StackContains(stack, value)) { |
| 112 throw MakeTypeError('circular_structure', []); | 114 throw MakeTypeError('circular_structure', []); |
| 115 } |
| 113 stack.push(value); | 116 stack.push(value); |
| 114 var stepback = indent; | 117 var stepback = indent; |
| 115 indent += gap; | 118 indent += gap; |
| 116 var partial = []; | 119 var partial = []; |
| 117 var len = value.length; | 120 var len = value.length; |
| 118 for (var i = 0; i < len; i++) { | 121 for (var i = 0; i < len; i++) { |
| 119 var strP = JSONSerialize($String(i), value, replacer, stack, | 122 var strP = JSONSerialize($String(i), value, replacer, stack, |
| 120 indent, gap); | 123 indent, gap); |
| 121 if (IS_UNDEFINED(strP)) | 124 if (IS_UNDEFINED(strP)) { |
| 122 strP = "null"; | 125 strP = "null"; |
| 126 } |
| 123 partial.push(strP); | 127 partial.push(strP); |
| 124 } | 128 } |
| 125 var final; | 129 var final; |
| 126 if (gap == "") { | 130 if (gap == "") { |
| 127 final = "[" + partial.join(",") + "]"; | 131 final = "[" + partial.join(",") + "]"; |
| 128 } else if (partial.length > 0) { | 132 } else if (partial.length > 0) { |
| 129 var separator = ",\n" + indent; | 133 var separator = ",\n" + indent; |
| 130 final = "[\n" + indent + partial.join(separator) + "\n" + | 134 final = "[\n" + indent + partial.join(separator) + "\n" + |
| 131 stepback + "]"; | 135 stepback + "]"; |
| 132 } else { | 136 } else { |
| 133 final = "[]"; | 137 final = "[]"; |
| 134 } | 138 } |
| 135 stack.pop(); | 139 stack.pop(); |
| 136 return final; | 140 return final; |
| 137 } | 141 } |
| 138 | 142 |
| 139 function SerializeObject(value, replacer, stack, indent, gap) { | 143 function SerializeObject(value, replacer, stack, indent, gap) { |
| 140 if (StackContains(stack, value)) | 144 if (StackContains(stack, value)) { |
| 141 throw MakeTypeError('circular_structure', []); | 145 throw MakeTypeError('circular_structure', []); |
| 146 } |
| 142 stack.push(value); | 147 stack.push(value); |
| 143 var stepback = indent; | 148 var stepback = indent; |
| 144 indent += gap; | 149 indent += gap; |
| 145 var partial = []; | 150 var partial = []; |
| 146 if (IS_ARRAY(replacer)) { | 151 if (IS_ARRAY(replacer)) { |
| 147 var length = replacer.length; | 152 var length = replacer.length; |
| 148 for (var i = 0; i < length; i++) { | 153 for (var i = 0; i < length; i++) { |
| 149 if (ObjectHasOwnProperty.call(replacer, i)) { | 154 if (ObjectHasOwnProperty.call(replacer, i)) { |
| 150 var p = replacer[i]; | 155 var p = replacer[i]; |
| 151 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 156 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 181 final = "{}"; | 186 final = "{}"; |
| 182 } | 187 } |
| 183 stack.pop(); | 188 stack.pop(); |
| 184 return final; | 189 return final; |
| 185 } | 190 } |
| 186 | 191 |
| 187 function JSONSerialize(key, holder, replacer, stack, indent, gap) { | 192 function JSONSerialize(key, holder, replacer, stack, indent, gap) { |
| 188 var value = holder[key]; | 193 var value = holder[key]; |
| 189 if (IS_OBJECT(value) && value) { | 194 if (IS_OBJECT(value) && value) { |
| 190 var toJSON = value.toJSON; | 195 var toJSON = value.toJSON; |
| 191 if (IS_FUNCTION(toJSON)) | 196 if (IS_FUNCTION(toJSON)) { |
| 192 value = toJSON.call(value, key); | 197 value = toJSON.call(value, key); |
| 198 } |
| 193 } | 199 } |
| 194 if (IS_FUNCTION(replacer)) | 200 if (IS_FUNCTION(replacer)) { |
| 195 value = replacer.call(holder, key, value); | 201 value = replacer.call(holder, key, value); |
| 202 } |
| 196 // Unwrap value if necessary | 203 // Unwrap value if necessary |
| 197 if (IS_OBJECT(value)) { | 204 if (IS_OBJECT(value)) { |
| 198 if (IS_NUMBER_WRAPPER(value)) { | 205 if (IS_NUMBER_WRAPPER(value)) { |
| 199 value = $Number(value); | 206 value = $Number(value); |
| 200 } else if (IS_STRING_WRAPPER(value)) { | 207 } else if (IS_STRING_WRAPPER(value)) { |
| 201 value = $String(value); | 208 value = $String(value); |
| 209 } else if (IS_BOOLEAN_WRAPPER(value)) { |
| 210 value = $Boolean(value); |
| 202 } | 211 } |
| 203 } | 212 } |
| 204 switch (typeof value) { | 213 switch (typeof value) { |
| 205 case "string": | 214 case "string": |
| 206 return QuoteJSONString(value); | 215 return QuoteJSONString(value); |
| 207 case "object": | 216 case "object": |
| 208 if (!value) { | 217 if (!value) { |
| 209 return "null"; | 218 return "null"; |
| 210 } else if (IS_ARRAY(value)) { | 219 } else if (IS_ARRAY(value)) { |
| 211 return SerializeArray(value, replacer, stack, indent, gap); | 220 return SerializeArray(value, replacer, stack, indent, gap); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 225 if (IS_OBJECT(space)) { | 234 if (IS_OBJECT(space)) { |
| 226 // Unwrap 'space' if it is wrapped | 235 // Unwrap 'space' if it is wrapped |
| 227 if (IS_NUMBER_WRAPPER(space)) { | 236 if (IS_NUMBER_WRAPPER(space)) { |
| 228 space = $Number(space); | 237 space = $Number(space); |
| 229 } else if (IS_STRING_WRAPPER(space)) { | 238 } else if (IS_STRING_WRAPPER(space)) { |
| 230 space = $String(space); | 239 space = $String(space); |
| 231 } | 240 } |
| 232 } | 241 } |
| 233 var gap; | 242 var gap; |
| 234 if (IS_NUMBER(space)) { | 243 if (IS_NUMBER(space)) { |
| 235 space = $Math.min(space, 100); | 244 space = $Math.min(space, 10); |
| 236 gap = ""; | 245 gap = ""; |
| 237 for (var i = 0; i < space; i++) | 246 for (var i = 0; i < space; i++) { |
| 238 gap += " "; | 247 gap += " "; |
| 248 } |
| 239 } else if (IS_STRING(space)) { | 249 } else if (IS_STRING(space)) { |
| 240 gap = space; | 250 if (space.length > 10) { |
| 251 gap = space.substring(0, 10); |
| 252 } else { |
| 253 gap = space; |
| 254 } |
| 241 } else { | 255 } else { |
| 242 gap = ""; | 256 gap = ""; |
| 243 } | 257 } |
| 244 return JSONSerialize('', {'': value}, replacer, stack, indent, gap); | 258 return JSONSerialize('', {'': value}, replacer, stack, indent, gap); |
| 245 } | 259 } |
| 246 | 260 |
| 247 function SetupJSON() { | 261 function SetupJSON() { |
| 248 InstallFunctions($JSON, DONT_ENUM, $Array( | 262 InstallFunctions($JSON, DONT_ENUM, $Array( |
| 249 "parse", JSONParse, | 263 "parse", JSONParse, |
| 250 "stringify", JSONStringify | 264 "stringify", JSONStringify |
| 251 )); | 265 )); |
| 252 } | 266 } |
| 253 | 267 |
| 254 SetupJSON(); | 268 SetupJSON(); |
| OLD | NEW |