| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
| 6 | 6 |
| 7 #include "base/json/json_parser.h" | 7 #include "base/json/json_parser.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/values.h" |
| 9 | 10 |
| 10 namespace base { | 11 namespace base { |
| 11 | 12 |
| 12 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. | 13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. |
| 13 COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, | 14 COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, |
| 14 json_reader_error_out_of_bounds); | 15 json_reader_error_out_of_bounds); |
| 15 | 16 |
| 16 const char JSONReader::kInvalidEscape[] = | 17 const char JSONReader::kInvalidEscape[] = |
| 17 "Invalid escape sequence."; | 18 "Invalid escape sequence."; |
| 18 const char JSONReader::kSyntaxError[] = | 19 const char JSONReader::kSyntaxError[] = |
| (...skipping 16 matching lines...) Expand all Loading... |
| 35 } | 36 } |
| 36 | 37 |
| 37 JSONReader::JSONReader(int options) | 38 JSONReader::JSONReader(int options) |
| 38 : parser_(new internal::JSONParser(options)) { | 39 : parser_(new internal::JSONParser(options)) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 JSONReader::~JSONReader() { | 42 JSONReader::~JSONReader() { |
| 42 } | 43 } |
| 43 | 44 |
| 44 // static | 45 // static |
| 45 Value* JSONReader::Read(const StringPiece& json) { | 46 Value* JSONReader::DeprecatedRead(const StringPiece& json) { |
| 46 internal::JSONParser parser(JSON_PARSE_RFC); | 47 return Read(json).release(); |
| 47 return parser.Parse(json); | |
| 48 } | 48 } |
| 49 | 49 |
| 50 // static | 50 // static |
| 51 Value* JSONReader::Read(const StringPiece& json, | 51 scoped_ptr<Value> JSONReader::Read(const StringPiece& json) { |
| 52 int options) { | 52 internal::JSONParser parser(JSON_PARSE_RFC); |
| 53 internal::JSONParser parser(options); | 53 return make_scoped_ptr(parser.Parse(json)); |
| 54 return parser.Parse(json); | |
| 55 } | 54 } |
| 56 | 55 |
| 57 // static | 56 // static |
| 58 Value* JSONReader::ReadAndReturnError(const StringPiece& json, | 57 Value* JSONReader::DeprecatedRead(const StringPiece& json, int options) { |
| 59 int options, | 58 return Read(json, options).release(); |
| 60 int* error_code_out, | |
| 61 std::string* error_msg_out) { | |
| 62 internal::JSONParser parser(options); | |
| 63 Value* root = parser.Parse(json); | |
| 64 if (root) | |
| 65 return root; | |
| 66 | |
| 67 if (error_code_out) | |
| 68 *error_code_out = parser.error_code(); | |
| 69 if (error_msg_out) | |
| 70 *error_msg_out = parser.GetErrorMessage(); | |
| 71 | |
| 72 return NULL; | |
| 73 } | 59 } |
| 74 | 60 |
| 75 // static | 61 // static |
| 62 scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) { |
| 63 internal::JSONParser parser(options); |
| 64 return make_scoped_ptr(parser.Parse(json)); |
| 65 } |
| 66 |
| 67 // static |
| 68 Value* JSONReader::DeprecatedReadAndReturnError(const StringPiece& json, |
| 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 |
| 77 scoped_ptr<Value> JSONReader::ReadAndReturnError(const StringPiece& json, |
| 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 |
| 76 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { | 94 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { |
| 77 switch (error_code) { | 95 switch (error_code) { |
| 78 case JSON_NO_ERROR: | 96 case JSON_NO_ERROR: |
| 79 return std::string(); | 97 return std::string(); |
| 80 case JSON_INVALID_ESCAPE: | 98 case JSON_INVALID_ESCAPE: |
| 81 return kInvalidEscape; | 99 return kInvalidEscape; |
| 82 case JSON_SYNTAX_ERROR: | 100 case JSON_SYNTAX_ERROR: |
| 83 return kSyntaxError; | 101 return kSyntaxError; |
| 84 case JSON_UNEXPECTED_TOKEN: | 102 case JSON_UNEXPECTED_TOKEN: |
| 85 return kUnexpectedToken; | 103 return kUnexpectedToken; |
| 86 case JSON_TRAILING_COMMA: | 104 case JSON_TRAILING_COMMA: |
| 87 return kTrailingComma; | 105 return kTrailingComma; |
| 88 case JSON_TOO_MUCH_NESTING: | 106 case JSON_TOO_MUCH_NESTING: |
| 89 return kTooMuchNesting; | 107 return kTooMuchNesting; |
| 90 case JSON_UNEXPECTED_DATA_AFTER_ROOT: | 108 case JSON_UNEXPECTED_DATA_AFTER_ROOT: |
| 91 return kUnexpectedDataAfterRoot; | 109 return kUnexpectedDataAfterRoot; |
| 92 case JSON_UNSUPPORTED_ENCODING: | 110 case JSON_UNSUPPORTED_ENCODING: |
| 93 return kUnsupportedEncoding; | 111 return kUnsupportedEncoding; |
| 94 case JSON_UNQUOTED_DICTIONARY_KEY: | 112 case JSON_UNQUOTED_DICTIONARY_KEY: |
| 95 return kUnquotedDictionaryKey; | 113 return kUnquotedDictionaryKey; |
| 96 default: | 114 default: |
| 97 NOTREACHED(); | 115 NOTREACHED(); |
| 98 return std::string(); | 116 return std::string(); |
| 99 } | 117 } |
| 100 } | 118 } |
| 101 | 119 |
| 102 Value* JSONReader::ReadToValue(const std::string& json) { | 120 scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) { |
| 103 return parser_->Parse(json); | 121 return make_scoped_ptr(parser_->Parse(json)); |
| 104 } | 122 } |
| 105 | 123 |
| 106 JSONReader::JsonParseError JSONReader::error_code() const { | 124 JSONReader::JsonParseError JSONReader::error_code() const { |
| 107 return parser_->error_code(); | 125 return parser_->error_code(); |
| 108 } | 126 } |
| 109 | 127 |
| 110 std::string JSONReader::GetErrorMessage() const { | 128 std::string JSONReader::GetErrorMessage() const { |
| 111 return parser_->GetErrorMessage(); | 129 return parser_->GetErrorMessage(); |
| 112 } | 130 } |
| 113 | 131 |
| 114 } // namespace base | 132 } // namespace base |
| OLD | NEW |