Chromium Code Reviews| 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" | |
| 10 #include "base/values.h" | 9 #include "base/values.h" |
| 11 | 10 |
| 12 namespace base { | 11 namespace base { |
| 13 | 12 |
| 14 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. | 13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. |
| 15 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, | 14 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, |
| 16 "JSONReader error out of bounds"); | 15 "JSONReader error out of bounds"); |
| 17 | 16 |
| 18 const char JSONReader::kInvalidEscape[] = | 17 const char JSONReader::kInvalidEscape[] = |
| 19 "Invalid escape sequence."; | 18 "Invalid escape sequence."; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 37 } | 36 } |
| 38 | 37 |
| 39 JSONReader::JSONReader(int options) | 38 JSONReader::JSONReader(int options) |
| 40 : parser_(new internal::JSONParser(options)) { | 39 : parser_(new internal::JSONParser(options)) { |
| 41 } | 40 } |
| 42 | 41 |
| 43 JSONReader::~JSONReader() { | 42 JSONReader::~JSONReader() { |
| 44 } | 43 } |
| 45 | 44 |
| 46 // static | 45 // static |
| 47 std::unique_ptr<Value> JSONReader::Read(const StringPiece& json) { | 46 std::unique_ptr<Value> JSONReader::Read(StringPiece json) { |
| 48 internal::JSONParser parser(JSON_PARSE_RFC); | 47 internal::JSONParser parser(JSON_PARSE_RFC); |
| 49 return WrapUnique(parser.Parse(json)); | 48 return parser.Parse(json); |
|
danakj
2016/04/28 17:39:37
nit: move all the StringPieces?
dcheng
2016/04/28 18:03:23
See previous.
| |
| 50 } | 49 } |
| 51 | 50 |
| 52 // static | 51 // static |
| 53 std::unique_ptr<Value> JSONReader::Read(const StringPiece& json, int options) { | 52 std::unique_ptr<Value> JSONReader::Read(StringPiece json, int options) { |
| 54 internal::JSONParser parser(options); | 53 internal::JSONParser parser(options); |
| 55 return WrapUnique(parser.Parse(json)); | 54 return parser.Parse(json); |
| 56 } | 55 } |
| 57 | 56 |
| 58 | 57 |
| 59 // static | 58 // static |
| 60 std::unique_ptr<Value> JSONReader::ReadAndReturnError( | 59 std::unique_ptr<Value> JSONReader::ReadAndReturnError( |
| 61 const StringPiece& json, | 60 const StringPiece& json, |
| 62 int options, | 61 int options, |
| 63 int* error_code_out, | 62 int* error_code_out, |
| 64 std::string* error_msg_out, | 63 std::string* error_msg_out, |
| 65 int* error_line_out, | 64 int* error_line_out, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 case JSON_UNSUPPORTED_ENCODING: | 99 case JSON_UNSUPPORTED_ENCODING: |
| 101 return kUnsupportedEncoding; | 100 return kUnsupportedEncoding; |
| 102 case JSON_UNQUOTED_DICTIONARY_KEY: | 101 case JSON_UNQUOTED_DICTIONARY_KEY: |
| 103 return kUnquotedDictionaryKey; | 102 return kUnquotedDictionaryKey; |
| 104 default: | 103 default: |
| 105 NOTREACHED(); | 104 NOTREACHED(); |
| 106 return std::string(); | 105 return std::string(); |
| 107 } | 106 } |
| 108 } | 107 } |
| 109 | 108 |
| 110 std::unique_ptr<Value> JSONReader::ReadToValue(const std::string& json) { | 109 std::unique_ptr<Value> JSONReader::ReadToValue(StringPiece json) { |
| 111 return WrapUnique(parser_->Parse(json)); | 110 return parser_->Parse(json); |
| 112 } | 111 } |
| 113 | 112 |
| 114 JSONReader::JsonParseError JSONReader::error_code() const { | 113 JSONReader::JsonParseError JSONReader::error_code() const { |
| 115 return parser_->error_code(); | 114 return parser_->error_code(); |
| 116 } | 115 } |
| 117 | 116 |
| 118 std::string JSONReader::GetErrorMessage() const { | 117 std::string JSONReader::GetErrorMessage() const { |
| 119 return parser_->GetErrorMessage(); | 118 return parser_->GetErrorMessage(); |
| 120 } | 119 } |
| 121 | 120 |
| 122 } // namespace base | 121 } // namespace base |
| OLD | NEW |