Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 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 | COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, |
| 15 | json_reader_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 | |
| 34 | JSONReader::JSONReader() |
| 35 | : JSONReader(JSON_PARSE_RFC) { |
| 36 | } |
| 37 | |
| 38 | JSONReader::JSONReader(int options) |
| 39 | : parser_(new internal::JSONParser(options)) { |
| 40 | } |
| 41 | |
| 42 | JSONReader::~JSONReader() { |
| 43 | } |
| 44 | |
| 45 | // static |
Vitaly Buka | ce65ec8 | 2015-09-14 10:28:49 -0700 | [diff] [blame] | 46 | Value* JSONReader::DeprecatedRead(const std::string& json) { |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 47 | return Read(json).release(); |
| 48 | } |
| 49 | |
| 50 | // static |
Vitaly Buka | ce65ec8 | 2015-09-14 10:28:49 -0700 | [diff] [blame] | 51 | scoped_ptr<Value> JSONReader::Read(const std::string& json) { |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 52 | internal::JSONParser parser(JSON_PARSE_RFC); |
| 53 | return make_scoped_ptr(parser.Parse(json)); |
| 54 | } |
| 55 | |
| 56 | // static |
Vitaly Buka | ce65ec8 | 2015-09-14 10:28:49 -0700 | [diff] [blame] | 57 | Value* JSONReader::DeprecatedRead(const std::string& json, int options) { |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 58 | return Read(json, options).release(); |
| 59 | } |
| 60 | |
| 61 | // static |
Vitaly Buka | ce65ec8 | 2015-09-14 10:28:49 -0700 | [diff] [blame] | 62 | scoped_ptr<Value> JSONReader::Read(const std::string& json, int options) { |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 63 | internal::JSONParser parser(options); |
| 64 | return make_scoped_ptr(parser.Parse(json)); |
| 65 | } |
| 66 | |
| 67 | // static |
Vitaly Buka | ce65ec8 | 2015-09-14 10:28:49 -0700 | [diff] [blame] | 68 | Value* JSONReader::DeprecatedReadAndReturnError(const std::string& json, |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 69 | 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 Buka | ce65ec8 | 2015-09-14 10:28:49 -0700 | [diff] [blame] | 77 | scoped_ptr<Value> JSONReader::ReadAndReturnError(const std::string& json, |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 78 | 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 |
| 94 | std::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 | |
| 120 | scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) { |
| 121 | return make_scoped_ptr(parser_->Parse(json)); |
| 122 | } |
| 123 | |
| 124 | JSONReader::JsonParseError JSONReader::error_code() const { |
| 125 | return parser_->error_code(); |
| 126 | } |
| 127 | |
| 128 | std::string JSONReader::GetErrorMessage() const { |
| 129 | return parser_->GetErrorMessage(); |
| 130 | } |
| 131 | |
| 132 | } // namespace base |