1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/json/json_reader.h"
6 
7 #include <utility>
8 #include <vector>
9 
10 #include "base/json/json_parser.h"
11 #include "base/logging.h"
12 #include "base/optional.h"
13 
14 namespace base {
15 
16 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
17 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
18               "JSONReader error out of bounds");
19 
20 const char JSONReader::kInvalidEscape[] =
21     "Invalid escape sequence.";
22 const char JSONReader::kSyntaxError[] =
23     "Syntax error.";
24 const char JSONReader::kUnexpectedToken[] =
25     "Unexpected token.";
26 const char JSONReader::kTrailingComma[] =
27     "Trailing comma not allowed.";
28 const char JSONReader::kTooMuchNesting[] =
29     "Too much nesting.";
30 const char JSONReader::kUnexpectedDataAfterRoot[] =
31     "Unexpected data after root element.";
32 const char JSONReader::kUnsupportedEncoding[] =
33     "Unsupported encoding. JSON must be UTF-8.";
34 const char JSONReader::kUnquotedDictionaryKey[] =
35     "Dictionary keys must be quoted.";
36 const char JSONReader::kInputTooLarge[] =
37     "Input string is too large (>2GB).";
38 const char JSONReader::kUnrepresentableNumber[] =
39     "Number cannot be represented.";
40 
41 JSONReader::ValueWithError::ValueWithError() = default;
42 
43 JSONReader::ValueWithError::ValueWithError(ValueWithError&& other) = default;
44 
45 JSONReader::ValueWithError::~ValueWithError() = default;
46 
47 JSONReader::ValueWithError& JSONReader::ValueWithError::operator=(
48     ValueWithError&& other) = default;
49 
JSONReader(int options,size_t max_depth)50 JSONReader::JSONReader(int options, size_t max_depth)
51     : parser_(new internal::JSONParser(options, max_depth)) {}
52 
53 JSONReader::~JSONReader() = default;
54 
55 // static
Read(StringPiece json,int options,size_t max_depth)56 Optional<Value> JSONReader::Read(StringPiece json,
57                                  int options,
58                                  size_t max_depth) {
59   internal::JSONParser parser(options, max_depth);
60   return parser.Parse(json);
61 }
62 
ReadDeprecated(StringPiece json,int options,size_t max_depth)63 std::unique_ptr<Value> JSONReader::ReadDeprecated(StringPiece json,
64                                                   int options,
65                                                   size_t max_depth) {
66   Optional<Value> value = Read(json, options, max_depth);
67   return value ? Value::ToUniquePtrValue(std::move(*value)) : nullptr;
68 }
69 
70 // static
ReadAndReturnValueWithError(StringPiece json,int options)71 JSONReader::ValueWithError JSONReader::ReadAndReturnValueWithError(
72     StringPiece json,
73     int options) {
74   ValueWithError ret;
75   internal::JSONParser parser(options);
76   ret.value = parser.Parse(json);
77   if (!ret.value) {
78     ret.error_message = parser.GetErrorMessage();
79     ret.error_code = parser.error_code();
80     ret.error_line = parser.error_line();
81     ret.error_column = parser.error_column();
82   }
83   return ret;
84 }
85 
86 // static
ReadAndReturnErrorDeprecated(StringPiece json,int options,int * error_code_out,std::string * error_msg_out,int * error_line_out,int * error_column_out)87 std::unique_ptr<Value> JSONReader::ReadAndReturnErrorDeprecated(
88     StringPiece json,
89     int options,
90     int* error_code_out,
91     std::string* error_msg_out,
92     int* error_line_out,
93     int* error_column_out) {
94   ValueWithError ret = ReadAndReturnValueWithError(json, options);
95   if (ret.value)
96     return Value::ToUniquePtrValue(std::move(*ret.value));
97 
98   if (error_code_out)
99     *error_code_out = ret.error_code;
100   if (error_msg_out)
101     *error_msg_out = ret.error_message;
102   if (error_line_out)
103     *error_line_out = ret.error_line;
104   if (error_column_out)
105     *error_column_out = ret.error_column;
106   return nullptr;
107 }
108 
109 // static
ErrorCodeToString(JsonParseError error_code)110 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
111   switch (error_code) {
112     case JSON_NO_ERROR:
113       return std::string();
114     case JSON_INVALID_ESCAPE:
115       return kInvalidEscape;
116     case JSON_SYNTAX_ERROR:
117       return kSyntaxError;
118     case JSON_UNEXPECTED_TOKEN:
119       return kUnexpectedToken;
120     case JSON_TRAILING_COMMA:
121       return kTrailingComma;
122     case JSON_TOO_MUCH_NESTING:
123       return kTooMuchNesting;
124     case JSON_UNEXPECTED_DATA_AFTER_ROOT:
125       return kUnexpectedDataAfterRoot;
126     case JSON_UNSUPPORTED_ENCODING:
127       return kUnsupportedEncoding;
128     case JSON_UNQUOTED_DICTIONARY_KEY:
129       return kUnquotedDictionaryKey;
130     case JSON_TOO_LARGE:
131       return kInputTooLarge;
132     case JSON_UNREPRESENTABLE_NUMBER:
133       return kUnrepresentableNumber;
134     case JSON_PARSE_ERROR_COUNT:
135       break;
136   }
137   NOTREACHED();
138   return std::string();
139 }
140 
ReadToValue(StringPiece json)141 Optional<Value> JSONReader::ReadToValue(StringPiece json) {
142   return parser_->Parse(json);
143 }
144 
ReadToValueDeprecated(StringPiece json)145 std::unique_ptr<Value> JSONReader::ReadToValueDeprecated(StringPiece json) {
146   Optional<Value> value = parser_->Parse(json);
147   return value ? std::make_unique<Value>(std::move(*value)) : nullptr;
148 }
149 
error_code() const150 JSONReader::JsonParseError JSONReader::error_code() const {
151   return parser_->error_code();
152 }
153 
GetErrorMessage() const154 std::string JSONReader::GetErrorMessage() const {
155   return parser_->GetErrorMessage();
156 }
157 
158 }  // namespace base
159