Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: third_party/WebKit/Source/platform/JSONValues.cpp

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

Powered by Google App Engine
This is Rietveld 408576698