| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "platform/JSONValues.h" | 31 #include "platform/JSONValues.h" |
| 32 | 32 |
| 33 #include "platform/Decimal.h" | 33 #include "platform/Decimal.h" |
| 34 #include "wtf/MathExtras.h" | 34 #include "wtf/MathExtras.h" |
| 35 #include "wtf/text/StringBuilder.h" | 35 #include "wtf/text/StringBuilder.h" |
| 36 #include <algorithm> |
| 37 #include <cmath> |
| 36 | 38 |
| 37 namespace blink { | 39 namespace blink { |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 const char* const nullString = "null"; | 43 const char* const nullString = "null"; |
| 42 const char* const trueString = "true"; | 44 const char* const trueString = "true"; |
| 43 const char* const falseString = "false"; | 45 const char* const falseString = "false"; |
| 44 | 46 |
| 45 inline bool escapeChar(UChar c, StringBuilder* dst) | 47 inline bool escapeChar(UChar c, StringBuilder* dst) |
| 46 { | 48 { |
| 47 switch (c) { | 49 switch (c) { |
| 48 case '\b': dst->append("\\b"); break; | 50 case '\b': dst->append("\\b"); break; |
| 49 case '\f': dst->append("\\f"); break; | 51 case '\f': dst->append("\\f"); break; |
| 50 case '\n': dst->append("\\n"); break; | 52 case '\n': dst->append("\\n"); break; |
| 51 case '\r': dst->append("\\r"); break; | 53 case '\r': dst->append("\\r"); break; |
| 52 case '\t': dst->append("\\t"); break; | 54 case '\t': dst->append("\\t"); break; |
| 53 case '\\': dst->append("\\\\"); break; | 55 case '\\': dst->append("\\\\"); break; |
| 54 case '"': dst->append("\\\""); break; | 56 case '"': dst->append("\\\""); break; |
| 55 default: | 57 default: |
| 56 return false; | 58 return false; |
| 57 } | 59 } |
| 58 return true; | 60 return true; |
| 59 } | 61 } |
| 60 | 62 |
| 63 const LChar hexDigits[17] = "0123456789ABCDEF"; |
| 64 |
| 65 void appendUnsignedAsHex(UChar number, StringBuilder* dst) |
| 66 { |
| 67 dst->append("\\u"); |
| 68 for (size_t i = 0; i < 4; ++i) { |
| 69 dst->append(hexDigits[(number & 0xF000) >> 12]); |
| 70 number <<= 4; |
| 71 } |
| 72 } |
| 73 |
| 61 void writeIndent(int depth, StringBuilder* output) | 74 void writeIndent(int depth, StringBuilder* output) |
| 62 { | 75 { |
| 63 for (int i = 0; i < depth; ++i) | 76 for (int i = 0; i < depth; ++i) |
| 64 output->append(" "); | 77 output->append(" "); |
| 65 } | 78 } |
| 66 | 79 |
| 67 } // anonymous namespace | 80 } // anonymous namespace |
| 68 | 81 |
| 69 void escapeStringForJSON(const String& str, StringBuilder* dst) | 82 void escapeStringForJSON(const String& str, StringBuilder* dst) |
| 70 { | 83 { |
| 71 for (unsigned i = 0; i < str.length(); ++i) { | 84 for (unsigned i = 0; i < str.length(); ++i) { |
| 72 UChar c = str[i]; | 85 UChar c = str[i]; |
| 73 if (!escapeChar(c, dst)) { | 86 if (!escapeChar(c, dst)) { |
| 74 if (c < 32 || c > 126 || c == '<' || c == '>') { | 87 if (c < 32 || c > 126 || c == '<' || c == '>') { |
| 75 // 1. Escaping <, > to prevent script execution. | 88 // 1. Escaping <, > to prevent script execution. |
| 76 // 2. Technically, we could also pass through c > 126 as UTF8, b
ut this | 89 // 2. Technically, we could also pass through c > 126 as UTF8, b
ut this |
| 77 // is also optional. It would also be a pain to implement her
e. | 90 // is also optional. It would also be a pain to implement her
e. |
| 78 unsigned symbol = static_cast<unsigned>(c); | 91 appendUnsignedAsHex(c, dst); |
| 79 String symbolCode = String::format("\\u%04X", symbol); | |
| 80 dst->append(symbolCode); | |
| 81 } else { | 92 } else { |
| 82 dst->append(c); | 93 dst->append(c); |
| 83 } | 94 } |
| 84 } | 95 } |
| 85 } | 96 } |
| 86 } | 97 } |
| 87 | 98 |
| 88 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) | 99 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) |
| 89 { | 100 { |
| 90 dst->append('"'); | 101 dst->append('"'); |
| 91 escapeStringForJSON(str, dst); | 102 escapeStringForJSON(str, dst); |
| 92 dst->append('"'); | 103 dst->append('"'); |
| 93 } | 104 } |
| 94 | 105 |
| 95 String JSONValue::quoteString(const String& input) | 106 String JSONValue::quoteString(const String& input) |
| 96 { | 107 { |
| 97 StringBuilder builder; | 108 StringBuilder builder; |
| 98 doubleQuoteStringForJSON(input, &builder); | 109 doubleQuoteStringForJSON(input, &builder); |
| 99 return builder.toString(); | 110 return builder.toString(); |
| 100 } | 111 } |
| 101 | 112 |
| 102 bool JSONValue::asBoolean(bool*) const | 113 bool JSONValue::asBoolean(bool*) const |
| 103 { | 114 { |
| 104 return false; | 115 return false; |
| 105 } | 116 } |
| 106 | 117 |
| 107 bool JSONValue::asNumber(double*) const | 118 bool JSONValue::asDouble(double*) const |
| 108 { | 119 { |
| 109 return false; | 120 return false; |
| 110 } | 121 } |
| 111 | 122 |
| 112 bool JSONValue::asNumber(long*) const | 123 bool JSONValue::asInteger(int*) const |
| 113 { | |
| 114 return false; | |
| 115 } | |
| 116 | |
| 117 bool JSONValue::asNumber(int*) const | |
| 118 { | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 bool JSONValue::asNumber(unsigned long*) const | |
| 123 { | |
| 124 return false; | |
| 125 } | |
| 126 | |
| 127 bool JSONValue::asNumber(unsigned*) const | |
| 128 { | 124 { |
| 129 return false; | 125 return false; |
| 130 } | 126 } |
| 131 | 127 |
| 132 bool JSONValue::asString(String*) const | 128 bool JSONValue::asString(String*) const |
| 133 { | 129 { |
| 134 return false; | 130 return false; |
| 135 } | 131 } |
| 132 |
| 133 bool JSONValue::asSerialized(String*) const |
| 134 { |
| 135 return false; |
| 136 } |
| 136 | 137 |
| 137 String JSONValue::toJSONString() const | 138 String JSONValue::toJSONString() const |
| 138 { | 139 { |
| 139 StringBuilder result; | 140 StringBuilder result; |
| 140 result.reserveCapacity(512); | 141 result.reserveCapacity(512); |
| 141 writeJSON(&result); | 142 writeJSON(&result); |
| 142 return result.toString(); | 143 return result.toString(); |
| 143 } | 144 } |
| 144 | 145 |
| 145 String JSONValue::toPrettyJSONString() const | 146 String JSONValue::toPrettyJSONString() const |
| 146 { | 147 { |
| 147 StringBuilder result; | 148 StringBuilder result; |
| 148 result.reserveCapacity(512); | 149 result.reserveCapacity(512); |
| 149 prettyWriteJSON(&result); | 150 prettyWriteJSON(&result); |
| 150 return result.toString(); | 151 return result.toString(); |
| 151 } | 152 } |
| 152 | 153 |
| 153 void JSONValue::writeJSON(StringBuilder* output) const | 154 void JSONValue::writeJSON(StringBuilder* output) const |
| 154 { | 155 { |
| 155 ASSERT(m_type == TypeNull); | 156 DCHECK(m_type == TypeNull); |
| 156 output->append(nullString, 4); | 157 output->append(nullString, 4); |
| 157 } | 158 } |
| 158 | 159 |
| 159 void JSONValue::prettyWriteJSON(StringBuilder* output) const | 160 void JSONValue::prettyWriteJSON(StringBuilder* output) const |
| 160 { | 161 { |
| 161 prettyWriteJSONInternal(output, 0); | 162 prettyWriteJSONInternal(output, 0); |
| 162 output->append('\n'); | 163 output->append('\n'); |
| 163 } | 164 } |
| 164 | 165 |
| 165 void JSONValue::prettyWriteJSONInternal(StringBuilder* output, int depth) const | 166 void JSONValue::prettyWriteJSONInternal(StringBuilder* output, int depth) const |
| 166 { | 167 { |
| 167 writeJSON(output); | 168 writeJSON(output); |
| 168 } | 169 } |
| 169 | 170 |
| 171 std::unique_ptr<JSONValue> JSONValue::clone() const |
| 172 { |
| 173 return JSONValue::null(); |
| 174 } |
| 175 |
| 170 bool JSONBasicValue::asBoolean(bool* output) const | 176 bool JSONBasicValue::asBoolean(bool* output) const |
| 171 { | 177 { |
| 172 if (getType() != TypeBoolean) | 178 if (type() != TypeBoolean) |
| 173 return false; | 179 return false; |
| 174 *output = m_boolValue; | 180 *output = m_boolValue; |
| 175 return true; | 181 return true; |
| 176 } | 182 } |
| 177 | 183 |
| 178 bool JSONBasicValue::asNumber(double* output) const | 184 bool JSONBasicValue::asDouble(double* output) const |
| 179 { | 185 { |
| 180 if (getType() != TypeNumber) | 186 if (type() == TypeDouble) { |
| 181 return false; | 187 *output = m_doubleValue; |
| 182 *output = m_doubleValue; | 188 return true; |
| 183 return true; | 189 } |
| 190 if (type() == TypeInteger) { |
| 191 *output = m_integerValue; |
| 192 return true; |
| 193 } |
| 194 return false; |
| 184 } | 195 } |
| 185 | 196 |
| 186 bool JSONBasicValue::asNumber(long* output) const | 197 bool JSONBasicValue::asInteger(int* output) const |
| 187 { | 198 { |
| 188 if (getType() != TypeNumber) | 199 if (type() != TypeInteger) |
| 189 return false; | 200 return false; |
| 190 *output = static_cast<long>(m_doubleValue); | 201 *output = m_integerValue; |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 bool JSONBasicValue::asNumber(int* output) const | |
| 195 { | |
| 196 if (getType() != TypeNumber) | |
| 197 return false; | |
| 198 *output = static_cast<int>(m_doubleValue); | |
| 199 return true; | |
| 200 } | |
| 201 | |
| 202 bool JSONBasicValue::asNumber(unsigned long* output) const | |
| 203 { | |
| 204 if (getType() != TypeNumber) | |
| 205 return false; | |
| 206 *output = static_cast<unsigned long>(m_doubleValue); | |
| 207 return true; | |
| 208 } | |
| 209 | |
| 210 bool JSONBasicValue::asNumber(unsigned* output) const | |
| 211 { | |
| 212 if (getType() != TypeNumber) | |
| 213 return false; | |
| 214 *output = static_cast<unsigned>(m_doubleValue); | |
| 215 return true; | 202 return true; |
| 216 } | 203 } |
| 217 | 204 |
| 218 void JSONBasicValue::writeJSON(StringBuilder* output) const | 205 void JSONBasicValue::writeJSON(StringBuilder* output) const |
| 219 { | 206 { |
| 220 ASSERT(getType() == TypeBoolean || getType() == TypeNumber); | 207 DCHECK(type() == TypeBoolean || type() == TypeInteger || type() == TypeDoubl
e); |
| 221 if (getType() == TypeBoolean) { | 208 if (type() == TypeBoolean) { |
| 222 if (m_boolValue) | 209 if (m_boolValue) |
| 223 output->append(trueString, 4); | 210 output->append(trueString, 4); |
| 224 else | 211 else |
| 225 output->append(falseString, 5); | 212 output->append(falseString, 5); |
| 226 } else if (getType() == TypeNumber) { | 213 } else if (type() == TypeDouble) { |
| 227 if (!std::isfinite(m_doubleValue)) { | 214 if (!std::isfinite(m_doubleValue)) { |
| 228 output->append(nullString, 4); | 215 output->append(nullString, 4); |
| 229 return; | 216 return; |
| 230 } | 217 } |
| 231 output->append(Decimal::fromDouble(m_doubleValue).toString()); | 218 output->append(Decimal::fromDouble(m_doubleValue).toString()); |
| 219 } else if (type() == TypeInteger) { |
| 220 output->append(String::number(m_integerValue)); |
| 232 } | 221 } |
| 233 } | 222 } |
| 234 | 223 |
| 224 std::unique_ptr<JSONValue> JSONBasicValue::clone() const |
| 225 { |
| 226 switch (type()) { |
| 227 case TypeDouble: return JSONBasicValue::create(m_doubleValue); |
| 228 case TypeInteger: return JSONBasicValue::create(m_integerValue); |
| 229 case TypeBoolean: return JSONBasicValue::create(m_boolValue); |
| 230 default: |
| 231 NOTREACHED(); |
| 232 } |
| 233 return nullptr; |
| 234 } |
| 235 |
| 235 bool JSONString::asString(String* output) const | 236 bool JSONString::asString(String* output) const |
| 236 { | 237 { |
| 237 *output = m_stringValue; | 238 *output = m_stringValue; |
| 238 return true; | 239 return true; |
| 239 } | 240 } |
| 240 | 241 |
| 241 void JSONString::writeJSON(StringBuilder* output) const | 242 void JSONString::writeJSON(StringBuilder* output) const |
| 242 { | 243 { |
| 243 ASSERT(getType() == TypeString); | 244 DCHECK(type() == TypeString); |
| 244 doubleQuoteStringForJSON(m_stringValue, output); | 245 doubleQuoteStringForJSON(m_stringValue, output); |
| 245 } | 246 } |
| 246 | 247 |
| 248 std::unique_ptr<JSONValue> JSONString::clone() const |
| 249 { |
| 250 return JSONString::create(m_stringValue); |
| 251 } |
| 252 |
| 253 bool SerializedValue::asSerialized(String* output) const |
| 254 { |
| 255 *output = m_serializedValue; |
| 256 return true; |
| 257 } |
| 258 |
| 259 void SerializedValue::writeJSON(StringBuilder* output) const |
| 260 { |
| 261 DCHECK(type() == TypeSerialized); |
| 262 output->append(m_serializedValue); |
| 263 } |
| 264 |
| 265 std::unique_ptr<JSONValue> SerializedValue::clone() const |
| 266 { |
| 267 return SerializedValue::create(m_serializedValue); |
| 268 } |
| 269 |
| 247 JSONObject::~JSONObject() | 270 JSONObject::~JSONObject() |
| 248 { | 271 { |
| 249 } | 272 } |
| 250 | 273 |
| 251 void JSONObject::setBoolean(const String& name, bool value) | 274 void JSONObject::setBoolean(const String& name, bool value) |
| 252 { | 275 { |
| 253 setValue(name, JSONBasicValue::create(value)); | 276 setValue(name, JSONBasicValue::create(value)); |
| 254 } | 277 } |
| 255 | 278 |
| 256 void JSONObject::setNumber(const String& name, double value) | 279 void JSONObject::setInteger(const String& name, int value) |
| 257 { | 280 { |
| 258 setValue(name, JSONBasicValue::create(value)); | 281 setValue(name, JSONBasicValue::create(value)); |
| 259 } | 282 } |
| 283 |
| 284 void JSONObject::setDouble(const String& name, double value) |
| 285 { |
| 286 setValue(name, JSONBasicValue::create(value)); |
| 287 } |
| 260 | 288 |
| 261 void JSONObject::setString(const String& name, const String& value) | 289 void JSONObject::setString(const String& name, const String& value) |
| 262 { | 290 { |
| 263 setValue(name, JSONString::create(value)); | 291 setValue(name, JSONString::create(value)); |
| 264 } | 292 } |
| 265 | 293 |
| 266 void JSONObject::setValue(const String& name, PassRefPtr<JSONValue> value) | 294 void JSONObject::setValue(const String& name, std::unique_ptr<JSONValue> value) |
| 267 { | 295 { |
| 268 ASSERT(value); | 296 set(name, value); |
| 269 if (m_data.set(name, value).isNewEntry) | |
| 270 m_order.append(name); | |
| 271 } | 297 } |
| 272 | 298 |
| 273 void JSONObject::setObject(const String& name, PassRefPtr<JSONObject> value) | 299 void JSONObject::setObject(const String& name, std::unique_ptr<JSONObject> value
) |
| 274 { | 300 { |
| 275 ASSERT(value); | 301 set(name, value); |
| 276 if (m_data.set(name, value).isNewEntry) | |
| 277 m_order.append(name); | |
| 278 } | 302 } |
| 279 | 303 |
| 280 void JSONObject::setArray(const String& name, PassRefPtr<JSONArray> value) | 304 void JSONObject::setArray(const String& name, std::unique_ptr<JSONArray> value) |
| 281 { | 305 { |
| 282 ASSERT(value); | 306 set(name, value); |
| 283 if (m_data.set(name, value).isNewEntry) | |
| 284 m_order.append(name); | |
| 285 } | |
| 286 | |
| 287 JSONObject::iterator JSONObject::find(const String& name) | |
| 288 { | |
| 289 return m_data.find(name); | |
| 290 } | |
| 291 | |
| 292 JSONObject::const_iterator JSONObject::find(const String& name) const | |
| 293 { | |
| 294 return m_data.find(name); | |
| 295 } | 307 } |
| 296 | 308 |
| 297 bool JSONObject::getBoolean(const String& name, bool* output) const | 309 bool JSONObject::getBoolean(const String& name, bool* output) const |
| 298 { | 310 { |
| 299 RefPtr<JSONValue> value = get(name); | 311 JSONValue* value = get(name); |
| 300 if (!value) | 312 if (!value) |
| 301 return false; | 313 return false; |
| 302 return value->asBoolean(output); | 314 return value->asBoolean(output); |
| 303 } | 315 } |
| 304 | 316 |
| 317 bool JSONObject::getInteger(const String& name, int* output) const |
| 318 { |
| 319 JSONValue* value = get(name); |
| 320 if (!value) |
| 321 return false; |
| 322 return value->asInteger(output); |
| 323 } |
| 324 |
| 325 bool JSONObject::getDouble(const String& name, double* output) const |
| 326 { |
| 327 JSONValue* value = get(name); |
| 328 if (!value) |
| 329 return false; |
| 330 return value->asDouble(output); |
| 331 } |
| 332 |
| 305 bool JSONObject::getString(const String& name, String* output) const | 333 bool JSONObject::getString(const String& name, String* output) const |
| 306 { | 334 { |
| 307 RefPtr<JSONValue> value = get(name); | 335 JSONValue* value = get(name); |
| 308 if (!value) | 336 if (!value) |
| 309 return false; | 337 return false; |
| 310 return value->asString(output); | 338 return value->asString(output); |
| 311 } | 339 } |
| 312 | 340 |
| 313 PassRefPtr<JSONObject> JSONObject::getObject(const String& name) const | 341 JSONObject* JSONObject::getObject(const String& name) const |
| 314 { | 342 { |
| 315 return JSONObject::cast(get(name)); | 343 return JSONObject::cast(get(name)); |
| 316 } | 344 } |
| 317 | 345 |
| 318 PassRefPtr<JSONArray> JSONObject::getArray(const String& name) const | 346 JSONArray* JSONObject::getArray(const String& name) const |
| 319 { | 347 { |
| 320 return JSONArray::cast(get(name)); | 348 return JSONArray::cast(get(name)); |
| 321 } | 349 } |
| 322 | 350 |
| 323 PassRefPtr<JSONValue> JSONObject::get(const String& name) const | 351 JSONValue* JSONObject::get(const String& name) const |
| 324 { | 352 { |
| 325 Dictionary::const_iterator it = m_data.find(name); | 353 Dictionary::const_iterator it = m_data.find(name); |
| 326 if (it == m_data.end()) | 354 if (it == m_data.end()) |
| 327 return nullptr; | 355 return nullptr; |
| 328 return it->value; | 356 return it->value.get(); |
| 357 } |
| 358 |
| 359 JSONObject::Entry JSONObject::at(size_t index) const |
| 360 { |
| 361 const String key = m_order[index]; |
| 362 return std::make_pair(key, m_data.find(key)->value.get()); |
| 329 } | 363 } |
| 330 | 364 |
| 331 bool JSONObject::booleanProperty(const String& name, bool defaultValue) const | 365 bool JSONObject::booleanProperty(const String& name, bool defaultValue) const |
| 332 { | 366 { |
| 333 bool result = defaultValue; | 367 bool result = defaultValue; |
| 334 getBoolean(name, &result); | 368 getBoolean(name, &result); |
| 335 return result; | 369 return result; |
| 336 } | 370 } |
| 337 | 371 |
| 372 int JSONObject::integerProperty(const String& name, int defaultValue) const |
| 373 { |
| 374 int result = defaultValue; |
| 375 getInteger(name, &result); |
| 376 return result; |
| 377 } |
| 378 |
| 379 double JSONObject::doubleProperty(const String& name, double defaultValue) const |
| 380 { |
| 381 double result = defaultValue; |
| 382 getDouble(name, &result); |
| 383 return result; |
| 384 } |
| 385 |
| 338 void JSONObject::remove(const String& name) | 386 void JSONObject::remove(const String& name) |
| 339 { | 387 { |
| 340 m_data.remove(name); | 388 m_data.remove(name); |
| 341 for (size_t i = 0; i < m_order.size(); ++i) { | 389 for (size_t i = 0; i < m_order.size(); ++i) { |
| 342 if (m_order[i] == name) { | 390 if (m_order[i] == name) { |
| 343 m_order.remove(i); | 391 m_order.remove(i); |
| 344 break; | 392 break; |
| 345 } | 393 } |
| 346 } | 394 } |
| 347 } | 395 } |
| 348 | 396 |
| 349 void JSONObject::writeJSON(StringBuilder* output) const | 397 void JSONObject::writeJSON(StringBuilder* output) const |
| 350 { | 398 { |
| 351 output->append('{'); | 399 output->append('{'); |
| 352 for (size_t i = 0; i < m_order.size(); ++i) { | 400 for (size_t i = 0; i < m_order.size(); ++i) { |
| 353 Dictionary::const_iterator it = m_data.find(m_order[i]); | 401 Dictionary::const_iterator it = m_data.find(m_order[i]); |
| 354 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); | 402 CHECK(it != m_data.end()); |
| 355 if (i) | 403 if (i) |
| 356 output->append(','); | 404 output->append(','); |
| 357 doubleQuoteStringForJSON(it->key, output); | 405 doubleQuoteStringForJSON(it->key, output); |
| 358 output->append(':'); | 406 output->append(':'); |
| 359 it->value->writeJSON(output); | 407 it->value->writeJSON(output); |
| 360 } | 408 } |
| 361 output->append('}'); | 409 output->append('}'); |
| 362 } | 410 } |
| 363 | 411 |
| 364 void JSONObject::prettyWriteJSONInternal(StringBuilder* output, int depth) const | 412 void JSONObject::prettyWriteJSONInternal(StringBuilder* output, int depth) const |
| 365 { | 413 { |
| 366 output->append("{\n"); | 414 output->append("{\n"); |
| 367 for (size_t i = 0; i < m_order.size(); ++i) { | 415 for (size_t i = 0; i < m_order.size(); ++i) { |
| 368 Dictionary::const_iterator it = m_data.find(m_order[i]); | 416 Dictionary::const_iterator it = m_data.find(m_order[i]); |
| 369 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); | 417 CHECK(it != m_data.end()); |
| 370 if (i) | 418 if (i) |
| 371 output->append(",\n"); | 419 output->append(",\n"); |
| 372 writeIndent(depth + 1, output); | 420 writeIndent(depth + 1, output); |
| 373 doubleQuoteStringForJSON(it->key, output); | 421 doubleQuoteStringForJSON(it->key, output); |
| 374 output->append(": "); | 422 output->append(": "); |
| 375 it->value->prettyWriteJSONInternal(output, depth + 1); | 423 it->value->prettyWriteJSONInternal(output, depth + 1); |
| 376 } | 424 } |
| 377 output->append('\n'); | 425 output->append('\n'); |
| 378 writeIndent(depth, output); | 426 writeIndent(depth, output); |
| 379 output->append('}'); | 427 output->append('}'); |
| 380 } | 428 } |
| 381 | 429 |
| 430 std::unique_ptr<JSONValue> JSONObject::clone() const |
| 431 { |
| 432 std::unique_ptr<JSONObject> result = JSONObject::create(); |
| 433 for (size_t i = 0; i < m_order.size(); ++i) { |
| 434 String key = m_order[i]; |
| 435 Dictionary::const_iterator value = m_data.find(key); |
| 436 DCHECK(value != m_data.end() && value->value); |
| 437 result->setValue(key, value->value->clone()); |
| 438 } |
| 439 return std::move(result); |
| 440 } |
| 441 |
| 382 JSONObject::JSONObject() | 442 JSONObject::JSONObject() |
| 383 : JSONValue(TypeObject) | 443 : JSONValue(TypeObject) |
| 384 , m_data() | 444 , m_data() |
| 385 , m_order() | 445 , m_order() |
| 386 { | 446 { |
| 387 } | 447 } |
| 388 | 448 |
| 389 JSONArray::~JSONArray() | 449 JSONArray::~JSONArray() |
| 390 { | 450 { |
| 391 } | 451 } |
| 392 | 452 |
| 393 void JSONArray::writeJSON(StringBuilder* output) const | 453 void JSONArray::writeJSON(StringBuilder* output) const |
| 394 { | 454 { |
| 395 output->append('['); | 455 output->append('['); |
| 396 for (Vector<RefPtr<JSONValue>>::const_iterator it = m_data.begin(); it != m_
data.end(); ++it) { | 456 bool first = true; |
| 397 if (it != m_data.begin()) | 457 for (const std::unique_ptr<JSONValue>& value : m_data) { |
| 458 if (!first) |
| 398 output->append(','); | 459 output->append(','); |
| 399 (*it)->writeJSON(output); | 460 value->writeJSON(output); |
| 461 first = false; |
| 400 } | 462 } |
| 401 output->append(']'); | 463 output->append(']'); |
| 402 } | 464 } |
| 403 | 465 |
| 404 void JSONArray::prettyWriteJSONInternal(StringBuilder* output, int depth) const | 466 void JSONArray::prettyWriteJSONInternal(StringBuilder* output, int depth) const |
| 405 { | 467 { |
| 406 output->append('['); | 468 output->append('['); |
| 469 bool first = true; |
| 407 bool lastInsertedNewLine = false; | 470 bool lastInsertedNewLine = false; |
| 408 for (Vector<RefPtr<JSONValue>>::const_iterator it = m_data.begin(); it != m_
data.end(); ++it) { | 471 for (const std::unique_ptr<JSONValue>& value : m_data) { |
| 409 bool insertNewLine = (*it)->getType() == JSONValue::TypeObject || (*it)-
>getType() == JSONValue::TypeArray || (*it)->getType() == JSONValue::TypeString; | 472 bool insertNewLine = value->type() == JSONValue::TypeObject || value->ty
pe() == JSONValue::TypeArray || value->type() == JSONValue::TypeString; |
| 410 if (it == m_data.begin()) { | 473 if (first) { |
| 411 if (insertNewLine) { | 474 if (insertNewLine) { |
| 412 output->append('\n'); | 475 output->append('\n'); |
| 413 writeIndent(depth + 1, output); | 476 writeIndent(depth + 1, output); |
| 414 } | 477 } |
| 478 first = false; |
| 415 } else { | 479 } else { |
| 416 output->append(','); | 480 output->append(','); |
| 417 if (lastInsertedNewLine) { | 481 if (lastInsertedNewLine) { |
| 418 output->append('\n'); | 482 output->append('\n'); |
| 419 writeIndent(depth + 1, output); | 483 writeIndent(depth + 1, output); |
| 420 } else { | 484 } else { |
| 421 output->append(' '); | 485 output->append(' '); |
| 422 } | 486 } |
| 423 } | 487 } |
| 424 (*it)->prettyWriteJSONInternal(output, depth + 1); | 488 value->prettyWriteJSONInternal(output, depth + 1); |
| 425 lastInsertedNewLine = insertNewLine; | 489 lastInsertedNewLine = insertNewLine; |
| 426 } | 490 } |
| 427 if (lastInsertedNewLine) { | 491 if (lastInsertedNewLine) { |
| 428 output->append('\n'); | 492 output->append('\n'); |
| 429 writeIndent(depth, output); | 493 writeIndent(depth, output); |
| 430 } | 494 } |
| 431 output->append(']'); | 495 output->append(']'); |
| 432 } | 496 } |
| 433 | 497 |
| 498 std::unique_ptr<JSONValue> JSONArray::clone() const |
| 499 { |
| 500 std::unique_ptr<JSONArray> result = JSONArray::create(); |
| 501 for (const std::unique_ptr<JSONValue>& value : m_data) |
| 502 result->pushValue(value->clone()); |
| 503 return std::move(result); |
| 504 } |
| 505 |
| 434 JSONArray::JSONArray() | 506 JSONArray::JSONArray() |
| 435 : JSONValue(TypeArray) | 507 : JSONValue(TypeArray) |
| 436 , m_data() | |
| 437 { | 508 { |
| 438 } | 509 } |
| 439 | 510 |
| 440 void JSONArray::pushBoolean(bool value) | 511 void JSONArray::pushBoolean(bool value) |
| 441 { | 512 { |
| 442 m_data.append(JSONBasicValue::create(value)); | 513 m_data.append(JSONBasicValue::create(value)); |
| 443 } | 514 } |
| 444 | 515 |
| 445 void JSONArray::pushInt(int value) | 516 void JSONArray::pushInteger(int value) |
| 446 { | 517 { |
| 447 m_data.append(JSONBasicValue::create(value)); | 518 m_data.append(JSONBasicValue::create(value)); |
| 448 } | 519 } |
| 449 | 520 |
| 450 void JSONArray::pushNumber(double value) | 521 void JSONArray::pushDouble(double value) |
| 451 { | 522 { |
| 452 m_data.append(JSONBasicValue::create(value)); | 523 m_data.append(JSONBasicValue::create(value)); |
| 453 } | 524 } |
| 454 | 525 |
| 455 void JSONArray::pushString(const String& value) | 526 void JSONArray::pushString(const String& value) |
| 456 { | 527 { |
| 457 m_data.append(JSONString::create(value)); | 528 m_data.append(JSONString::create(value)); |
| 458 } | 529 } |
| 459 | 530 |
| 460 void JSONArray::pushValue(PassRefPtr<JSONValue> value) | 531 void JSONArray::pushValue(std::unique_ptr<JSONValue> value) |
| 461 { | 532 { |
| 462 ASSERT(value); | 533 DCHECK(value); |
| 463 m_data.append(value); | 534 m_data.append(std::move(value)); |
| 464 } | 535 } |
| 465 | 536 |
| 466 void JSONArray::pushObject(PassRefPtr<JSONObject> value) | 537 void JSONArray::pushObject(std::unique_ptr<JSONObject> value) |
| 467 { | 538 { |
| 468 ASSERT(value); | 539 DCHECK(value); |
| 469 m_data.append(value); | 540 m_data.append(std::move(value)); |
| 470 } | 541 } |
| 471 | 542 |
| 472 void JSONArray::pushArray(PassRefPtr<JSONArray> value) | 543 void JSONArray::pushArray(std::unique_ptr<JSONArray> value) |
| 473 { | 544 { |
| 474 ASSERT(value); | 545 DCHECK(value); |
| 475 m_data.append(value); | 546 m_data.append(std::move(value)); |
| 476 } | 547 } |
| 477 | 548 |
| 478 PassRefPtr<JSONValue> JSONArray::get(size_t index) | 549 JSONValue* JSONArray::at(size_t index) |
| 479 { | 550 { |
| 480 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); | 551 DCHECK_LT(index, m_data.size()); |
| 481 return m_data[index]; | 552 return m_data[index].get(); |
| 482 } | 553 } |
| 483 | 554 |
| 484 } // namespace blink | 555 } // namespace blink |
| OLD | NEW |