blob: 015df4379de82fd135ccf3c0c812de27af4ebde1 [file] [log] [blame]
rsesek@chromium.org125e8992012-03-24 12:57:17 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
brettw@chromium.org7cd41eb2009-10-24 05:00:20 +09005#include "base/json/json_reader.h"
initial.commit3f4a7322008-07-27 06:49:38 +09006
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +09007#include "base/json/json_parser.h"
initial.commit3f4a7322008-07-27 06:49:38 +09008#include "base/logging.h"
estade9b9edfd2015-05-21 07:46:58 +09009#include "base/values.h"
initial.commit3f4a7322008-07-27 06:49:38 +090010
tfarina@chromium.org7c304b52011-09-02 02:13:20 +090011namespace base {
12
gab@chromium.org67667002013-11-21 16:44:09 +090013// Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
avi486c61f2015-11-24 23:26:24 +090014static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
15 "JSONReader error out of bounds");
gab@chromium.org67667002013-11-21 16:44:09 +090016
thestigdd7aaa02014-10-21 12:11:21 +090017const char JSONReader::kInvalidEscape[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090018 "Invalid escape sequence.";
thestigdd7aaa02014-10-21 12:11:21 +090019const char JSONReader::kSyntaxError[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090020 "Syntax error.";
thestigdd7aaa02014-10-21 12:11:21 +090021const char JSONReader::kUnexpectedToken[] =
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +090022 "Unexpected token.";
thestigdd7aaa02014-10-21 12:11:21 +090023const char JSONReader::kTrailingComma[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090024 "Trailing comma not allowed.";
thestigdd7aaa02014-10-21 12:11:21 +090025const char JSONReader::kTooMuchNesting[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090026 "Too much nesting.";
thestigdd7aaa02014-10-21 12:11:21 +090027const char JSONReader::kUnexpectedDataAfterRoot[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090028 "Unexpected data after root element.";
thestigdd7aaa02014-10-21 12:11:21 +090029const char JSONReader::kUnsupportedEncoding[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090030 "Unsupported encoding. JSON must be UTF-8.";
thestigdd7aaa02014-10-21 12:11:21 +090031const char JSONReader::kUnquotedDictionaryKey[] =
aa@chromium.org971901c2008-12-06 07:14:46 +090032 "Dictionary keys must be quoted.";
33
erg@google.com37c078e2011-01-11 09:50:59 +090034JSONReader::JSONReader()
andersre16752b2014-11-08 02:02:52 +090035 : JSONReader(JSON_PARSE_RFC) {
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +090036}
37
38JSONReader::JSONReader(int options)
39 : parser_(new internal::JSONParser(options)) {
40}
41
42JSONReader::~JSONReader() {
43}
erg@google.com37c078e2011-01-11 09:50:59 +090044
tfarina@chromium.org7c304b52011-09-02 02:13:20 +090045// static
estadefe9020d2015-05-23 01:30:13 +090046scoped_ptr<Value> JSONReader::Read(const StringPiece& json) {
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +090047 internal::JSONParser parser(JSON_PARSE_RFC);
estadefe9020d2015-05-23 01:30:13 +090048 return make_scoped_ptr(parser.Parse(json));
rsesek@chromium.orgdc692f52012-04-11 09:15:41 +090049}
50
51// static
estadefe9020d2015-05-23 01:30:13 +090052scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) {
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +090053 internal::JSONParser parser(options);
estadefe9020d2015-05-23 01:30:13 +090054 return make_scoped_ptr(parser.Parse(json));
55}
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090056
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090057
estadefe9020d2015-05-23 01:30:13 +090058// static
59scoped_ptr<Value> JSONReader::ReadAndReturnError(const StringPiece& json,
60 int options,
61 int* error_code_out,
62 std::string* error_msg_out) {
63 internal::JSONParser parser(options);
64 scoped_ptr<Value> root(parser.Parse(json));
65 if (!root) {
66 if (error_code_out)
67 *error_code_out = parser.error_code();
68 if (error_msg_out)
69 *error_msg_out = parser.GetErrorMessage();
70 }
71
72 return root;
aa@chromium.org971901c2008-12-06 07:14:46 +090073}
74
tfarina@chromium.org7c304b52011-09-02 02:13:20 +090075// static
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +090076std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
77 switch (error_code) {
78 case JSON_NO_ERROR:
79 return std::string();
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +090080 case JSON_INVALID_ESCAPE:
81 return kInvalidEscape;
82 case JSON_SYNTAX_ERROR:
83 return kSyntaxError;
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +090084 case JSON_UNEXPECTED_TOKEN:
85 return kUnexpectedToken;
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +090086 case JSON_TRAILING_COMMA:
87 return kTrailingComma;
88 case JSON_TOO_MUCH_NESTING:
89 return kTooMuchNesting;
90 case JSON_UNEXPECTED_DATA_AFTER_ROOT:
91 return kUnexpectedDataAfterRoot;
92 case JSON_UNSUPPORTED_ENCODING:
93 return kUnsupportedEncoding;
94 case JSON_UNQUOTED_DICTIONARY_KEY:
95 return kUnquotedDictionaryKey;
96 default:
97 NOTREACHED();
98 return std::string();
99 }
100}
101
estade9b9edfd2015-05-21 07:46:58 +0900102scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) {
103 return make_scoped_ptr(parser_->Parse(json));
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900104}
105
106JSONReader::JsonParseError JSONReader::error_code() const {
107 return parser_->error_code();
108}
109
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900110std::string JSONReader::GetErrorMessage() const {
rsesek@chromium.orgc2d2b202012-05-17 00:23:30 +0900111 return parser_->GetErrorMessage();
aa@chromium.org971901c2008-12-06 07:14:46 +0900112}
brettw@chromium.org7cd41eb2009-10-24 05:00:20 +0900113
114} // namespace base