0

[ios] Eliminate some JSONStringValue[De]Serializer usages

Eliminate some JSONStringValue[De]Serializer
usages in the //ios directory and replace them
with slightly more concise equivalents.

Along with some other changes that may be
necessary.

Bug: 40912727,40258809
Change-Id: I80e3c51a08eddb04f9a1c62b6dc30fa18da99757
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6348000
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Ho Cheung <hocheung@chromium.org>
Reviewed-by: Andrew Paseltiner <apaseltiner@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1432459}
This commit is contained in:
Ho Cheung
2025-03-13 17:11:16 -07:00
committed by Chromium LUCI CQ
parent 79fb70e71e
commit 7e55cffece
5 changed files with 32 additions and 36 deletions

@ -4,9 +4,10 @@
#import <Foundation/Foundation.h>
#import <optional>
#import <string>
#import "base/json/json_string_value_serializer.h"
#import "base/json/json_writer.h"
#import "base/strings/strcat.h"
#import "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
@ -67,10 +68,9 @@ constexpr int kTextInputFieldMaxLength = 524288;
// Serializes a dictionary value in a NSString.
NSString* SerializeDictValueToNSString(const base::Value::Dict& value) {
std::string output;
JSONStringValueSerializer serializer(&output);
EXPECT_TRUE(serializer.Serialize(value));
return base::SysUTF8ToNSString(output);
std::optional<std::string> output = base::WriteJson(value);
EXPECT_TRUE(output);
return base::SysUTF8ToNSString(*output);
}
base::Value::Dict ParsedField(std::string renderer_id,

@ -4,7 +4,9 @@
#import "ios/chrome/browser/policy/model/policy_earl_grey_utils.h"
#import "base/json/json_string_value_serializer.h"
#import <string>
#import "base/json/json_writer.h"
#import "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/policy/model/policy_app_interface.h"
@ -13,11 +15,8 @@ namespace {
// Returns a JSON-encoded string representing the given `base::Value`. If
// `value` is nullptr, returns a string representing a `base::Value` of type
// NONE.
std::string SerializeValue(const base::Value value) {
std::string serialized_value;
JSONStringValueSerializer serializer(&serialized_value);
serializer.Serialize(std::move(value));
return serialized_value;
std::string SerializeValue(const base::Value& value) {
return base::WriteJson(value).value_or(std::string());
}
// Merges the json policy corresponding to `policy_key` to the existing

@ -3,8 +3,10 @@
// found in the LICENSE file.
#import <memory>
#import <optional>
#import <string>
#import "base/json/json_string_value_serializer.h"
#import "base/json/json_reader.h"
#import "base/strings/string_util.h"
#import "base/strings/sys_string_conversions.h"
#import "components/enterprise/browser/enterprise_switches.h"
@ -24,12 +26,10 @@ namespace {
// Returns the value of a given policy, looked up in the current platform policy
// provider.
std::unique_ptr<base::Value> GetPlatformPolicy(const std::string& key) {
std::optional<base::Value> GetPlatformPolicy(const std::string& key) {
std::string json_representation = base::SysNSStringToUTF8(
[PolicyAppInterface valueForPlatformPolicy:base::SysUTF8ToNSString(key)]);
JSONStringValueDeserializer deserializer(json_representation);
return deserializer.Deserialize(/*error_code=*/nullptr,
/*error_message=*/nullptr);
return base::JSONReader::Read(json_representation);
}
// Returns an AppLaunchConfiguration containing the given policy data.
@ -88,14 +88,14 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data,
// Tests the values of policies that were explicitly set.
- (void)testPolicyExplicitlySet {
std::unique_ptr<base::Value> searchValue =
std::optional<base::Value> searchValue =
GetPlatformPolicy(policy::key::kDefaultSearchProviderName);
GREYAssertTrue(searchValue && searchValue->is_string(),
@"searchValue was not of type string");
GREYAssertEqual(searchValue->GetString(), std::string{"Test"},
@"searchValue had an unexpected value");
std::unique_ptr<base::Value> suggestValue =
std::optional<base::Value> suggestValue =
GetPlatformPolicy(policy::key::kSearchSuggestEnabled);
GREYAssertTrue(suggestValue && suggestValue->is_bool(),
@"suggestValue was not of type bool");
@ -106,7 +106,7 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data,
// Test the value of a policy that exists in the schema but was not explicitly
// set.
- (void)testPolicyNotSet {
std::unique_ptr<base::Value> blocklistValue =
std::optional<base::Value> blocklistValue =
GetPlatformPolicy(policy::key::kURLBlocklist);
GREYAssertTrue(blocklistValue && blocklistValue->is_none(),
@"blocklistValue was unexpectedly present");
@ -115,7 +115,7 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data,
// Test the value of a policy that was set in the configuration but is unknown
// to the policy system.
- (void)testPolicyUnknown {
std::unique_ptr<base::Value> unknownValue =
std::optional<base::Value> unknownValue =
GetPlatformPolicy("NotARegisteredPolicy");
GREYAssertTrue(unknownValue && unknownValue->is_string(),
@"unknownValue was not of type string");
@ -143,7 +143,7 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data,
// Ensure that policies can still be correctly set, and that the browser is
// working normally by visiting the about:policy page.
- (void)testPoliciesWork {
std::unique_ptr<base::Value> suggestValue =
std::optional<base::Value> suggestValue =
GetPlatformPolicy(policy::key::kSearchSuggestEnabled);
GREYAssertTrue(suggestValue && suggestValue->is_bool(),
@"suggestValue was not of type bool");
@ -175,7 +175,7 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data,
// Ensure that policies can still be correctly set, and that the browser is
// working normally by visiting the about:policy page.
- (void)testPoliciesWork {
std::unique_ptr<base::Value> suggestValue =
std::optional<base::Value> suggestValue =
GetPlatformPolicy(policy::key::kSearchSuggestEnabled);
GREYAssertTrue(suggestValue && suggestValue->is_bool(),
@"suggestValue was not of type bool");

@ -5,9 +5,9 @@
#import "ios/chrome/browser/webui/ui_bundled/policy/policy_ui.h"
#import <memory>
#import <optional>
#import <string>
#import "base/json/json_string_value_serializer.h"
#import "base/json/json_writer.h"
#import "components/grit/policy_resources.h"
#import "components/grit/policy_resources_map.h"
@ -178,10 +178,9 @@ web::WebUIIOSDataSource* CreatePolicyUIHtmlSource(ProfileIOS* profile) {
policy::Schema::Wrap(policy::GetChromeSchemaData());
base::Value::List policy_names = GetChromePolicyNames(profile);
std::string schema;
JSONStringValueSerializer serializer(&schema);
serializer.Serialize(PolicyUI::GetSchema(profile));
source->AddString("initialSchema", schema);
std::optional<std::string> schema =
base::WriteJson(PolicyUI::GetSchema(profile));
source->AddString("initialSchema", schema.value_or(std::string()));
// Strings for policy levels, scopes and sources.
static constexpr webui::LocalizedString kPolicyTestTypes[] = {

@ -6,6 +6,8 @@
#import <WebKit/WebKit.h>
#import <string>
#import "base/apple/foundation_util.h"
#import "base/barrier_closure.h"
#import "base/command_line.h"
@ -13,7 +15,7 @@
#import "base/files/file.h"
#import "base/files/file_util.h"
#import "base/ios/ios_util.h"
#import "base/json/json_string_value_serializer.h"
#import "base/json/json_writer.h"
#import "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#import "base/test/scoped_feature_list.h"
@ -117,10 +119,8 @@ NSString* SerializedPref(const PrefService::Preference* pref) {
const base::Value* value = pref ? pref->GetValue() : &none_value;
DCHECK(value);
std::string serialized_value;
JSONStringValueSerializer serializer(&serialized_value);
serializer.Serialize(*value);
return base::SysUTF8ToNSString(serialized_value);
return base::SysUTF8ToNSString(
base::WriteJson(*value).value_or(std::string()));
}
// Returns a JSON-encoded string representing the given `value`. If `value` is
// nullptr, returns a string representing a base::Value of type NONE.
@ -129,10 +129,8 @@ NSString* SerializedValue(const base::Value* value) {
const base::Value* result = value ? value : &none_value;
DCHECK(result);
std::string serialized_value;
JSONStringValueSerializer serializer(&serialized_value);
serializer.Serialize(*result);
return base::SysUTF8ToNSString(serialized_value);
return base::SysUTF8ToNSString(
base::WriteJson(*result).value_or(std::string()));
}
} // namespace