rsesek@chromium.org | 125e899 | 2012-03-24 12:57:17 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
brettw@chromium.org | 7cd41eb | 2009-10-24 05:00:20 +0900 | [diff] [blame] | 5 | #include "base/json/json_reader.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 6 | |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 7 | #include "base/json/json_parser.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | #include "base/logging.h" |
estade | 9b9edfd | 2015-05-21 07:46:58 +0900 | [diff] [blame] | 9 | #include "base/values.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 10 | |
tfarina@chromium.org | 7c304b5 | 2011-09-02 02:13:20 +0900 | [diff] [blame] | 11 | namespace base { |
| 12 | |
gab@chromium.org | 6766700 | 2013-11-21 16:44:09 +0900 | [diff] [blame] | 13 | // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 14 | static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, |
| 15 | "JSONReader error out of bounds"); |
gab@chromium.org | 6766700 | 2013-11-21 16:44:09 +0900 | [diff] [blame] | 16 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 17 | const char JSONReader::kInvalidEscape[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 18 | "Invalid escape sequence."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 19 | const char JSONReader::kSyntaxError[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 20 | "Syntax error."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 21 | const char JSONReader::kUnexpectedToken[] = |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 22 | "Unexpected token."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 23 | const char JSONReader::kTrailingComma[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 24 | "Trailing comma not allowed."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 25 | const char JSONReader::kTooMuchNesting[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 26 | "Too much nesting."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 27 | const char JSONReader::kUnexpectedDataAfterRoot[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 28 | "Unexpected data after root element."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 29 | const char JSONReader::kUnsupportedEncoding[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 30 | "Unsupported encoding. JSON must be UTF-8."; |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 31 | const char JSONReader::kUnquotedDictionaryKey[] = |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 32 | "Dictionary keys must be quoted."; |
| 33 | |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 34 | JSONReader::JSONReader() |
andersr | e16752b | 2014-11-08 02:02:52 +0900 | [diff] [blame] | 35 | : JSONReader(JSON_PARSE_RFC) { |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | JSONReader::JSONReader(int options) |
| 39 | : parser_(new internal::JSONParser(options)) { |
| 40 | } |
| 41 | |
| 42 | JSONReader::~JSONReader() { |
| 43 | } |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 44 | |
tfarina@chromium.org | 7c304b5 | 2011-09-02 02:13:20 +0900 | [diff] [blame] | 45 | // static |
estade | fe9020d | 2015-05-23 01:30:13 +0900 | [diff] [blame] | 46 | scoped_ptr<Value> JSONReader::Read(const StringPiece& json) { |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 47 | internal::JSONParser parser(JSON_PARSE_RFC); |
estade | fe9020d | 2015-05-23 01:30:13 +0900 | [diff] [blame] | 48 | return make_scoped_ptr(parser.Parse(json)); |
rsesek@chromium.org | dc692f5 | 2012-04-11 09:15:41 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // static |
estade | fe9020d | 2015-05-23 01:30:13 +0900 | [diff] [blame] | 52 | scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) { |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 53 | internal::JSONParser parser(options); |
estade | fe9020d | 2015-05-23 01:30:13 +0900 | [diff] [blame] | 54 | return make_scoped_ptr(parser.Parse(json)); |
| 55 | } |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 56 | |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 57 | |
estade | fe9020d | 2015-05-23 01:30:13 +0900 | [diff] [blame] | 58 | // static |
| 59 | scoped_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.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 73 | } |
| 74 | |
tfarina@chromium.org | 7c304b5 | 2011-09-02 02:13:20 +0900 | [diff] [blame] | 75 | // static |
erikkay@chromium.org | 64b2cf4 | 2010-04-07 00:42:39 +0900 | [diff] [blame] | 76 | std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { |
| 77 | switch (error_code) { |
| 78 | case JSON_NO_ERROR: |
| 79 | return std::string(); |
erikkay@chromium.org | 64b2cf4 | 2010-04-07 00:42:39 +0900 | [diff] [blame] | 80 | case JSON_INVALID_ESCAPE: |
| 81 | return kInvalidEscape; |
| 82 | case JSON_SYNTAX_ERROR: |
| 83 | return kSyntaxError; |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 84 | case JSON_UNEXPECTED_TOKEN: |
| 85 | return kUnexpectedToken; |
erikkay@chromium.org | 64b2cf4 | 2010-04-07 00:42:39 +0900 | [diff] [blame] | 86 | 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 | |
estade | 9b9edfd | 2015-05-21 07:46:58 +0900 | [diff] [blame] | 102 | scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) { |
| 103 | return make_scoped_ptr(parser_->Parse(json)); |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | JSONReader::JsonParseError JSONReader::error_code() const { |
| 107 | return parser_->error_code(); |
| 108 | } |
| 109 | |
erikkay@chromium.org | 64b2cf4 | 2010-04-07 00:42:39 +0900 | [diff] [blame] | 110 | std::string JSONReader::GetErrorMessage() const { |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 111 | return parser_->GetErrorMessage(); |
aa@chromium.org | 971901c | 2008-12-06 07:14:46 +0900 | [diff] [blame] | 112 | } |
brettw@chromium.org | 7cd41eb | 2009-10-24 05:00:20 +0900 | [diff] [blame] | 113 | |
| 114 | } // namespace base |