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 "base/json/json_parser.h"
8 #include "base/logging.h"
9 #include "base/values.h"
10
11 namespace base {
12
13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
14 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
15 "JSONReader error out of bounds");
16
17 const char JSONReader::kInvalidEscape[] =
18 "Invalid escape sequence.";
19 const char JSONReader::kSyntaxError[] =
20 "Syntax error.";
21 const char JSONReader::kUnexpectedToken[] =
22 "Unexpected token.";
23 const char JSONReader::kTrailingComma[] =
24 "Trailing comma not allowed.";
25 const char JSONReader::kTooMuchNesting[] =
26 "Too much nesting.";
27 const char JSONReader::kUnexpectedDataAfterRoot[] =
28 "Unexpected data after root element.";
29 const char JSONReader::kUnsupportedEncoding[] =
30 "Unsupported encoding. JSON must be UTF-8.";
31 const char JSONReader::kUnquotedDictionaryKey[] =
32 "Dictionary keys must be quoted.";
33
JSONReader()34 JSONReader::JSONReader()
35 : JSONReader(JSON_PARSE_RFC) {
36 }
37
JSONReader(int options)38 JSONReader::JSONReader(int options)
39 : parser_(new internal::JSONParser(options)) {
40 }
41
~JSONReader()42 JSONReader::~JSONReader() {
43 }
44
45 // static
Read(StringPiece json)46 std::unique_ptr<Value> JSONReader::Read(StringPiece json) {
47 internal::JSONParser parser(JSON_PARSE_RFC);
48 return parser.Parse(json);
49 }
50
51 // static
Read(StringPiece json,int options)52 std::unique_ptr<Value> JSONReader::Read(StringPiece json, int options) {
53 internal::JSONParser parser(options);
54 return parser.Parse(json);
55 }
56
57
58 // static
ReadAndReturnError(const StringPiece & json,int options,int * error_code_out,std::string * error_msg_out,int * error_line_out,int * error_column_out)59 std::unique_ptr<Value> JSONReader::ReadAndReturnError(
60 const StringPiece& json,
61 int options,
62 int* error_code_out,
63 std::string* error_msg_out,
64 int* error_line_out,
65 int* error_column_out) {
66 internal::JSONParser parser(options);
67 std::unique_ptr<Value> root(parser.Parse(json));
68 if (!root) {
69 if (error_code_out)
70 *error_code_out = parser.error_code();
71 if (error_msg_out)
72 *error_msg_out = parser.GetErrorMessage();
73 if (error_line_out)
74 *error_line_out = parser.error_line();
75 if (error_column_out)
76 *error_column_out = parser.error_column();
77 }
78
79 return root;
80 }
81
82 // static
ErrorCodeToString(JsonParseError error_code)83 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
84 switch (error_code) {
85 case JSON_NO_ERROR:
86 return std::string();
87 case JSON_INVALID_ESCAPE:
88 return kInvalidEscape;
89 case JSON_SYNTAX_ERROR:
90 return kSyntaxError;
91 case JSON_UNEXPECTED_TOKEN:
92 return kUnexpectedToken;
93 case JSON_TRAILING_COMMA:
94 return kTrailingComma;
95 case JSON_TOO_MUCH_NESTING:
96 return kTooMuchNesting;
97 case JSON_UNEXPECTED_DATA_AFTER_ROOT:
98 return kUnexpectedDataAfterRoot;
99 case JSON_UNSUPPORTED_ENCODING:
100 return kUnsupportedEncoding;
101 case JSON_UNQUOTED_DICTIONARY_KEY:
102 return kUnquotedDictionaryKey;
103 default:
104 NOTREACHED();
105 return std::string();
106 }
107 }
108
ReadToValue(StringPiece json)109 std::unique_ptr<Value> JSONReader::ReadToValue(StringPiece json) {
110 return parser_->Parse(json);
111 }
112
error_code() const113 JSONReader::JsonParseError JSONReader::error_code() const {
114 return parser_->error_code();
115 }
116
GetErrorMessage() const117 std::string JSONReader::GetErrorMessage() const {
118 return parser_->GetErrorMessage();
119 }
120
121 } // namespace base
122