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.
5 #include "base/json/json_reader.h"
7 #include "base/json/json_parser.h"
8 #include "base/logging.h"
9 #include "base/values.h"
13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
14 COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT
< 1000,
15 json_reader_error_out_of_bounds
);
17 const char JSONReader::kInvalidEscape
[] =
18 "Invalid escape sequence.";
19 const char JSONReader::kSyntaxError
[] =
21 const char JSONReader::kUnexpectedToken
[] =
23 const char JSONReader::kTrailingComma
[] =
24 "Trailing comma not allowed.";
25 const char JSONReader::kTooMuchNesting
[] =
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.";
34 JSONReader::JSONReader()
35 : JSONReader(JSON_PARSE_RFC
) {
38 JSONReader::JSONReader(int options
)
39 : parser_(new internal::JSONParser(options
)) {
42 JSONReader::~JSONReader() {
46 Value
* JSONReader::DeprecatedRead(const StringPiece
& json
) {
47 return Read(json
).release();
51 scoped_ptr
<Value
> JSONReader::Read(const StringPiece
& json
) {
52 internal::JSONParser
parser(JSON_PARSE_RFC
);
53 return make_scoped_ptr(parser
.Parse(json
));
57 Value
* JSONReader::DeprecatedRead(const StringPiece
& json
, int options
) {
58 return Read(json
, options
).release();
62 scoped_ptr
<Value
> JSONReader::Read(const StringPiece
& json
, int options
) {
63 internal::JSONParser
parser(options
);
64 return make_scoped_ptr(parser
.Parse(json
));
68 Value
* JSONReader::DeprecatedReadAndReturnError(const StringPiece
& json
,
71 std::string
* error_msg_out
) {
72 return ReadAndReturnError(json
, options
, error_code_out
, error_msg_out
)
77 scoped_ptr
<Value
> JSONReader::ReadAndReturnError(const StringPiece
& json
,
80 std::string
* error_msg_out
) {
81 internal::JSONParser
parser(options
);
82 scoped_ptr
<Value
> root(parser
.Parse(json
));
85 *error_code_out
= parser
.error_code();
87 *error_msg_out
= parser
.GetErrorMessage();
94 std::string
JSONReader::ErrorCodeToString(JsonParseError error_code
) {
98 case JSON_INVALID_ESCAPE
:
99 return kInvalidEscape
;
100 case JSON_SYNTAX_ERROR
:
102 case JSON_UNEXPECTED_TOKEN
:
103 return kUnexpectedToken
;
104 case JSON_TRAILING_COMMA
:
105 return kTrailingComma
;
106 case JSON_TOO_MUCH_NESTING
:
107 return kTooMuchNesting
;
108 case JSON_UNEXPECTED_DATA_AFTER_ROOT
:
109 return kUnexpectedDataAfterRoot
;
110 case JSON_UNSUPPORTED_ENCODING
:
111 return kUnsupportedEncoding
;
112 case JSON_UNQUOTED_DICTIONARY_KEY
:
113 return kUnquotedDictionaryKey
;
116 return std::string();
120 scoped_ptr
<Value
> JSONReader::ReadToValue(const std::string
& json
) {
121 return make_scoped_ptr(parser_
->Parse(json
));
124 JSONReader::JsonParseError
JSONReader::error_code() const {
125 return parser_
->error_code();
128 std::string
JSONReader::GetErrorMessage() const {
129 return parser_
->GetErrorMessage();