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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 bool JSONValue::asNumber(unsigned*) const | 127 bool JSONValue::asNumber(unsigned*) const |
128 { | 128 { |
129 return false; | 129 return false; |
130 } | 130 } |
131 | 131 |
132 bool JSONValue::asString(String*) const | 132 bool JSONValue::asString(String*) const |
133 { | 133 { |
134 return false; | 134 return false; |
135 } | 135 } |
136 | 136 |
137 bool JSONValue::asObject(RefPtr<JSONObject>*) | |
138 { | |
139 return false; | |
140 } | |
141 | |
142 bool JSONValue::asArray(RefPtr<JSONArray>*) | |
143 { | |
144 return false; | |
145 } | |
146 | |
147 PassRefPtr<JSONObject> JSONValue::asObject() | |
148 { | |
149 return nullptr; | |
150 } | |
151 | |
152 PassRefPtr<JSONArray> JSONValue::asArray() | |
153 { | |
154 return nullptr; | |
155 } | |
156 | |
157 String JSONValue::toJSONString() const | 137 String JSONValue::toJSONString() const |
158 { | 138 { |
159 StringBuilder result; | 139 StringBuilder result; |
160 result.reserveCapacity(512); | 140 result.reserveCapacity(512); |
161 writeJSON(&result); | 141 writeJSON(&result); |
162 return result.toString(); | 142 return result.toString(); |
163 } | 143 } |
164 | 144 |
165 String JSONValue::toPrettyJSONString() const | 145 String JSONValue::toPrettyJSONString() const |
166 { | 146 { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 *output = m_stringValue; | 237 *output = m_stringValue; |
258 return true; | 238 return true; |
259 } | 239 } |
260 | 240 |
261 void JSONString::writeJSON(StringBuilder* output) const | 241 void JSONString::writeJSON(StringBuilder* output) const |
262 { | 242 { |
263 ASSERT(type() == TypeString); | 243 ASSERT(type() == TypeString); |
264 doubleQuoteStringForJSON(m_stringValue, output); | 244 doubleQuoteStringForJSON(m_stringValue, output); |
265 } | 245 } |
266 | 246 |
267 JSONObjectBase::~JSONObjectBase() | 247 JSONObject::~JSONObject() |
268 { | 248 { |
269 } | 249 } |
270 | 250 |
271 bool JSONObjectBase::asObject(RefPtr<JSONObject>* output) | 251 void JSONObject::setBoolean(const String& name, bool value) |
272 { | |
273 static_assert(sizeof(JSONObject) == sizeof(JSONObjectBase), "cannot cast"); | |
274 *output = static_cast<JSONObject*>(this); | |
275 return true; | |
276 } | |
277 | |
278 PassRefPtr<JSONObject> JSONObjectBase::asObject() | |
279 { | |
280 return openAccessors(); | |
281 } | |
282 | |
283 void JSONObjectBase::setBoolean(const String& name, bool value) | |
284 { | 252 { |
285 setValue(name, JSONBasicValue::create(value)); | 253 setValue(name, JSONBasicValue::create(value)); |
286 } | 254 } |
287 | 255 |
288 void JSONObjectBase::setNumber(const String& name, double value) | 256 void JSONObject::setNumber(const String& name, double value) |
289 { | 257 { |
290 setValue(name, JSONBasicValue::create(value)); | 258 setValue(name, JSONBasicValue::create(value)); |
291 } | 259 } |
292 | 260 |
293 void JSONObjectBase::setString(const String& name, const String& value) | 261 void JSONObject::setString(const String& name, const String& value) |
294 { | 262 { |
295 setValue(name, JSONString::create(value)); | 263 setValue(name, JSONString::create(value)); |
296 } | 264 } |
297 | 265 |
298 void JSONObjectBase::setValue(const String& name, PassRefPtr<JSONValue> value) | 266 void JSONObject::setValue(const String& name, PassRefPtr<JSONValue> value) |
299 { | 267 { |
300 ASSERT(value); | 268 ASSERT(value); |
301 if (m_data.set(name, value).isNewEntry) | 269 if (m_data.set(name, value).isNewEntry) |
302 m_order.append(name); | 270 m_order.append(name); |
303 } | 271 } |
304 | 272 |
305 void JSONObjectBase::setObject(const String& name, PassRefPtr<JSONObject> value) | 273 void JSONObject::setObject(const String& name, PassRefPtr<JSONObject> value) |
306 { | 274 { |
307 ASSERT(value); | 275 ASSERT(value); |
308 if (m_data.set(name, value).isNewEntry) | 276 if (m_data.set(name, value).isNewEntry) |
309 m_order.append(name); | 277 m_order.append(name); |
310 } | 278 } |
311 | 279 |
312 void JSONObjectBase::setArray(const String& name, PassRefPtr<JSONArray> value) | 280 void JSONObject::setArray(const String& name, PassRefPtr<JSONArray> value) |
313 { | 281 { |
314 ASSERT(value); | 282 ASSERT(value); |
315 if (m_data.set(name, value).isNewEntry) | 283 if (m_data.set(name, value).isNewEntry) |
316 m_order.append(name); | 284 m_order.append(name); |
317 } | 285 } |
318 | 286 |
319 JSONObject* JSONObjectBase::openAccessors() | 287 JSONObject::iterator JSONObject::find(const String& name) |
320 { | |
321 static_assert(sizeof(JSONObject) == sizeof(JSONObjectBase), "cannot cast"); | |
322 return static_cast<JSONObject*>(this); | |
323 } | |
324 | |
325 JSONObjectBase::iterator JSONObjectBase::find(const String& name) | |
326 { | 288 { |
327 return m_data.find(name); | 289 return m_data.find(name); |
328 } | 290 } |
329 | 291 |
330 JSONObjectBase::const_iterator JSONObjectBase::find(const String& name) const | 292 JSONObject::const_iterator JSONObject::find(const String& name) const |
331 { | 293 { |
332 return m_data.find(name); | 294 return m_data.find(name); |
333 } | 295 } |
334 | 296 |
335 bool JSONObjectBase::getBoolean(const String& name, bool* output) const | 297 bool JSONObject::getBoolean(const String& name, bool* output) const |
336 { | 298 { |
337 RefPtr<JSONValue> value = get(name); | 299 RefPtr<JSONValue> value = get(name); |
338 if (!value) | 300 if (!value) |
339 return false; | 301 return false; |
340 return value->asBoolean(output); | 302 return value->asBoolean(output); |
341 } | 303 } |
342 | 304 |
343 bool JSONObjectBase::getString(const String& name, String* output) const | 305 bool JSONObject::getString(const String& name, String* output) const |
344 { | 306 { |
345 RefPtr<JSONValue> value = get(name); | 307 RefPtr<JSONValue> value = get(name); |
346 if (!value) | 308 if (!value) |
347 return false; | 309 return false; |
348 return value->asString(output); | 310 return value->asString(output); |
349 } | 311 } |
350 | 312 |
351 PassRefPtr<JSONObject> JSONObjectBase::getObject(const String& name) const | 313 PassRefPtr<JSONObject> JSONObject::getObject(const String& name) const |
352 { | 314 { |
353 RefPtr<JSONValue> value = get(name); | 315 return JSONObject::cast(get(name)); |
354 if (!value) | |
355 return nullptr; | |
356 return value->asObject(); | |
357 } | 316 } |
358 | 317 |
359 PassRefPtr<JSONArray> JSONObjectBase::getArray(const String& name) const | 318 PassRefPtr<JSONArray> JSONObject::getArray(const String& name) const |
360 { | 319 { |
361 RefPtr<JSONValue> value = get(name); | 320 return JSONArray::cast(get(name)); |
362 if (!value) | |
363 return nullptr; | |
364 return value->asArray(); | |
365 } | 321 } |
366 | 322 |
367 PassRefPtr<JSONValue> JSONObjectBase::get(const String& name) const | 323 PassRefPtr<JSONValue> JSONObject::get(const String& name) const |
368 { | 324 { |
369 Dictionary::const_iterator it = m_data.find(name); | 325 Dictionary::const_iterator it = m_data.find(name); |
370 if (it == m_data.end()) | 326 if (it == m_data.end()) |
371 return nullptr; | 327 return nullptr; |
372 return it->value; | 328 return it->value; |
373 } | 329 } |
374 | 330 |
375 bool JSONObjectBase::booleanProperty(const String& name, bool defaultValue) cons
t | 331 bool JSONObject::booleanProperty(const String& name, bool defaultValue) const |
376 { | 332 { |
377 bool result = defaultValue; | 333 bool result = defaultValue; |
378 getBoolean(name, &result); | 334 getBoolean(name, &result); |
379 return result; | 335 return result; |
380 } | 336 } |
381 | 337 |
382 void JSONObjectBase::remove(const String& name) | 338 void JSONObject::remove(const String& name) |
383 { | 339 { |
384 m_data.remove(name); | 340 m_data.remove(name); |
385 for (size_t i = 0; i < m_order.size(); ++i) { | 341 for (size_t i = 0; i < m_order.size(); ++i) { |
386 if (m_order[i] == name) { | 342 if (m_order[i] == name) { |
387 m_order.remove(i); | 343 m_order.remove(i); |
388 break; | 344 break; |
389 } | 345 } |
390 } | 346 } |
391 } | 347 } |
392 | 348 |
393 void JSONObjectBase::writeJSON(StringBuilder* output) const | 349 void JSONObject::writeJSON(StringBuilder* output) const |
394 { | 350 { |
395 output->append('{'); | 351 output->append('{'); |
396 for (size_t i = 0; i < m_order.size(); ++i) { | 352 for (size_t i = 0; i < m_order.size(); ++i) { |
397 Dictionary::const_iterator it = m_data.find(m_order[i]); | 353 Dictionary::const_iterator it = m_data.find(m_order[i]); |
398 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); | 354 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); |
399 if (i) | 355 if (i) |
400 output->append(','); | 356 output->append(','); |
401 doubleQuoteStringForJSON(it->key, output); | 357 doubleQuoteStringForJSON(it->key, output); |
402 output->append(':'); | 358 output->append(':'); |
403 it->value->writeJSON(output); | 359 it->value->writeJSON(output); |
404 } | 360 } |
405 output->append('}'); | 361 output->append('}'); |
406 } | 362 } |
407 | 363 |
408 void JSONObjectBase::prettyWriteJSONInternal(StringBuilder* output, int depth) c
onst | 364 void JSONObject::prettyWriteJSONInternal(StringBuilder* output, int depth) const |
409 { | 365 { |
410 output->appendLiteral("{\n"); | 366 output->appendLiteral("{\n"); |
411 for (size_t i = 0; i < m_order.size(); ++i) { | 367 for (size_t i = 0; i < m_order.size(); ++i) { |
412 Dictionary::const_iterator it = m_data.find(m_order[i]); | 368 Dictionary::const_iterator it = m_data.find(m_order[i]); |
413 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); | 369 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); |
414 if (i) | 370 if (i) |
415 output->appendLiteral(",\n"); | 371 output->appendLiteral(",\n"); |
416 writeIndent(depth + 1, output); | 372 writeIndent(depth + 1, output); |
417 doubleQuoteStringForJSON(it->key, output); | 373 doubleQuoteStringForJSON(it->key, output); |
418 output->appendLiteral(": "); | 374 output->appendLiteral(": "); |
419 it->value->prettyWriteJSONInternal(output, depth + 1); | 375 it->value->prettyWriteJSONInternal(output, depth + 1); |
420 } | 376 } |
421 output->append('\n'); | 377 output->append('\n'); |
422 writeIndent(depth, output); | 378 writeIndent(depth, output); |
423 output->append('}'); | 379 output->append('}'); |
424 } | 380 } |
425 | 381 |
426 JSONObjectBase::JSONObjectBase() | 382 JSONObject::JSONObject() |
427 : JSONValue(TypeObject) | 383 : JSONValue(TypeObject) |
428 , m_data() | 384 , m_data() |
429 , m_order() | 385 , m_order() |
430 { | 386 { |
431 } | 387 } |
432 | 388 |
433 JSONArrayBase::~JSONArrayBase() | 389 JSONArray::~JSONArray() |
434 { | 390 { |
435 } | 391 } |
436 | 392 |
437 bool JSONArrayBase::asArray(RefPtr<JSONArray>* output) | 393 void JSONArray::writeJSON(StringBuilder* output) const |
438 { | |
439 static_assert(sizeof(JSONArrayBase) == sizeof(JSONArray), "cannot cast"); | |
440 *output = static_cast<JSONArray*>(this); | |
441 return true; | |
442 } | |
443 | |
444 PassRefPtr<JSONArray> JSONArrayBase::asArray() | |
445 { | |
446 static_assert(sizeof(JSONArrayBase) == sizeof(JSONArray), "cannot cast"); | |
447 return static_cast<JSONArray*>(this); | |
448 } | |
449 | |
450 void JSONArrayBase::writeJSON(StringBuilder* output) const | |
451 { | 394 { |
452 output->append('['); | 395 output->append('['); |
453 for (Vector<RefPtr<JSONValue>>::const_iterator it = m_data.begin(); it != m_
data.end(); ++it) { | 396 for (Vector<RefPtr<JSONValue>>::const_iterator it = m_data.begin(); it != m_
data.end(); ++it) { |
454 if (it != m_data.begin()) | 397 if (it != m_data.begin()) |
455 output->append(','); | 398 output->append(','); |
456 (*it)->writeJSON(output); | 399 (*it)->writeJSON(output); |
457 } | 400 } |
458 output->append(']'); | 401 output->append(']'); |
459 } | 402 } |
460 | 403 |
461 void JSONArrayBase::prettyWriteJSONInternal(StringBuilder* output, int depth) co
nst | 404 void JSONArray::prettyWriteJSONInternal(StringBuilder* output, int depth) const |
462 { | 405 { |
463 output->append('['); | 406 output->append('['); |
464 bool lastInsertedNewLine = false; | 407 bool lastInsertedNewLine = false; |
465 for (Vector<RefPtr<JSONValue>>::const_iterator it = m_data.begin(); it != m_
data.end(); ++it) { | 408 for (Vector<RefPtr<JSONValue>>::const_iterator it = m_data.begin(); it != m_
data.end(); ++it) { |
466 bool insertNewLine = (*it)->type() == JSONValue::TypeObject || (*it)->ty
pe() == JSONValue::TypeArray || (*it)->type() == JSONValue::TypeString; | 409 bool insertNewLine = (*it)->type() == JSONValue::TypeObject || (*it)->ty
pe() == JSONValue::TypeArray || (*it)->type() == JSONValue::TypeString; |
467 if (it == m_data.begin()) { | 410 if (it == m_data.begin()) { |
468 if (insertNewLine) { | 411 if (insertNewLine) { |
469 output->append('\n'); | 412 output->append('\n'); |
470 writeIndent(depth + 1, output); | 413 writeIndent(depth + 1, output); |
471 } | 414 } |
472 } else { | 415 } else { |
473 output->append(','); | 416 output->append(','); |
474 if (lastInsertedNewLine) { | 417 if (lastInsertedNewLine) { |
475 output->append('\n'); | 418 output->append('\n'); |
476 writeIndent(depth + 1, output); | 419 writeIndent(depth + 1, output); |
477 } else { | 420 } else { |
478 output->append(' '); | 421 output->append(' '); |
479 } | 422 } |
480 } | 423 } |
481 (*it)->prettyWriteJSONInternal(output, depth + 1); | 424 (*it)->prettyWriteJSONInternal(output, depth + 1); |
482 lastInsertedNewLine = insertNewLine; | 425 lastInsertedNewLine = insertNewLine; |
483 } | 426 } |
484 if (lastInsertedNewLine) { | 427 if (lastInsertedNewLine) { |
485 output->append('\n'); | 428 output->append('\n'); |
486 writeIndent(depth, output); | 429 writeIndent(depth, output); |
487 } | 430 } |
488 output->append(']'); | 431 output->append(']'); |
489 } | 432 } |
490 | 433 |
491 JSONArrayBase::JSONArrayBase() | 434 JSONArray::JSONArray() |
492 : JSONValue(TypeArray) | 435 : JSONValue(TypeArray) |
493 , m_data() | 436 , m_data() |
494 { | 437 { |
495 } | 438 } |
496 | 439 |
497 void JSONArrayBase::pushBoolean(bool value) | 440 void JSONArray::pushBoolean(bool value) |
498 { | 441 { |
499 m_data.append(JSONBasicValue::create(value)); | 442 m_data.append(JSONBasicValue::create(value)); |
500 } | 443 } |
501 | 444 |
502 void JSONArrayBase::pushInt(int value) | 445 void JSONArray::pushInt(int value) |
503 { | 446 { |
504 m_data.append(JSONBasicValue::create(value)); | 447 m_data.append(JSONBasicValue::create(value)); |
505 } | 448 } |
506 | 449 |
507 void JSONArrayBase::pushNumber(double value) | 450 void JSONArray::pushNumber(double value) |
508 { | 451 { |
509 m_data.append(JSONBasicValue::create(value)); | 452 m_data.append(JSONBasicValue::create(value)); |
510 } | 453 } |
511 | 454 |
512 void JSONArrayBase::pushString(const String& value) | 455 void JSONArray::pushString(const String& value) |
513 { | 456 { |
514 m_data.append(JSONString::create(value)); | 457 m_data.append(JSONString::create(value)); |
515 } | 458 } |
516 | 459 |
517 void JSONArrayBase::pushValue(PassRefPtr<JSONValue> value) | 460 void JSONArray::pushValue(PassRefPtr<JSONValue> value) |
518 { | 461 { |
519 ASSERT(value); | 462 ASSERT(value); |
520 m_data.append(value); | 463 m_data.append(value); |
521 } | 464 } |
522 | 465 |
523 void JSONArrayBase::pushObject(PassRefPtr<JSONObject> value) | 466 void JSONArray::pushObject(PassRefPtr<JSONObject> value) |
524 { | 467 { |
525 ASSERT(value); | 468 ASSERT(value); |
526 m_data.append(value); | 469 m_data.append(value); |
527 } | 470 } |
528 | 471 |
529 void JSONArrayBase::pushArray(PassRefPtr<JSONArray> value) | 472 void JSONArray::pushArray(PassRefPtr<JSONArray> value) |
530 { | 473 { |
531 ASSERT(value); | 474 ASSERT(value); |
532 m_data.append(value); | 475 m_data.append(value); |
533 } | 476 } |
534 | 477 |
535 PassRefPtr<JSONValue> JSONArrayBase::get(size_t index) | 478 PassRefPtr<JSONValue> JSONArray::get(size_t index) |
536 { | 479 { |
537 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); | 480 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); |
538 return m_data[index]; | 481 return m_data[index]; |
539 } | 482 } |
540 | 483 |
541 } // namespace blink | 484 } // namespace blink |
OLD | NEW |