Commit 198130ea authored by Nigel Tao's avatar Nigel Tao Committed by Commit Bot

Check JSON value instead of error_code

Checking (result.error_code != JSON_NO_ERROR) is equivalent to checking
result.value (which has a base::Optional type, convertible to bool).

The error_code enum is really a private implementation detail. Callers
don't care about it (other than it being an error or no error). Removing
references (outside of //base/json) to that enum will let us more easily
modify the //base/json implementation.

Bug: 1070409
Change-Id: I0fe1f6d5debb837b6e7abab6fdfa0fd6a8a22e02
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2143190Reviewed-by: default avatarRoman Sorokin [CET] <rsorokin@chromium.org>
Commit-Queue: Nigel Tao <nigeltao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759058}
parent a1836464
......@@ -76,7 +76,7 @@ base::Optional<base::Value> ParseJsonToDict(const std::string& json) {
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
json, base::JSON_ALLOW_TRAILING_COMMAS);
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR) {
if (!value_with_error.value) {
LOG(ERROR) << "Could not parse policy value as JSON: "
<< value_with_error.error_message;
return base::nullopt;
......
......@@ -1799,7 +1799,7 @@ base::Optional<base::Value> DecodeJsonStringAndNormalize(
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
json_string, base::JSON_ALLOW_TRAILING_COMMAS);
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR) {
if (!value_with_error.value) {
*error = "Invalid JSON string: " + value_with_error.error_message;
return base::nullopt;
}
......
......@@ -564,7 +564,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::ValidateJsonString(
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
json_string, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR) {
if (!value_with_error.value) {
if (errors) {
errors->AddError(policy_name_, ErrorPath(index, ""),
IDS_POLICY_INVALID_JSON_ERROR,
......
......@@ -398,7 +398,7 @@ bool ComponentCloudPolicyStore::ParsePolicy(const std::string& data,
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
data, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR) {
if (!value_with_error.value) {
LOG(ERROR) << "Invalid JSON blob: " << value_with_error.error_message;
return false;
}
......
......@@ -89,7 +89,7 @@ base::Value DecodeJsonProto(const em::StringPolicyProto& proto,
base::JSONReader::ReadAndReturnValueWithError(
json, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR) {
if (!value_with_error.value) {
// Can't parse as JSON so return it as a string, and leave it to the handler
// to validate.
LOG(WARNING) << "Invalid JSON: " << json;
......
......@@ -1423,7 +1423,7 @@ base::Optional<base::Value> Schema::ParseToDictAndValidate(
schema, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
*error = value_with_error.error_message;
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR)
if (!value_with_error.value)
return base::nullopt;
base::Value json = std::move(value_with_error.value.value());
if (!json.is_dict()) {
......
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