blob: edff8dcaff67268d6395d9317c064320079e4307 [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// 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
11namespace base {
12
13// Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
14COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
15 json_reader_error_out_of_bounds);
16
17const char JSONReader::kInvalidEscape[] =
18 "Invalid escape sequence.";
19const char JSONReader::kSyntaxError[] =
20 "Syntax error.";
21const char JSONReader::kUnexpectedToken[] =
22 "Unexpected token.";
23const char JSONReader::kTrailingComma[] =
24 "Trailing comma not allowed.";
25const char JSONReader::kTooMuchNesting[] =
26 "Too much nesting.";
27const char JSONReader::kUnexpectedDataAfterRoot[] =
28 "Unexpected data after root element.";
29const char JSONReader::kUnsupportedEncoding[] =
30 "Unsupported encoding. JSON must be UTF-8.";
31const char JSONReader::kUnquotedDictionaryKey[] =
32 "Dictionary keys must be quoted.";
33
34JSONReader::JSONReader()
35 : JSONReader(JSON_PARSE_RFC) {
36}
37
38JSONReader::JSONReader(int options)
39 : parser_(new internal::JSONParser(options)) {
40}
41
42JSONReader::~JSONReader() {
43}
44
45// static
Vitaly Bukace65ec82015-09-14 10:28:49 -070046Value* JSONReader::DeprecatedRead(const std::string& json) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070047 return Read(json).release();
48}
49
50// static
Vitaly Bukace65ec82015-09-14 10:28:49 -070051scoped_ptr<Value> JSONReader::Read(const std::string& json) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070052 internal::JSONParser parser(JSON_PARSE_RFC);
53 return make_scoped_ptr(parser.Parse(json));
54}
55
56// static
Vitaly Bukace65ec82015-09-14 10:28:49 -070057Value* JSONReader::DeprecatedRead(const std::string& json, int options) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070058 return Read(json, options).release();
59}
60
61// static
Vitaly Bukace65ec82015-09-14 10:28:49 -070062scoped_ptr<Value> JSONReader::Read(const std::string& json, int options) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070063 internal::JSONParser parser(options);
64 return make_scoped_ptr(parser.Parse(json));
65}
66
67// static
Vitaly Bukace65ec82015-09-14 10:28:49 -070068Value* JSONReader::DeprecatedReadAndReturnError(const std::string& json,
Vitaly Bukacbed2062015-08-17 12:54:05 -070069 int options,
70 int* error_code_out,
71 std::string* error_msg_out) {
72 return ReadAndReturnError(json, options, error_code_out, error_msg_out)
73 .release();
74}
75
76// static
Vitaly Bukace65ec82015-09-14 10:28:49 -070077scoped_ptr<Value> JSONReader::ReadAndReturnError(const std::string& json,
Vitaly Bukacbed2062015-08-17 12:54:05 -070078 int options,
79 int* error_code_out,
80 std::string* error_msg_out) {
81 internal::JSONParser parser(options);
82 scoped_ptr<Value> root(parser.Parse(json));
83 if (!root) {
84 if (error_code_out)
85 *error_code_out = parser.error_code();
86 if (error_msg_out)
87 *error_msg_out = parser.GetErrorMessage();
88 }
89
90 return root;
91}
92
93// static
94std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
95 switch (error_code) {
96 case JSON_NO_ERROR:
97 return std::string();
98 case JSON_INVALID_ESCAPE:
99 return kInvalidEscape;
100 case JSON_SYNTAX_ERROR:
101 return kSyntaxError;
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;
114 default:
115 NOTREACHED();
116 return std::string();
117 }
118}
119
120scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) {
121 return make_scoped_ptr(parser_->Parse(json));
122}
123
124JSONReader::JsonParseError JSONReader::error_code() const {
125 return parser_->error_code();
126}
127
128std::string JSONReader::GetErrorMessage() const {
129 return parser_->GetErrorMessage();
130}
131
132} // namespace base