| 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/memory/ptr_util.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 | 13 |
| 13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. | 14 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. |
| 14 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, | 15 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, |
| 15 "JSONReader error out of bounds"); | 16 "JSONReader error out of bounds"); |
| 16 | 17 |
| 17 const char JSONReader::kInvalidEscape[] = | 18 const char JSONReader::kInvalidEscape[] = |
| 18 "Invalid escape sequence."; | 19 "Invalid escape sequence."; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 36 } | 37 } |
| 37 | 38 |
| 38 JSONReader::JSONReader(int options) | 39 JSONReader::JSONReader(int options) |
| 39 : parser_(new internal::JSONParser(options)) { | 40 : parser_(new internal::JSONParser(options)) { |
| 40 } | 41 } |
| 41 | 42 |
| 42 JSONReader::~JSONReader() { | 43 JSONReader::~JSONReader() { |
| 43 } | 44 } |
| 44 | 45 |
| 45 // static | 46 // static |
| 46 scoped_ptr<Value> JSONReader::Read(const StringPiece& json) { | 47 std::unique_ptr<Value> JSONReader::Read(const StringPiece& json) { |
| 47 internal::JSONParser parser(JSON_PARSE_RFC); | 48 internal::JSONParser parser(JSON_PARSE_RFC); |
| 48 return make_scoped_ptr(parser.Parse(json)); | 49 return WrapUnique(parser.Parse(json)); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // static | 52 // static |
| 52 scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) { | 53 std::unique_ptr<Value> JSONReader::Read(const StringPiece& json, int options) { |
| 53 internal::JSONParser parser(options); | 54 internal::JSONParser parser(options); |
| 54 return make_scoped_ptr(parser.Parse(json)); | 55 return WrapUnique(parser.Parse(json)); |
| 55 } | 56 } |
| 56 | 57 |
| 57 | 58 |
| 58 // static | 59 // static |
| 59 scoped_ptr<Value> JSONReader::ReadAndReturnError(const StringPiece& json, | 60 std::unique_ptr<Value> JSONReader::ReadAndReturnError( |
| 60 int options, | 61 const StringPiece& json, |
| 61 int* error_code_out, | 62 int options, |
| 62 std::string* error_msg_out, | 63 int* error_code_out, |
| 63 int* error_line_out, | 64 std::string* error_msg_out, |
| 64 int* error_column_out) { | 65 int* error_line_out, |
| 66 int* error_column_out) { |
| 65 internal::JSONParser parser(options); | 67 internal::JSONParser parser(options); |
| 66 scoped_ptr<Value> root(parser.Parse(json)); | 68 std::unique_ptr<Value> root(parser.Parse(json)); |
| 67 if (!root) { | 69 if (!root) { |
| 68 if (error_code_out) | 70 if (error_code_out) |
| 69 *error_code_out = parser.error_code(); | 71 *error_code_out = parser.error_code(); |
| 70 if (error_msg_out) | 72 if (error_msg_out) |
| 71 *error_msg_out = parser.GetErrorMessage(); | 73 *error_msg_out = parser.GetErrorMessage(); |
| 72 if (error_line_out) | 74 if (error_line_out) |
| 73 *error_line_out = parser.error_line(); | 75 *error_line_out = parser.error_line(); |
| 74 if (error_column_out) | 76 if (error_column_out) |
| 75 *error_column_out = parser.error_column(); | 77 *error_column_out = parser.error_column(); |
| 76 } | 78 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 98 case JSON_UNSUPPORTED_ENCODING: | 100 case JSON_UNSUPPORTED_ENCODING: |
| 99 return kUnsupportedEncoding; | 101 return kUnsupportedEncoding; |
| 100 case JSON_UNQUOTED_DICTIONARY_KEY: | 102 case JSON_UNQUOTED_DICTIONARY_KEY: |
| 101 return kUnquotedDictionaryKey; | 103 return kUnquotedDictionaryKey; |
| 102 default: | 104 default: |
| 103 NOTREACHED(); | 105 NOTREACHED(); |
| 104 return std::string(); | 106 return std::string(); |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 | 109 |
| 108 scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) { | 110 std::unique_ptr<Value> JSONReader::ReadToValue(const std::string& json) { |
| 109 return make_scoped_ptr(parser_->Parse(json)); | 111 return WrapUnique(parser_->Parse(json)); |
| 110 } | 112 } |
| 111 | 113 |
| 112 JSONReader::JsonParseError JSONReader::error_code() const { | 114 JSONReader::JsonParseError JSONReader::error_code() const { |
| 113 return parser_->error_code(); | 115 return parser_->error_code(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 std::string JSONReader::GetErrorMessage() const { | 118 std::string JSONReader::GetErrorMessage() const { |
| 117 return parser_->GetErrorMessage(); | 119 return parser_->GetErrorMessage(); |
| 118 } | 120 } |
| 119 | 121 |
| 120 } // namespace base | 122 } // namespace base |
| OLD | NEW |