16d528ed9Sopenharmony_ci// Copyright (c) 2012 The Chromium Authors. All rights reserved. 26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 36d528ed9Sopenharmony_ci// found in the LICENSE file. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci#include "base/json/json_reader.h" 66d528ed9Sopenharmony_ci 76d528ed9Sopenharmony_ci#include <utility> 86d528ed9Sopenharmony_ci#include <vector> 96d528ed9Sopenharmony_ci 106d528ed9Sopenharmony_ci#include "base/json/json_parser.h" 116d528ed9Sopenharmony_ci#include "base/logging.h" 126d528ed9Sopenharmony_ci#include "base/values.h" 136d528ed9Sopenharmony_ci 146d528ed9Sopenharmony_cinamespace base { 156d528ed9Sopenharmony_ci 166d528ed9Sopenharmony_ci// Chosen to support 99.9% of documents found in the wild late 2016. 176d528ed9Sopenharmony_ci// http://crbug.com/673263 186d528ed9Sopenharmony_ciconst int JSONReader::kStackMaxDepth = 200; 196d528ed9Sopenharmony_ci 206d528ed9Sopenharmony_ci// Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. 216d528ed9Sopenharmony_cistatic_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, 226d528ed9Sopenharmony_ci "JSONReader error out of bounds"); 236d528ed9Sopenharmony_ci 246d528ed9Sopenharmony_ciconst char JSONReader::kInvalidEscape[] = "Invalid escape sequence."; 256d528ed9Sopenharmony_ciconst char JSONReader::kSyntaxError[] = "Syntax error."; 266d528ed9Sopenharmony_ciconst char JSONReader::kUnexpectedToken[] = "Unexpected token."; 276d528ed9Sopenharmony_ciconst char JSONReader::kTrailingComma[] = "Trailing comma not allowed."; 286d528ed9Sopenharmony_ciconst char JSONReader::kTooMuchNesting[] = "Too much nesting."; 296d528ed9Sopenharmony_ciconst char JSONReader::kUnexpectedDataAfterRoot[] = 306d528ed9Sopenharmony_ci "Unexpected data after root element."; 316d528ed9Sopenharmony_ciconst char JSONReader::kUnsupportedEncoding[] = 326d528ed9Sopenharmony_ci "Unsupported encoding. JSON must be UTF-8."; 336d528ed9Sopenharmony_ciconst char JSONReader::kUnquotedDictionaryKey[] = 346d528ed9Sopenharmony_ci "Dictionary keys must be quoted."; 356d528ed9Sopenharmony_ciconst char JSONReader::kInputTooLarge[] = "Input string is too large (>2GB)."; 366d528ed9Sopenharmony_ci 376d528ed9Sopenharmony_ciJSONReader::JSONReader(int options, int max_depth) 386d528ed9Sopenharmony_ci : parser_(new internal::JSONParser(options, max_depth)) {} 396d528ed9Sopenharmony_ci 406d528ed9Sopenharmony_ciJSONReader::~JSONReader() = default; 416d528ed9Sopenharmony_ci 426d528ed9Sopenharmony_ci// static 436d528ed9Sopenharmony_cistd::unique_ptr<Value> JSONReader::Read(std::string_view json, 446d528ed9Sopenharmony_ci int options, 456d528ed9Sopenharmony_ci int max_depth) { 466d528ed9Sopenharmony_ci internal::JSONParser parser(options, max_depth); 476d528ed9Sopenharmony_ci std::optional<Value> root = parser.Parse(json); 486d528ed9Sopenharmony_ci return root ? std::make_unique<Value>(std::move(*root)) : nullptr; 496d528ed9Sopenharmony_ci} 506d528ed9Sopenharmony_ci 516d528ed9Sopenharmony_ci// static 526d528ed9Sopenharmony_cistd::unique_ptr<Value> JSONReader::ReadAndReturnError( 536d528ed9Sopenharmony_ci std::string_view json, 546d528ed9Sopenharmony_ci int options, 556d528ed9Sopenharmony_ci int* error_code_out, 566d528ed9Sopenharmony_ci std::string* error_msg_out, 576d528ed9Sopenharmony_ci int* error_line_out, 586d528ed9Sopenharmony_ci int* error_column_out) { 596d528ed9Sopenharmony_ci internal::JSONParser parser(options); 606d528ed9Sopenharmony_ci std::optional<Value> root = parser.Parse(json); 616d528ed9Sopenharmony_ci if (!root) { 626d528ed9Sopenharmony_ci if (error_code_out) 636d528ed9Sopenharmony_ci *error_code_out = parser.error_code(); 646d528ed9Sopenharmony_ci if (error_msg_out) 656d528ed9Sopenharmony_ci *error_msg_out = parser.GetErrorMessage(); 666d528ed9Sopenharmony_ci if (error_line_out) 676d528ed9Sopenharmony_ci *error_line_out = parser.error_line(); 686d528ed9Sopenharmony_ci if (error_column_out) 696d528ed9Sopenharmony_ci *error_column_out = parser.error_column(); 706d528ed9Sopenharmony_ci } 716d528ed9Sopenharmony_ci 726d528ed9Sopenharmony_ci return root ? std::make_unique<Value>(std::move(*root)) : nullptr; 736d528ed9Sopenharmony_ci} 746d528ed9Sopenharmony_ci 756d528ed9Sopenharmony_ci// static 766d528ed9Sopenharmony_cistd::string JSONReader::ErrorCodeToString(JsonParseError error_code) { 776d528ed9Sopenharmony_ci switch (error_code) { 786d528ed9Sopenharmony_ci case JSON_NO_ERROR: 796d528ed9Sopenharmony_ci return std::string(); 806d528ed9Sopenharmony_ci case JSON_INVALID_ESCAPE: 816d528ed9Sopenharmony_ci return kInvalidEscape; 826d528ed9Sopenharmony_ci case JSON_SYNTAX_ERROR: 836d528ed9Sopenharmony_ci return kSyntaxError; 846d528ed9Sopenharmony_ci case JSON_UNEXPECTED_TOKEN: 856d528ed9Sopenharmony_ci return kUnexpectedToken; 866d528ed9Sopenharmony_ci case JSON_TRAILING_COMMA: 876d528ed9Sopenharmony_ci return kTrailingComma; 886d528ed9Sopenharmony_ci case JSON_TOO_MUCH_NESTING: 896d528ed9Sopenharmony_ci return kTooMuchNesting; 906d528ed9Sopenharmony_ci case JSON_UNEXPECTED_DATA_AFTER_ROOT: 916d528ed9Sopenharmony_ci return kUnexpectedDataAfterRoot; 926d528ed9Sopenharmony_ci case JSON_UNSUPPORTED_ENCODING: 936d528ed9Sopenharmony_ci return kUnsupportedEncoding; 946d528ed9Sopenharmony_ci case JSON_UNQUOTED_DICTIONARY_KEY: 956d528ed9Sopenharmony_ci return kUnquotedDictionaryKey; 966d528ed9Sopenharmony_ci case JSON_TOO_LARGE: 976d528ed9Sopenharmony_ci return kInputTooLarge; 986d528ed9Sopenharmony_ci case JSON_PARSE_ERROR_COUNT: 996d528ed9Sopenharmony_ci break; 1006d528ed9Sopenharmony_ci } 1016d528ed9Sopenharmony_ci NOTREACHED(); 1026d528ed9Sopenharmony_ci return std::string(); 1036d528ed9Sopenharmony_ci} 1046d528ed9Sopenharmony_ci 1056d528ed9Sopenharmony_cistd::unique_ptr<Value> JSONReader::ReadToValue(std::string_view json) { 1066d528ed9Sopenharmony_ci std::optional<Value> value = parser_->Parse(json); 1076d528ed9Sopenharmony_ci return value ? std::make_unique<Value>(std::move(*value)) : nullptr; 1086d528ed9Sopenharmony_ci} 1096d528ed9Sopenharmony_ci 1106d528ed9Sopenharmony_ciJSONReader::JsonParseError JSONReader::error_code() const { 1116d528ed9Sopenharmony_ci return parser_->error_code(); 1126d528ed9Sopenharmony_ci} 1136d528ed9Sopenharmony_ci 1146d528ed9Sopenharmony_cistd::string JSONReader::GetErrorMessage() const { 1156d528ed9Sopenharmony_ci return parser_->GetErrorMessage(); 1166d528ed9Sopenharmony_ci} 1176d528ed9Sopenharmony_ci 1186d528ed9Sopenharmony_ci} // namespace base 119