Commit 471572e3 authored by Nigel Tao's avatar Nigel Tao Committed by Commit Bot

Remove some deprecated base::JSONReader methods

Some deleted methods explicitly have "Deprecated" in their names. Others
are non-static methods, which can cause confusion when mixed with static
methods (e.g. crrev.com/c/2227944, crrev.com/c/2237582).

Also delete the constructors. All remaining methods are static.

Bug: 1069271
Bug: 1070409
Change-Id: I81e9791cef4edcc0ef55d5c637f5f65336d45a7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2227726
Commit-Queue: Nigel Tao <nigeltao@chromium.org>
Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779586}
parent 5f401649
...@@ -41,11 +41,6 @@ JSONReader::ValueWithError::~ValueWithError() = default; ...@@ -41,11 +41,6 @@ JSONReader::ValueWithError::~ValueWithError() = default;
JSONReader::ValueWithError& JSONReader::ValueWithError::operator=( JSONReader::ValueWithError& JSONReader::ValueWithError::operator=(
ValueWithError&& other) = default; ValueWithError&& other) = default;
JSONReader::JSONReader(int options, size_t max_depth)
: parser_(new internal::JSONParser(options, max_depth)) {}
JSONReader::~JSONReader() = default;
// static // static
Optional<Value> JSONReader::Read(StringPiece json, Optional<Value> JSONReader::Read(StringPiece json,
int options, int options,
...@@ -54,6 +49,7 @@ Optional<Value> JSONReader::Read(StringPiece json, ...@@ -54,6 +49,7 @@ Optional<Value> JSONReader::Read(StringPiece json,
return parser.Parse(json); return parser.Parse(json);
} }
// static
std::unique_ptr<Value> JSONReader::ReadDeprecated(StringPiece json, std::unique_ptr<Value> JSONReader::ReadDeprecated(StringPiece json,
int options, int options,
size_t max_depth) { size_t max_depth) {
...@@ -77,29 +73,6 @@ JSONReader::ValueWithError JSONReader::ReadAndReturnValueWithError( ...@@ -77,29 +73,6 @@ JSONReader::ValueWithError JSONReader::ReadAndReturnValueWithError(
return ret; return ret;
} }
// static
std::unique_ptr<Value> JSONReader::ReadAndReturnErrorDeprecated(
StringPiece json,
int options,
int* error_code_out,
std::string* error_msg_out,
int* error_line_out,
int* error_column_out) {
ValueWithError ret = ReadAndReturnValueWithError(json, options);
if (ret.value)
return Value::ToUniquePtrValue(std::move(*ret.value));
if (error_code_out)
*error_code_out = ret.error_code;
if (error_msg_out)
*error_msg_out = ret.error_message;
if (error_line_out)
*error_line_out = ret.error_line;
if (error_column_out)
*error_column_out = ret.error_column;
return nullptr;
}
// static // static
std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
switch (error_code) { switch (error_code) {
...@@ -132,17 +105,4 @@ std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { ...@@ -132,17 +105,4 @@ std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
return std::string(); return std::string();
} }
Optional<Value> JSONReader::ReadToValue(StringPiece json) {
return parser_->Parse(json);
}
std::unique_ptr<Value> JSONReader::ReadToValueDeprecated(StringPiece json) {
Optional<Value> value = parser_->Parse(json);
return value ? std::make_unique<Value>(std::move(*value)) : nullptr;
}
std::string JSONReader::GetErrorMessage() const {
return parser_->GetErrorMessage();
}
} // namespace base } // namespace base
...@@ -49,10 +49,6 @@ ...@@ -49,10 +49,6 @@
namespace base { namespace base {
namespace internal {
class JSONParser;
}
enum JSONParserOptions { enum JSONParserOptions {
// Parses the input strictly according to RFC 8259, except for where noted // Parses the input strictly according to RFC 8259, except for where noted
// above. // above.
...@@ -122,12 +118,6 @@ class BASE_EXPORT JSONReader { ...@@ -122,12 +118,6 @@ class BASE_EXPORT JSONReader {
static const char kInputTooLarge[]; static const char kInputTooLarge[];
static const char kUnrepresentableNumber[]; static const char kUnrepresentableNumber[];
// Constructs a reader.
JSONReader(int options = JSON_PARSE_RFC,
size_t max_depth = internal::kAbsoluteMaxDepth);
~JSONReader();
// Reads and parses |json|, returning a Value. // Reads and parses |json|, returning a Value.
// If |json| is not a properly formed JSON string, returns base::nullopt. // If |json| is not a properly formed JSON string, returns base::nullopt.
static Optional<Value> Read(StringPiece json, static Optional<Value> Read(StringPiece json,
...@@ -151,36 +141,13 @@ class BASE_EXPORT JSONReader { ...@@ -151,36 +141,13 @@ class BASE_EXPORT JSONReader {
StringPiece json, StringPiece json,
int options = JSON_PARSE_RFC); int options = JSON_PARSE_RFC);
// Deprecated. Use the ReadAndReturnValueWithError() method above.
// Reads and parses |json| like Read(). |error_code_out| and |error_msg_out|
// are optional. If specified and nullptr is returned, they will be populated
// an error code and a formatted error message (including error location if
// appropriate). Otherwise, they will be unmodified.
static std::unique_ptr<Value> ReadAndReturnErrorDeprecated(
StringPiece json,
int options, // JSONParserOptions
int* error_code_out,
std::string* error_msg_out,
int* error_line_out = nullptr,
int* error_column_out = nullptr);
// Converts a JSON parse error code into a human readable message. // Converts a JSON parse error code into a human readable message.
// Returns an empty string if error_code is JSON_NO_ERROR. // Returns an empty string if error_code is JSON_NO_ERROR.
static std::string ErrorCodeToString(JsonParseError error_code); static std::string ErrorCodeToString(JsonParseError error_code);
// Non-static version of Read() above. // This class contains only static methods.
Optional<Value> ReadToValue(StringPiece json); JSONReader() = delete;
DISALLOW_COPY_AND_ASSIGN(JSONReader);
// Deprecated. Use the ReadToValue() method above.
// Non-static version of Read() above.
std::unique_ptr<Value> ReadToValueDeprecated(StringPiece json);
// Converts error_code_ to a human-readable string, including line and column
// numbers if appropriate.
std::string GetErrorMessage() const;
private:
std::unique_ptr<internal::JSONParser> parser_;
}; };
} // namespace base } // namespace base
......
...@@ -647,10 +647,10 @@ TEST(JSONReaderTest, ReadFromFile) { ...@@ -647,10 +647,10 @@ TEST(JSONReaderTest, ReadFromFile) {
std::string input; std::string input;
ASSERT_TRUE(ReadFileToString(path.AppendASCII("bom_feff.json"), &input)); ASSERT_TRUE(ReadFileToString(path.AppendASCII("bom_feff.json"), &input));
JSONReader reader; JSONReader::ValueWithError root =
Optional<Value> root(reader.ReadToValue(input)); JSONReader::ReadAndReturnValueWithError(input);
ASSERT_TRUE(root) << reader.GetErrorMessage(); ASSERT_TRUE(root.value) << root.error_message;
EXPECT_TRUE(root->is_dict()); EXPECT_TRUE(root.value->is_dict());
} }
// Tests that the root of a JSON object can be deleted safely while its // Tests that the root of a JSON object can be deleted safely while its
...@@ -736,19 +736,21 @@ TEST(JSONReaderTest, InvalidSanity) { ...@@ -736,19 +736,21 @@ TEST(JSONReaderTest, InvalidSanity) {
}; };
for (size_t i = 0; i < base::size(kInvalidJson); ++i) { for (size_t i = 0; i < base::size(kInvalidJson); ++i) {
JSONReader reader;
LOG(INFO) << "Sanity test " << i << ": <" << kInvalidJson[i] << ">"; LOG(INFO) << "Sanity test " << i << ": <" << kInvalidJson[i] << ">";
EXPECT_FALSE(reader.ReadToValue(kInvalidJson[i])); JSONReader::ValueWithError root =
EXPECT_NE("", reader.GetErrorMessage()); JSONReader::ReadAndReturnValueWithError(kInvalidJson[i]);
EXPECT_FALSE(root.value);
EXPECT_NE("", root.error_message);
} }
} }
TEST(JSONReaderTest, IllegalTrailingNull) { TEST(JSONReaderTest, IllegalTrailingNull) {
const char json[] = {'"', 'n', 'u', 'l', 'l', '"', '\0'}; const char json[] = {'"', 'n', 'u', 'l', 'l', '"', '\0'};
std::string json_string(json, sizeof(json)); std::string json_string(json, sizeof(json));
JSONReader reader; JSONReader::ValueWithError root =
EXPECT_FALSE(reader.ReadToValue(json_string)); JSONReader::ReadAndReturnValueWithError(json_string);
EXPECT_NE("", reader.GetErrorMessage()); EXPECT_FALSE(root.value);
EXPECT_NE("", root.error_message);
} }
TEST(JSONReaderTest, ASCIIControlCodes) { TEST(JSONReaderTest, ASCIIControlCodes) {
......
...@@ -49,6 +49,14 @@ JSONStringValueDeserializer::~JSONStringValueDeserializer() = default; ...@@ -49,6 +49,14 @@ JSONStringValueDeserializer::~JSONStringValueDeserializer() = default;
std::unique_ptr<Value> JSONStringValueDeserializer::Deserialize( std::unique_ptr<Value> JSONStringValueDeserializer::Deserialize(
int* error_code, int* error_code,
std::string* error_str) { std::string* error_str) {
return base::JSONReader::ReadAndReturnErrorDeprecated(json_string_, options_, base::JSONReader::ValueWithError ret =
error_code, error_str); base::JSONReader::ReadAndReturnValueWithError(json_string_, options_);
if (ret.value)
return base::Value::ToUniquePtrValue(std::move(*ret.value));
if (error_code)
*error_code = ret.error_code;
if (error_str)
*error_str = std::move(ret.error_message);
return nullptr;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment