1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
* Copyright (C) 2014 University of Washington. All rights reserved.
* Copyright (C) 2017-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <wtf/HashMap.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/WTFString.h>
namespace WTF {
class PrintStream;
// Make sure compiled symbols contain the WTF namespace prefix, but
// use a different inner namespace name so that JSON::Value is not ambigious.
// Otherwise, the compiler would have both WTF::JSON::Value and JSON::Value
// in scope and client code would have to use WTF::JSON::Value, which is tedious.
namespace JSONImpl {
class Array;
class ArrayBase;
class Object;
class ObjectBase;
// FIXME: unify this JSON parser with JSONParse in JavaScriptCore.
class WTF_EXPORT_PRIVATE Value : public RefCounted<Value> {
public:
static constexpr int maxDepth = 1000;
void operator delete(Value*, std::destroying_delete_t);
bool operator!() const;
~Value()
{
switch (m_type) {
case Type::Null:
case Type::Boolean:
case Type::Double:
case Type::Integer:
case Type::Object:
case Type::Array:
break;
case Type::String:
if (m_value.string)
m_value.string->deref();
break;
}
}
static Ref<Value> null();
static Ref<Value> create(bool);
static Ref<Value> create(int);
static Ref<Value> create(double);
static Ref<Value> create(const String&);
template<class T>
static Ref<Value> create(T) = delete;
enum class Type {
Null = 0,
Boolean,
Double,
Integer,
String,
Object,
Array,
};
Type type() const { return m_type; }
bool isNull() const { return m_type == Type::Null; }
std::optional<bool> asBoolean() const;
std::optional<int> asInteger() const;
std::optional<double> asDouble() const;
String asString() const;
RefPtr<Value> asValue();
RefPtr<Object> asObject();
RefPtr<const Object> asObject() const;
RefPtr<Array> asArray();
static RefPtr<Value> parseJSON(StringView);
String toJSONString() const;
void dump(PrintStream&) const;
// FIXME: <http://webkit.org/b/179847> remove these functions when legacy InspectorObject symbols are no longer needed.
bool asDouble(double&) const;
bool asInteger(int&) const;
bool asString(String&) const;
size_t memoryCost() const;
void writeJSON(StringBuilder& output) const;
protected:
Value()
: m_type { Type::Null }
{
}
explicit Value(Type type)
: m_type(type)
{
}
explicit Value(bool value)
: m_type { Type::Boolean }
{
m_value.boolean = value;
}
explicit Value(int value)
: m_type { Type::Integer }
{
m_value.number = static_cast<double>(value);
}
explicit Value(double value)
: m_type(Type::Double)
{
m_value.number = value;
}
explicit Value(const String& value)
: m_type { Type::String }
{
m_value.string = value.impl();
if (m_value.string)
m_value.string->ref();
}
template<typename Visitor> constexpr decltype(auto) visitDerived(Visitor&&);
template<typename Visitor> constexpr decltype(auto) visitDerived(Visitor&&) const;
size_t memoryCostImpl() const;
void writeJSONImpl(StringBuilder& output) const;
private:
Type m_type { Type::Null };
union {
bool boolean;
double number;
StringImpl* string;
} m_value;
};
class SUPPRESS_REFCOUNTED_WITHOUT_VIRTUAL_DESTRUCTOR ObjectBase : public Value {
private:
friend class Value;
using DataStorage = UncheckedKeyHashMap<String, Ref<Value>>;
using OrderStorage = Vector<String>;
public:
using iterator = DataStorage::iterator;
using const_iterator = DataStorage::const_iterator;
protected:
~ObjectBase();
// FIXME: use templates to reduce the amount of duplicated set*() methods.
void setBoolean(const String& name, bool);
void setInteger(const String& name, int);
void setDouble(const String& name, double);
void setString(const String& name, const String&);
void setValue(const String& name, Ref<Value>&&);
void setObject(const String& name, Ref<ObjectBase>&&);
void setArray(const String& name, Ref<ArrayBase>&&);
iterator find(const String& name);
const_iterator find(const String& name) const;
WTF_EXPORT_PRIVATE std::optional<bool> getBoolean(const String& name) const;
WTF_EXPORT_PRIVATE std::optional<double> getDouble(const String& name) const;
WTF_EXPORT_PRIVATE std::optional<int> getInteger(const String& name) const;
WTF_EXPORT_PRIVATE String getString(const String& name) const;
WTF_EXPORT_PRIVATE RefPtr<Object> getObject(const String& name) const;
WTF_EXPORT_PRIVATE RefPtr<Array> getArray(const String& name) const;
WTF_EXPORT_PRIVATE RefPtr<Value> getValue(const String& name) const;
WTF_EXPORT_PRIVATE void remove(const String& name);
iterator begin() { return m_map.begin(); }
iterator end() { return m_map.end(); }
const_iterator begin() const { return m_map.begin(); }
const_iterator end() const { return m_map.end(); }
OrderStorage keys() const { return m_order; }
unsigned size() const { return m_map.size(); }
// FIXME: <http://webkit.org/b/179847> remove these functions when legacy InspectorObject symbols are no longer needed.
bool getBoolean(const String& name, bool& output) const;
WTF_EXPORT_PRIVATE bool getString(const String& name, String& output) const;
bool getObject(const String& name, RefPtr<Object>& output) const;
bool getArray(const String& name, RefPtr<Array>& output) const;
WTF_EXPORT_PRIVATE bool getValue(const String& name, RefPtr<Value>& output) const;
protected:
ObjectBase();
private:
size_t memoryCostImpl() const;
void writeJSONImpl(StringBuilder& output) const;
DataStorage m_map;
OrderStorage m_order;
};
class Object : public ObjectBase {
public:
static WTF_EXPORT_PRIVATE Ref<Object> create();
// This class expected non-cyclic values, as we cannot serialize cycles in JSON.
using ObjectBase::setBoolean;
using ObjectBase::setInteger;
using ObjectBase::setDouble;
using ObjectBase::setString;
using ObjectBase::setValue;
using ObjectBase::setObject;
using ObjectBase::setArray;
using ObjectBase::find;
using ObjectBase::getBoolean;
using ObjectBase::getInteger;
using ObjectBase::getDouble;
using ObjectBase::getString;
using ObjectBase::getObject;
using ObjectBase::getArray;
using ObjectBase::getValue;
using ObjectBase::remove;
using ObjectBase::begin;
using ObjectBase::end;
using ObjectBase::keys;
using ObjectBase::size;
};
class SUPPRESS_REFCOUNTED_WITHOUT_VIRTUAL_DESTRUCTOR WTF_EXPORT_PRIVATE ArrayBase : public Value {
private:
friend class Value;
using DataStorage = Vector<Ref<Value>>;
public:
using iterator = DataStorage::iterator;
using const_iterator = DataStorage::const_iterator;
size_t length() const { return m_map.size(); }
Ref<Value> get(size_t index) const;
protected:
~ArrayBase();
void pushBoolean(bool);
void pushInteger(int);
void pushDouble(double);
void pushString(const String&);
void pushValue(Ref<Value>&&);
void pushObject(Ref<ObjectBase>&&);
void pushArray(Ref<ArrayBase>&&);
iterator begin() { return m_map.begin(); }
iterator end() { return m_map.end(); }
const_iterator begin() const { return m_map.begin(); }
const_iterator end() const { return m_map.end(); }
protected:
ArrayBase();
private:
size_t memoryCostImpl() const;
void writeJSONImpl(StringBuilder& output) const;
DataStorage m_map;
};
class Array final : public ArrayBase {
public:
static WTF_EXPORT_PRIVATE Ref<Array> create();
// This class expected non-cyclic values, as we cannot serialize cycles in JSON.
using ArrayBase::pushBoolean;
using ArrayBase::pushInteger;
using ArrayBase::pushDouble;
using ArrayBase::pushString;
using ArrayBase::pushValue;
using ArrayBase::pushObject;
using ArrayBase::pushArray;
using ArrayBase::get;
using ArrayBase::begin;
using ArrayBase::end;
};
inline ObjectBase::iterator ObjectBase::find(const String& name)
{
return m_map.find(name);
}
inline ObjectBase::const_iterator ObjectBase::find(const String& name) const
{
return m_map.find(name);
}
inline void ObjectBase::setBoolean(const String& name, bool value)
{
setValue(name, Value::create(value));
}
inline void ObjectBase::setInteger(const String& name, int value)
{
setValue(name, Value::create(value));
}
inline void ObjectBase::setDouble(const String& name, double value)
{
setValue(name, Value::create(value));
}
inline void ObjectBase::setString(const String& name, const String& value)
{
setValue(name, Value::create(value));
}
inline void ObjectBase::setValue(const String& name, Ref<Value>&& value)
{
if (m_map.set(name, WTFMove(value)).isNewEntry)
m_order.append(name);
}
inline void ObjectBase::setObject(const String& name, Ref<ObjectBase>&& value)
{
if (m_map.set(name, WTFMove(value)).isNewEntry)
m_order.append(name);
}
inline void ObjectBase::setArray(const String& name, Ref<ArrayBase>&& value)
{
if (m_map.set(name, WTFMove(value)).isNewEntry)
m_order.append(name);
}
inline void ArrayBase::pushBoolean(bool value)
{
m_map.append(Value::create(value));
}
inline void ArrayBase::pushInteger(int value)
{
m_map.append(Value::create(value));
}
inline void ArrayBase::pushDouble(double value)
{
m_map.append(Value::create(value));
}
inline void ArrayBase::pushString(const String& value)
{
m_map.append(Value::create(value));
}
inline void ArrayBase::pushValue(Ref<Value>&& value)
{
m_map.append(WTFMove(value));
}
inline void ArrayBase::pushObject(Ref<ObjectBase>&& value)
{
m_map.append(WTFMove(value));
}
inline void ArrayBase::pushArray(Ref<ArrayBase>&& value)
{
m_map.append(WTFMove(value));
}
template<typename T>
class ArrayOf final : public ArrayBase {
private:
ArrayOf() { }
Array& castedArray()
{
static_assert(sizeof(Array) == sizeof(ArrayOf<T>), "cannot cast");
return *static_cast<Array*>(static_cast<ArrayBase*>(this));
}
public:
template <typename V = T>
std::enable_if_t<std::is_same_v<bool, V> || std::is_same_v<Value, V>> addItem(bool value)
{
castedArray().pushBoolean(value);
}
template <typename V = T>
std::enable_if_t<std::is_same_v<int, V> || std::is_same_v<Value, V>> addItem(int value)
{
castedArray().pushInteger(value);
}
template <typename V = T>
std::enable_if_t<std::is_same_v<double, V> || std::is_same_v<Value, V>> addItem(double value)
{
castedArray().pushDouble(value);
}
template <typename V = T>
std::enable_if_t<std::is_same_v<String, V> || std::is_same_v<Value, V>> addItem(const String& value)
{
castedArray().pushString(value);
}
template <typename V = T>
std::enable_if_t<std::is_base_of_v<Value, V> && !std::is_base_of_v<ObjectBase, V> && !std::is_base_of_v<ArrayBase, V>> addItem(Ref<Value>&& value)
{
castedArray().pushValue(WTFMove(value));
}
template <typename V = T>
std::enable_if_t<std::is_base_of_v<ObjectBase, V>> addItem(Ref<ObjectBase>&& value)
{
castedArray().pushObject(WTFMove(value));
}
template <typename V = T>
std::enable_if_t<std::is_base_of_v<ArrayBase, V>> addItem(Ref<ArrayBase>&& value)
{
castedArray().pushArray(WTFMove(value));
}
static Ref<ArrayOf<T>> create()
{
return adoptRef(*new ArrayOf<T>());
}
using ArrayBase::get;
using ArrayBase::begin;
using ArrayBase::end;
};
inline bool Value::operator!() const
{
return isNull();
}
inline RefPtr<Value> Value::asValue()
{
return this;
}
inline RefPtr<Object> Value::asObject()
{
switch (m_type) {
case Type::Null:
case Type::Boolean:
case Type::Double:
case Type::Integer:
case Type::String:
case Type::Array:
return nullptr;
case Type::Object:
static_assert(sizeof(Object) == sizeof(ObjectBase));
return static_cast<Object*>(this);
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
inline RefPtr<const Object> Value::asObject() const
{
switch (m_type) {
case Type::Null:
case Type::Boolean:
case Type::Double:
case Type::Integer:
case Type::String:
case Type::Array:
return nullptr;
case Type::Object:
static_assert(sizeof(Object) == sizeof(ObjectBase));
return static_cast<const Object*>(this);
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
inline RefPtr<Array> Value::asArray()
{
switch (m_type) {
case Type::Null:
case Type::Boolean:
case Type::Double:
case Type::Integer:
case Type::Object:
case Type::String:
return nullptr;
case Type::Array:
static_assert(sizeof(ArrayBase) == sizeof(Array));
return static_cast<Array*>(this);
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
} // namespace JSONImpl
inline size_t containerSize(const JSONImpl::Array& array) { return array.length(); }
} // namespace WTF
namespace JSON {
using namespace WTF::JSONImpl;
}
|