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
|
/*
* Copyright (C) 2010 Google 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.
*/
#include "third_party/blink/renderer/platform/json/json_values.h"
#include <algorithm>
#include <cmath>
#include "base/notreached.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/decimal.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
namespace {
inline bool EscapeChar(UChar c, StringBuilder* dst) {
switch (c) {
case '\b':
dst->Append("\\b");
break;
case '\f':
dst->Append("\\f");
break;
case '\n':
dst->Append("\\n");
break;
case '\r':
dst->Append("\\r");
break;
case '\t':
dst->Append("\\t");
break;
case '\\':
dst->Append("\\\\");
break;
case '"':
dst->Append("\\\"");
break;
default:
return false;
}
return true;
}
void AppendUnsignedAsHex(UChar number, StringBuilder* dst) {
constexpr auto kHexDigits = base::span_from_cstring("0123456789ABCDEF");
dst->Append("\\u");
for (size_t i = 0; i < 4; ++i) {
dst->Append(kHexDigits[(number & 0xF000) >> 12]);
number <<= 4;
}
}
void WriteIndent(int depth, StringBuilder* output) {
for (int i = 0; i < depth; ++i)
output->Append(" ");
}
} // anonymous namespace
void EscapeStringForJSON(const String& str, StringBuilder* dst) {
for (unsigned i = 0; i < str.length(); ++i) {
UChar c = str[i];
if (!EscapeChar(c, dst)) {
if (c < 32 || c == '<' || c == '>') {
// 1. Escaping <, > to prevent script execution.
AppendUnsignedAsHex(c, dst);
} else {
dst->Append(c);
}
}
}
}
void DoubleQuoteStringForJSON(const String& str, StringBuilder* dst) {
dst->Append('"');
EscapeStringForJSON(str, dst);
dst->Append('"');
}
String JSONValue::QuoteString(const String& input) {
StringBuilder builder;
DoubleQuoteStringForJSON(input, &builder);
return builder.ToString();
}
bool JSONValue::AsBoolean(bool*) const {
return false;
}
bool JSONValue::AsDouble(double*) const {
return false;
}
bool JSONValue::AsInteger(int*) const {
return false;
}
bool JSONValue::AsString(String*) const {
return false;
}
String JSONValue::ToJSONString() const {
StringBuilder result;
result.ReserveCapacity(512);
WriteJSON(&result);
return result.ToString();
}
String JSONValue::ToPrettyJSONString() const {
StringBuilder result;
result.ReserveCapacity(512);
PrettyWriteJSON(&result);
return result.ToString();
}
void JSONValue::WriteJSON(StringBuilder* output) const {
DCHECK(type_ == kTypeNull);
output->Append(base::byte_span_from_cstring(kJSONNullString));
}
void JSONValue::PrettyWriteJSON(StringBuilder* output) const {
PrettyWriteJSONInternal(output, 0);
output->Append('\n');
}
void JSONValue::PrettyWriteJSONInternal(StringBuilder* output,
int depth) const {
WriteJSON(output);
}
std::unique_ptr<JSONValue> JSONValue::Clone() const {
return JSONValue::Null();
}
bool JSONBasicValue::AsBoolean(bool* output) const {
if (GetType() != kTypeBoolean)
return false;
*output = bool_value_;
return true;
}
bool JSONBasicValue::AsDouble(double* output) const {
if (GetType() == kTypeDouble) {
*output = double_value_;
return true;
}
if (GetType() == kTypeInteger) {
*output = integer_value_;
return true;
}
return false;
}
bool JSONBasicValue::AsInteger(int* output) const {
if (GetType() != kTypeInteger)
return false;
*output = integer_value_;
return true;
}
void JSONBasicValue::WriteJSON(StringBuilder* output) const {
DCHECK(GetType() == kTypeBoolean || GetType() == kTypeInteger ||
GetType() == kTypeDouble);
if (GetType() == kTypeBoolean) {
if (bool_value_)
output->Append(base::byte_span_from_cstring(kJSONTrueString));
else
output->Append(base::byte_span_from_cstring(kJSONFalseString));
} else if (GetType() == kTypeDouble) {
if (!std::isfinite(double_value_)) {
output->Append(base::byte_span_from_cstring(kJSONNullString));
return;
}
output->Append(Decimal::FromDouble(double_value_).ToString());
} else if (GetType() == kTypeInteger) {
output->Append(String::Number(integer_value_));
}
}
std::unique_ptr<JSONValue> JSONBasicValue::Clone() const {
switch (GetType()) {
case kTypeDouble:
return std::make_unique<JSONBasicValue>(double_value_);
case kTypeInteger:
return std::make_unique<JSONBasicValue>(integer_value_);
case kTypeBoolean:
return std::make_unique<JSONBasicValue>(bool_value_);
default:
NOTREACHED();
}
}
bool JSONString::AsString(String* output) const {
*output = string_value_;
return true;
}
void JSONString::WriteJSON(StringBuilder* output) const {
DCHECK(GetType() == kTypeString);
DoubleQuoteStringForJSON(string_value_, output);
}
std::unique_ptr<JSONValue> JSONString::Clone() const {
return std::make_unique<JSONString>(string_value_);
}
JSONObject::~JSONObject() = default;
bool JSONObject::SetBoolean(const String& name, bool value) {
return SetValue(name, std::make_unique<JSONBasicValue>(value));
}
bool JSONObject::SetInteger(const String& name, int value) {
return SetValue(name, std::make_unique<JSONBasicValue>(value));
}
bool JSONObject::SetDouble(const String& name, double value) {
return SetValue(name, std::make_unique<JSONBasicValue>(value));
}
bool JSONObject::SetString(const String& name, const String& value) {
return SetValue(name, std::make_unique<JSONString>(value));
}
bool JSONObject::SetValue(const String& name,
std::unique_ptr<JSONValue> value) {
return Set(name, value);
}
bool JSONObject::SetObject(const String& name,
std::unique_ptr<JSONObject> value) {
return Set(name, value);
}
bool JSONObject::SetArray(const String& name,
std::unique_ptr<JSONArray> value) {
return Set(name, value);
}
bool JSONObject::GetBoolean(const String& name, bool* output) const {
JSONValue* value = Get(name);
if (!value)
return false;
return value->AsBoolean(output);
}
bool JSONObject::GetInteger(const String& name, int* output) const {
JSONValue* value = Get(name);
if (!value)
return false;
return value->AsInteger(output);
}
bool JSONObject::GetDouble(const String& name, double* output) const {
JSONValue* value = Get(name);
if (!value)
return false;
return value->AsDouble(output);
}
bool JSONObject::GetString(const String& name, String* output) const {
JSONValue* value = Get(name);
if (!value)
return false;
return value->AsString(output);
}
JSONObject* JSONObject::GetJSONObject(const String& name) const {
return JSONObject::Cast(Get(name));
}
JSONArray* JSONObject::GetArray(const String& name) const {
return JSONArray::Cast(Get(name));
}
JSONValue* JSONObject::Get(const String& name) const {
Dictionary::const_iterator it = data_.find(name);
if (it == data_.end())
return nullptr;
return it->value.get();
}
JSONObject::Entry JSONObject::at(wtf_size_t index) const {
const String key = order_[index];
return std::make_pair(key, data_.find(key)->value.get());
}
bool JSONObject::BooleanProperty(const String& name, bool default_value) const {
bool result = default_value;
GetBoolean(name, &result);
return result;
}
int JSONObject::IntegerProperty(const String& name, int default_value) const {
int result = default_value;
GetInteger(name, &result);
return result;
}
double JSONObject::DoubleProperty(const String& name,
double default_value) const {
double result = default_value;
GetDouble(name, &result);
return result;
}
void JSONObject::Remove(const String& name) {
data_.erase(name);
for (wtf_size_t i = 0; i < order_.size(); ++i) {
if (order_[i] == name) {
order_.EraseAt(i);
break;
}
}
}
void JSONObject::WriteJSON(StringBuilder* output) const {
output->Append('{');
for (wtf_size_t i = 0; i < order_.size(); ++i) {
Dictionary::const_iterator it = data_.find(order_[i]);
CHECK(it != data_.end());
if (i)
output->Append(',');
DoubleQuoteStringForJSON(it->key, output);
output->Append(':');
it->value->WriteJSON(output);
}
output->Append('}');
}
void JSONObject::PrettyWriteJSONInternal(StringBuilder* output,
int depth) const {
output->Append("{\n");
for (wtf_size_t i = 0; i < order_.size(); ++i) {
Dictionary::const_iterator it = data_.find(order_[i]);
CHECK(it != data_.end());
if (i)
output->Append(",\n");
WriteIndent(depth + 1, output);
DoubleQuoteStringForJSON(it->key, output);
output->Append(": ");
it->value->PrettyWriteJSONInternal(output, depth + 1);
}
output->Append('\n');
WriteIndent(depth, output);
output->Append('}');
}
std::unique_ptr<JSONValue> JSONObject::Clone() const {
auto result = std::make_unique<JSONObject>();
for (const String& key : order_) {
Dictionary::const_iterator value = data_.find(key);
DCHECK(value != data_.end() && value->value);
result->SetValue(key, value->value->Clone());
}
return std::move(result);
}
JSONObject::JSONObject() : JSONValue(kTypeObject), data_(), order_() {}
JSONArray::~JSONArray() = default;
void JSONArray::WriteJSON(StringBuilder* output) const {
output->Append('[');
bool first = true;
for (const std::unique_ptr<JSONValue>& value : data_) {
if (!first)
output->Append(',');
value->WriteJSON(output);
first = false;
}
output->Append(']');
}
void JSONArray::PrettyWriteJSONInternal(StringBuilder* output,
int depth) const {
output->Append('[');
bool first = true;
bool last_inserted_new_line = false;
for (const std::unique_ptr<JSONValue>& value : data_) {
bool insert_new_line = value->GetType() == JSONValue::kTypeObject ||
value->GetType() == JSONValue::kTypeArray ||
value->GetType() == JSONValue::kTypeString;
if (first) {
if (insert_new_line) {
output->Append('\n');
WriteIndent(depth + 1, output);
}
first = false;
} else {
output->Append(',');
if (last_inserted_new_line) {
output->Append('\n');
WriteIndent(depth + 1, output);
} else {
output->Append(' ');
}
}
value->PrettyWriteJSONInternal(output, depth + 1);
last_inserted_new_line = insert_new_line;
}
if (last_inserted_new_line) {
output->Append('\n');
WriteIndent(depth, output);
}
output->Append(']');
}
std::unique_ptr<JSONValue> JSONArray::Clone() const {
auto result = std::make_unique<JSONArray>();
for (const std::unique_ptr<JSONValue>& value : data_)
result->PushValue(value->Clone());
return std::move(result);
}
JSONArray::JSONArray() : JSONValue(kTypeArray) {}
void JSONArray::PushBoolean(bool value) {
data_.push_back(std::make_unique<JSONBasicValue>(value));
}
void JSONArray::PushInteger(int value) {
data_.push_back(std::make_unique<JSONBasicValue>(value));
}
void JSONArray::PushDouble(double value) {
data_.push_back(std::make_unique<JSONBasicValue>(value));
}
void JSONArray::PushString(const String& value) {
data_.push_back(std::make_unique<JSONString>(value));
}
void JSONArray::PushValue(std::unique_ptr<JSONValue> value) {
DCHECK(value);
data_.push_back(std::move(value));
}
void JSONArray::PushObject(std::unique_ptr<JSONObject> value) {
DCHECK(value);
data_.push_back(std::move(value));
}
void JSONArray::PushArray(std::unique_ptr<JSONArray> value) {
DCHECK(value);
data_.push_back(std::move(value));
}
JSONValue* JSONArray::at(wtf_size_t index) const {
DCHECK_LT(index, data_.size());
return data_[index].get();
}
} // namespace blink
|