From a327ced6f76ed4b8e75da531b9c7a442f85f9469 Mon Sep 17 00:00:00 2001 From: Brian Johnson <34129+bridiver@users.noreply.github.com> Date: Tue, 15 Oct 2024 10:26:33 -0700 Subject: [PATCH] encapsulate rust json code in cpp (#25975) encapsulate rust json code in cpp so rust managed memory stays within the component --- components/brave_wallet/browser/BUILD.gn | 4 +- .../browser/bitcoin/bitcoin_rpc.cc | 2 +- .../bitcoin/bitcoin_transaction_unittest.cc | 4 +- .../browser/blockchain_registry.cc | 5 +- .../brave_wallet/browser/fil_requests.cc | 18 +- .../browser/fil_response_parser.cc | 2 +- .../brave_wallet/browser/fil_transaction.cc | 21 +- .../browser/json_rpc_response_parser.cc | 9 +- .../brave_wallet/browser/json_rpc_service.cc | 2 +- .../browser/meld_integration_service.cc | 2 +- .../browser/simple_hash_client.cc | 2 +- .../brave_wallet/browser/solana_requests.cc | 6 +- .../browser/solana_response_parser.cc | 2 +- .../browser/swap_request_helper.cc | 14 +- .../browser/swap_response_parser.cc | 2 +- components/brave_wallet/browser/test/BUILD.gn | 2 +- components/json/BUILD.gn | 20 +- components/json/json_export.h | 30 + components/json/json_helper.cc | 62 ++ components/json/json_helper.h | 47 ++ components/json/json_parser_unittest.cc | 572 +++++++----------- components/json/rs/BUILD.gn | 2 + components/json/rs/src/lib.rs | 2 +- 23 files changed, 422 insertions(+), 410 deletions(-) create mode 100644 components/json/json_export.h create mode 100644 components/json/json_helper.cc create mode 100644 components/json/json_helper.h diff --git a/components/brave_wallet/browser/BUILD.gn b/components/brave_wallet/browser/BUILD.gn index 7604e0a54e6b..2c756bef8b93 100644 --- a/components/brave_wallet/browser/BUILD.gn +++ b/components/brave_wallet/browser/BUILD.gn @@ -278,7 +278,7 @@ static_library("browser") { "//brave/components/decentralized_dns/core", "//brave/components/filecoin/rs:rust_lib", "//brave/components/ipfs", - "//brave/components/json/rs:rust_lib", + "//brave/components/json", "//brave/components/p3a_utils", "//brave/components/resources:strings_grit", "//brave/components/services/brave_wallet/public/cpp", @@ -458,7 +458,7 @@ source_set("transaction") { "//brave/components/brave_wallet/common", "//brave/components/brave_wallet/common:mojom", "//brave/components/filecoin/rs:rust_lib", - "//brave/components/json/rs:rust_lib", + "//brave/components/json", ] } diff --git a/components/brave_wallet/browser/bitcoin/bitcoin_rpc.cc b/components/brave_wallet/browser/bitcoin/bitcoin_rpc.cc index 8c07d79e9e24..63f10731e88f 100644 --- a/components/brave_wallet/browser/bitcoin/bitcoin_rpc.cc +++ b/components/brave_wallet/browser/bitcoin/bitcoin_rpc.cc @@ -19,7 +19,7 @@ #include "brave/components/brave_wallet/browser/json_rpc_response_parser.h" #include "brave/components/brave_wallet/browser/network_manager.h" #include "brave/components/brave_wallet/common/features.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" #include "components/grit/brave_components_strings.h" #include "net/http/http_request_headers.h" #include "services/network/public/cpp/shared_url_loader_factory.h" diff --git a/components/brave_wallet/browser/bitcoin/bitcoin_transaction_unittest.cc b/components/brave_wallet/browser/bitcoin/bitcoin_transaction_unittest.cc index 2512791bd551..ed16be25eb6c 100644 --- a/components/brave_wallet/browser/bitcoin/bitcoin_transaction_unittest.cc +++ b/components/brave_wallet/browser/bitcoin/bitcoin_transaction_unittest.cc @@ -13,7 +13,7 @@ #include "base/test/values_test_util.h" #include "brave/components/brave_wallet/browser/bitcoin/bitcoin_serializer.h" #include "brave/components/brave_wallet/browser/bitcoin_rpc_responses.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -88,7 +88,7 @@ TEST(BitcoinTransaction, TxInput_FromRpcUtxo) { )"; auto rpc_utxo = bitcoin_rpc::UnspentOutput::FromValue(base::test::ParseJson( - std::string(json::convert_all_numbers_to_string(rpc_utxo_json, "")))); + json::convert_all_numbers_to_string(rpc_utxo_json, ""))); ASSERT_TRUE(rpc_utxo); auto input = BitcoinTransaction::TxInput::FromRpcUtxo(kAddress1, *rpc_utxo); diff --git a/components/brave_wallet/browser/blockchain_registry.cc b/components/brave_wallet/browser/blockchain_registry.cc index 03a103462bd5..9e596d6b911a 100644 --- a/components/brave_wallet/browser/blockchain_registry.cc +++ b/components/brave_wallet/browser/blockchain_registry.cc @@ -19,7 +19,7 @@ #include "brave/components/brave_wallet/browser/network_manager.h" #include "brave/components/brave_wallet/browser/wallet_data_files_installer.h" #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" #include "net/base/url_util.h" #include "services/data_decoder/public/cpp/json_sanitizer.h" @@ -171,8 +171,7 @@ void DoParseDappLists(const base::FilePath& dir, ParseListsResult& out) { return; } - auto converted_json = - std::string(json::convert_all_numbers_to_string(*result, "")); + auto converted_json = json::convert_all_numbers_to_string(*result, ""); if (converted_json.empty()) { return; } diff --git a/components/brave_wallet/browser/fil_requests.cc b/components/brave_wallet/browser/fil_requests.cc index b98345d3ed44..0778e0fae6e8 100644 --- a/components/brave_wallet/browser/fil_requests.cc +++ b/components/brave_wallet/browser/fil_requests.cc @@ -14,7 +14,7 @@ #include "brave/components/brave_wallet/browser/fil_transaction.h" #include "brave/components/brave_wallet/browser/json_rpc_requests_helper.h" #include "brave/components/brave_wallet/common/fil_address.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet::fil { @@ -72,15 +72,9 @@ std::string getEstimateGas(const std::string& from_address, dict.Set("id", 1); std::string json; base::JSONWriter::Write(dict, &json); - json = std::string(json::convert_string_value_to_int64("/params/0/GasLimit", - json.c_str(), false) - .c_str()); - json = std::string(json::convert_string_value_to_uint64("/params/0/Nonce", - json.c_str(), false) - .c_str()); - return std::string(json::convert_string_value_to_uint64("/params/0/Method", - json.c_str(), false) - .c_str()); + json = json::convert_string_value_to_int64("/params/0/GasLimit", json, false); + json = json::convert_string_value_to_uint64("/params/0/Nonce", json, false); + return json::convert_string_value_to_uint64("/params/0/Method", json, false); } std::string getChainHead() { @@ -93,9 +87,7 @@ std::string getStateSearchMsgLimited(const std::string& cid, uint64_t period) { auto result = GetJsonRpcString("Filecoin.StateSearchMsgLimited", std::move(cid_value), base::Value(std::to_string(period))); - result = std::string( - json::convert_string_value_to_uint64("/params/1", result.c_str(), false) - .c_str()); + result = json::convert_string_value_to_uint64("/params/1", result, false); return result; } diff --git a/components/brave_wallet/browser/fil_response_parser.cc b/components/brave_wallet/browser/fil_response_parser.cc index ea78eb3c4e72..52dec02cb64e 100644 --- a/components/brave_wallet/browser/fil_response_parser.cc +++ b/components/brave_wallet/browser/fil_response_parser.cc @@ -11,7 +11,7 @@ #include "base/strings/string_number_conversions.h" #include "brave/components/brave_wallet/browser/json_rpc_response_parser.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet { diff --git a/components/brave_wallet/browser/fil_transaction.cc b/components/brave_wallet/browser/fil_transaction.cc index 383ae1af79b7..d83bcf204799 100644 --- a/components/brave_wallet/browser/fil_transaction.cc +++ b/components/brave_wallet/browser/fil_transaction.cc @@ -16,7 +16,7 @@ #include "base/strings/string_util.h" #include "base/values.h" #include "brave/components/filecoin/rs/src/lib.rs.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet { @@ -222,12 +222,10 @@ std::optional FilTransaction::ConvertMesssageStringFieldsToInt64( std::string converted_json = json::convert_string_value_to_int64(path + "/GasLimit", json, true) .c_str(); - converted_json = json::convert_string_value_to_uint64( - path + "/Nonce", converted_json.c_str(), true) - .c_str(); - converted_json = json::convert_string_value_to_uint64( - path + "/Method", converted_json.c_str(), true) - .c_str(); + converted_json = json::convert_string_value_to_uint64(path + "/Nonce", + converted_json, true); + converted_json = json::convert_string_value_to_uint64(path + "/Method", + converted_json, true); if (converted_json.empty()) { return std::nullopt; } @@ -245,12 +243,9 @@ std::optional FilTransaction::ConvertSignedTxStringFieldsToInt64( std::optional FilTransaction::DeserializeSignedTx( const std::string& signed_tx) { std::string json = - json::convert_int64_value_to_string("/Message/GasLimit", signed_tx, true) - .c_str(); - json = - json::convert_int64_value_to_string("/Message/Nonce", json, true).c_str(); - json = json::convert_int64_value_to_string("/Message/Method", json, true) - .c_str(); + json::convert_int64_value_to_string("/Message/GasLimit", signed_tx, true); + json = json::convert_int64_value_to_string("/Message/Nonce", json, true); + json = json::convert_int64_value_to_string("/Message/Method", json, true); return base::JSONReader::Read(json); } diff --git a/components/brave_wallet/browser/json_rpc_response_parser.cc b/components/brave_wallet/browser/json_rpc_response_parser.cc index 4a51cf290a0c..4de1eca8e076 100644 --- a/components/brave_wallet/browser/json_rpc_response_parser.cc +++ b/components/brave_wallet/browser/json_rpc_response_parser.cc @@ -10,7 +10,7 @@ #include "brave/components/brave_wallet/browser/brave_wallet_constants.h" #include "brave/components/brave_wallet/common/hex_utils.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet { @@ -143,8 +143,8 @@ std::optional ConvertMultiUint64InObjectArrayToString( if (key.empty()) { return std::nullopt; } - converted_json = std::string(json::convert_uint64_in_object_array_to_string( - path_to_list, path_to_object, key, converted_json)); + converted_json = json::convert_uint64_in_object_array_to_string( + path_to_list, path_to_object, key, converted_json); if (converted_json.empty()) { return std::nullopt; } @@ -195,8 +195,7 @@ bool GetUint64FromDictValue(const base::Value::Dict& dict_value, std::optional ConvertAllNumbersToString(const std::string& path, const std::string& json) { - auto converted_json = - std::string(json::convert_all_numbers_to_string(json, path)); + auto converted_json = json::convert_all_numbers_to_string(json, path); if (converted_json.empty()) { return std::nullopt; } diff --git a/components/brave_wallet/browser/json_rpc_service.cc b/components/brave_wallet/browser/json_rpc_service.cc index 2bc4689f6d83..73cfdfc48052 100644 --- a/components/brave_wallet/browser/json_rpc_service.cc +++ b/components/brave_wallet/browser/json_rpc_service.cc @@ -49,7 +49,7 @@ #include "brave/components/brave_wallet/common/hex_utils.h" #include "brave/components/decentralized_dns/core/constants.h" #include "brave/components/decentralized_dns/core/utils.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" #include "brave/net/base/url_util.h" #include "components/grit/brave_components_strings.h" #include "components/prefs/pref_service.h" diff --git a/components/brave_wallet/browser/meld_integration_service.cc b/components/brave_wallet/browser/meld_integration_service.cc index 8782fbd9ba98..0b14f0541ab8 100644 --- a/components/brave_wallet/browser/meld_integration_service.cc +++ b/components/brave_wallet/browser/meld_integration_service.cc @@ -29,7 +29,7 @@ #include "brave/components/brave_wallet/common/buildflags.h" #include "brave/components/brave_wallet/common/meld_integration.mojom-forward.h" #include "brave/components/constants/brave_services_key.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" #include "net/base/url_util.h" #include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/simple_url_loader.h" diff --git a/components/brave_wallet/browser/simple_hash_client.cc b/components/brave_wallet/browser/simple_hash_client.cc index 879c6e7b8578..2b06e5ee5e30 100644 --- a/components/brave_wallet/browser/simple_hash_client.cc +++ b/components/brave_wallet/browser/simple_hash_client.cc @@ -22,7 +22,7 @@ #include "brave/components/brave_wallet/common/solana_utils.h" #include "brave/components/brave_wallet/common/string_utils.h" #include "brave/components/constants/brave_services_key.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" #include "net/base/url_util.h" #include "services/network/public/cpp/shared_url_loader_factory.h" diff --git a/components/brave_wallet/browser/solana_requests.cc b/components/brave_wallet/browser/solana_requests.cc index 7480d613bfb9..84d73460e77b 100644 --- a/components/brave_wallet/browser/solana_requests.cc +++ b/components/brave_wallet/browser/solana_requests.cc @@ -11,7 +11,7 @@ #include "base/strings/string_number_conversions.h" #include "brave/components/brave_wallet/browser/json_rpc_requests_helper.h" #include "brave/components/brave_wallet/common/solana_utils.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet::solana { @@ -51,8 +51,8 @@ std::string sendTransaction( base::Value::Dict dictionary = GetJsonRpcDictionary("sendTransaction", std::move(params)); - return std::string(json::convert_string_value_to_uint64( - "/params/1/maxRetries", GetJSON(dictionary), true)); + return json::convert_string_value_to_uint64("/params/1/maxRetries", + GetJSON(dictionary), true); } std::string getLatestBlockhash() { diff --git a/components/brave_wallet/browser/solana_response_parser.cc b/components/brave_wallet/browser/solana_response_parser.cc index ca824cec05cd..570858b0174e 100644 --- a/components/brave_wallet/browser/solana_response_parser.cc +++ b/components/brave_wallet/browser/solana_response_parser.cc @@ -15,7 +15,7 @@ #include "base/values.h" #include "brave/components/brave_wallet/browser/json_rpc_response_parser.h" #include "brave/components/brave_wallet/common/brave_wallet_types.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet { diff --git a/components/brave_wallet/browser/swap_request_helper.cc b/components/brave_wallet/browser/swap_request_helper.cc index 0c0f63b35e42..d7b02619116c 100644 --- a/components/brave_wallet/browser/swap_request_helper.cc +++ b/components/brave_wallet/browser/swap_request_helper.cc @@ -19,7 +19,7 @@ #include "brave/components/brave_wallet/common/brave_wallet_constants.h" #include "brave/components/brave_wallet/common/encoding_utils.h" #include "brave/components/brave_wallet/common/hex_utils.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet { @@ -121,18 +121,18 @@ std::optional EncodeTransactionParams( // FIXME - GetJSON should be refactored to accept a base::Value::Dict std::string result = GetJSON(base::Value(std::move(tx_params))); - result = std::string(json::convert_string_value_to_uint64( - "/quoteResponse/slippageBps", result, false)); + result = json::convert_string_value_to_uint64("/quoteResponse/slippageBps", + result, false); if (params.quote->platform_fee) { - result = std::string(json::convert_string_value_to_uint64( - "/quoteResponse/platformFee/feeBps", result, false)); + result = json::convert_string_value_to_uint64( + "/quoteResponse/platformFee/feeBps", result, false); } for (int i = 0; i < static_cast(params.quote->route_plan.size()); i++) { - result = std::string(json::convert_string_value_to_uint64( + result = json::convert_string_value_to_uint64( base::StringPrintf("/quoteResponse/routePlan/%d/percent", i), result, - false)); + false); } return result; diff --git a/components/brave_wallet/browser/swap_response_parser.cc b/components/brave_wallet/browser/swap_response_parser.cc index 392e7e9c79ba..b221a2c7e101 100644 --- a/components/brave_wallet/browser/swap_response_parser.cc +++ b/components/brave_wallet/browser/swap_response_parser.cc @@ -21,7 +21,7 @@ #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" #include "brave/components/brave_wallet/common/hex_utils.h" #include "brave/components/brave_wallet/common/string_utils.h" -#include "brave/components/json/rs/src/lib.rs.h" +#include "brave/components/json/json_helper.h" namespace brave_wallet { diff --git a/components/brave_wallet/browser/test/BUILD.gn b/components/brave_wallet/browser/test/BUILD.gn index 5d0b039eb0e0..413431988cde 100644 --- a/components/brave_wallet/browser/test/BUILD.gn +++ b/components/brave_wallet/browser/test/BUILD.gn @@ -139,7 +139,7 @@ source_set("brave_wallet_unit_tests") { "//brave/components/decentralized_dns/core", "//brave/components/filecoin/rs:rust_lib", "//brave/components/ipfs", - "//brave/components/json/rs:rust_lib", + "//brave/components/json", "//brave/components/resources:strings_grit", "//brave/components/services/brave_wallet/public/cpp/utils", "//brave/components/services/brave_wallet/public/mojom", diff --git a/components/json/BUILD.gn b/components/json/BUILD.gn index 84704c908aa7..71db61abf9fc 100644 --- a/components/json/BUILD.gn +++ b/components/json/BUILD.gn @@ -5,11 +5,27 @@ import("//testing/test.gni") +component("json") { + sources = [ + "json_export.h", + "json_helper.cc", + "json_helper.h", + ] + + defines = [ "JSON_HELPER_IMPLEMENTATION" ] + + deps = [ + "rs:rust_lib", + "//base", + ] +} + source_set("brave_json_unit_tests") { testonly = true sources = [ "//brave/components/json/json_parser_unittest.cc" ] deps = [ - "//brave/components/json/rs:rust_lib", + "//base", + "//brave/components/json", "//testing/gtest", ] -} # source_set("brave_wallet_unit_tests") +} diff --git a/components/json/json_export.h b/components/json/json_export.h new file mode 100644 index 000000000000..ea2ecb34b3cd --- /dev/null +++ b/components/json/json_export.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_JSON_JSON_EXPORT_H_ +#define BRAVE_COMPONENTS_JSON_JSON_EXPORT_H_ + +#if defined(COMPONENT_BUILD) +#if defined(WIN32) + +#if defined(JSON_HELPER_IMPLEMENTATION) +#define JSON_HELPER_EXPORT __declspec(dllexport) +#else +#define JSON_HELPER_EXPORT __declspec(dllimport) +#endif // defined(JSON_HELPER_IMPLEMENTATION) + +#else // defined(WIN32) +#if defined(JSON_HELPER_IMPLEMENTATION) +#define JSON_HELPER_EXPORT __attribute__((visibility("default"))) +#else +#define JSON_HELPER_EXPORT +#endif // defined(JSON_HELPER_IMPLEMENTATION) +#endif + +#else // defined(COMPONENT_BUILD) +#define JSON_HELPER_EXPORT +#endif + +#endif // BRAVE_COMPONENTS_JSON_JSON_EXPORT_H_ diff --git a/components/json/json_helper.cc b/components/json/json_helper.cc new file mode 100644 index 000000000000..29ee119d6465 --- /dev/null +++ b/components/json/json_helper.cc @@ -0,0 +1,62 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "brave/components/json/json_helper.h" + +#include "brave/components/json/rs/src/lib.rs.h" + +namespace json { + +std::string convert_uint64_value_to_string(const std::string& path, + const std::string& json, + bool optional) { + return std::string( + rust_json::convert_uint64_value_to_string(path, json, optional)); +} + +std::string convert_int64_value_to_string(const std::string& path, + const std::string& json, + bool optional) { + return std::string( + rust_json::convert_int64_value_to_string(path, json, optional)); +} + +std::string convert_string_value_to_uint64(const std::string& path, + const std::string& json, + bool optional) { + return std::string( + rust_json::convert_string_value_to_uint64(path, json, optional)); +} + +std::string convert_string_value_to_int64(const std::string& path, + const std::string& json, + bool optional) { + return std::string( + rust_json::convert_string_value_to_int64(path, json, optional)); +} + +std::string convert_uint64_in_object_array_to_string( + const std::string& path_to_list, + const std::string& path_to_object, + const std::string& key, + const std::string& json) { + return std::string(rust_json::convert_uint64_in_object_array_to_string( + path_to_list, path_to_object, key, json)); +} + +std::string convert_all_numbers_to_string(const std::string& json, + const std::string& path) { + return std::string(rust_json::convert_all_numbers_to_string(json, path)); +} + +std::string convert_all_numbers_to_string_and_remove_null_values( + const std::string& json, + const std::string& path) { + return std::string( + rust_json::convert_all_numbers_to_string_and_remove_null_values(json, + path)); +} + +} // namespace json diff --git a/components/json/json_helper.h b/components/json/json_helper.h new file mode 100644 index 000000000000..6b8b65d7f704 --- /dev/null +++ b/components/json/json_helper.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_JSON_JSON_HELPER_H_ +#define BRAVE_COMPONENTS_JSON_JSON_HELPER_H_ + +#include + +#include "brave/components/json/json_export.h" + +namespace json { + +JSON_HELPER_EXPORT std::string convert_uint64_value_to_string( + const std::string& path, + const std::string& json, + bool optional); +JSON_HELPER_EXPORT std::string convert_int64_value_to_string( + const std::string& path, + const std::string& json, + bool optional); +JSON_HELPER_EXPORT std::string convert_string_value_to_uint64( + const std::string& path, + const std::string& json, + bool optional); +JSON_HELPER_EXPORT std::string convert_string_value_to_int64( + const std::string& path, + const std::string& json, + bool optional); +JSON_HELPER_EXPORT std::string convert_uint64_in_object_array_to_string( + const std::string& path_to_list, + const std::string& path_to_object, + const std::string& key, + const std::string& json); + +JSON_HELPER_EXPORT std::string convert_all_numbers_to_string( + const std::string& json, + const std::string& path); + +JSON_HELPER_EXPORT std::string +convert_all_numbers_to_string_and_remove_null_values(const std::string& json, + const std::string& path); + +} // namespace json + +#endif // BRAVE_COMPONENTS_JSON_JSON_HELPER_H_ diff --git a/components/json/json_parser_unittest.cc b/components/json/json_parser_unittest.cc index 855936451011..bea123d820e1 100644 --- a/components/json/json_parser_unittest.cc +++ b/components/json/json_parser_unittest.cc @@ -3,498 +3,373 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "brave/components/json/rs/src/lib.rs.h" +#include +#include + +#include "base/strings/string_number_conversions.h" +#include "brave/components/json/json_helper.h" #include "testing/gtest/include/gtest/gtest.h" namespace brave_wallet { TEST(JsonParser, ConvertUint64ToString) { - std::string json = "{\"a\": " + std::to_string(UINT64_MAX) + "}"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a", json, false)), - R"({"a":"18446744073709551615"})"); + std::string json = + "{\"a\": " + base::NumberToString(std::numeric_limits::max()) + + "}"; + EXPECT_EQ(json::convert_uint64_value_to_string("/a", json, false), + R"({"a":"18446744073709551615"})"); - // UINT64_MAX + 1 + // std::numeric_limits::max + 1 json = "{\"a\": 18446744073709551616 }"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, true)) - .empty()); - - // INT64_MIN - json = "{\"a\": " + std::to_string(INT64_MIN) + "}"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, true).empty()); + + // std::numeric_limits::min() + json = + "{\"a\": " + base::NumberToString(std::numeric_limits::min()) + + "}"; + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, true).empty()); json = R"({"a": { "b/a": 1, "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a/b~1a", json, false)), - R"({"a":{"b/a":"1","c":2},"d":"string"})"); + EXPECT_EQ(json::convert_uint64_value_to_string("/a/b~1a", json, false), + R"({"a":{"b/a":"1","c":2},"d":"string"})"); json = R"({"a": { "b~a": 1, "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a/b~0a", json, false)), - R"({"a":{"b~a":"1","c":2},"d":"string"})"); + EXPECT_EQ(json::convert_uint64_value_to_string("/a/b~0a", json, false), + R"({"a":{"b~a":"1","c":2},"d":"string"})"); json = R"({"a": { "b": 1, "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a/b", json, false)), - R"({"a":{"b":"1","c":2},"d":"string"})"); + EXPECT_EQ(json::convert_uint64_value_to_string("/a/b", json, false), + R"({"a":{"b":"1","c":2},"d":"string"})"); json = R"({"a": { "b": [{"e":1}], "c": 2 }, "d": "string"})"; - EXPECT_EQ(std::string( - json::convert_uint64_value_to_string("/a/b/0/e", json, false)), + EXPECT_EQ(json::convert_uint64_value_to_string("/a/b/0/e", json, false), R"({"a":{"b":[{"e":"1"}],"c":2},"d":"string"})"); json = R"({"a":[{"b":1}]})"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a/0/b", json, false)), - R"({"a":[{"b":"1"}]})"); + EXPECT_EQ(json::convert_uint64_value_to_string("/a/0/b", json, false), + R"({"a":[{"b":"1"}]})"); json = R"({"a":[1]})"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a/0", json, false)), - R"({"a":["1"]})"); + EXPECT_EQ(json::convert_uint64_value_to_string("/a/0", json, false), + R"({"a":["1"]})"); json = R"({"a": 0 })"; - EXPECT_EQ( - std::string(json::convert_uint64_value_to_string("/a", json, false)), - R"({"a":"0"})"); + EXPECT_EQ(json::convert_uint64_value_to_string("/a", json, false), + R"({"a":"0"})"); json = R"({"a": 0.1 })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"a": "1" })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"a": "" })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"a": -1.0 })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"a": "a" })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"b": 1 })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"a": [] })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); json = R"({"a": {} })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty()); // Optional, unchanged if path not found or value is null. json = R"({"b": 1 })"; - EXPECT_EQ(std::string(json::convert_uint64_value_to_string("/a", json, true)), - json); + EXPECT_EQ(json::convert_uint64_value_to_string("/a", json, true), json); json = R"({"a": null })"; - EXPECT_EQ(std::string(json::convert_uint64_value_to_string("/a", json, true)), - json); + EXPECT_EQ(json::convert_uint64_value_to_string("/a", json, true), json); // Wrong value type should still fail. json = R"({"a": "1" })"; - EXPECT_TRUE( - std::string(json::convert_uint64_value_to_string("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, true).empty()); } TEST(JsonParser, ConvertInt64ToString) { std::string json = "{\"a\": 18446744073709551615 }"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); - // INT64_MIN - json = "{\"a\": " + std::to_string(INT64_MIN) + "}"; - EXPECT_EQ(std::string(json::convert_int64_value_to_string("/a", json, false)), + // std::numeric_limits::min() + json = + "{\"a\": " + base::NumberToString(std::numeric_limits::min()) + + "}"; + EXPECT_EQ(json::convert_int64_value_to_string("/a", json, false), R"({"a":"-9223372036854775808"})"); - // INT64_MIN - 1 + // std::numeric_limits::min() - 1 json = "{\"a\": -9223372036854775809 }"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); - EXPECT_TRUE(std::string(json::convert_int64_value_to_string("/a", json, true)) - .empty()); - - // INT64_MAX - json = "{\"a\": " + std::to_string(INT64_MAX) + "}"; - EXPECT_EQ(std::string(json::convert_int64_value_to_string("/a", json, false)), + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, true).empty()); + + // std::numeric_limits::max() + json = + "{\"a\": " + base::NumberToString(std::numeric_limits::max()) + + "}"; + EXPECT_EQ(json::convert_int64_value_to_string("/a", json, false), R"({"a":"9223372036854775807"})"); json = R"({"a": { "b/a": 1, "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_int64_value_to_string("/a/b~1a", json, false)), - R"({"a":{"b/a":"1","c":2},"d":"string"})"); + EXPECT_EQ(json::convert_int64_value_to_string("/a/b~1a", json, false), + R"({"a":{"b/a":"1","c":2},"d":"string"})"); json = R"({"a": { "b~a": 1, "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_int64_value_to_string("/a/b~0a", json, false)), - R"({"a":{"b~a":"1","c":2},"d":"string"})"); + EXPECT_EQ(json::convert_int64_value_to_string("/a/b~0a", json, false), + R"({"a":{"b~a":"1","c":2},"d":"string"})"); json = R"({"a": { "b": 1, "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_int64_value_to_string("/a/b", json, false)), - R"({"a":{"b":"1","c":2},"d":"string"})"); + EXPECT_EQ(json::convert_int64_value_to_string("/a/b", json, false), + R"({"a":{"b":"1","c":2},"d":"string"})"); json = R"({"a": { "b": [{"e":1}], "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_int64_value_to_string("/a/b/0/e", json, false)), - R"({"a":{"b":[{"e":"1"}],"c":2},"d":"string"})"); + EXPECT_EQ(json::convert_int64_value_to_string("/a/b/0/e", json, false), + R"({"a":{"b":[{"e":"1"}],"c":2},"d":"string"})"); json = R"({"a":[{"b":1}]})"; - EXPECT_EQ( - std::string(json::convert_int64_value_to_string("/a/0/b", json, false)), - R"({"a":[{"b":"1"}]})"); + EXPECT_EQ(json::convert_int64_value_to_string("/a/0/b", json, false), + R"({"a":[{"b":"1"}]})"); json = R"({"a":[1]})"; - EXPECT_EQ( - std::string(json::convert_int64_value_to_string("/a/0", json, false)), - R"({"a":["1"]})"); + EXPECT_EQ(json::convert_int64_value_to_string("/a/0", json, false), + R"({"a":["1"]})"); json = R"({"a": 0 })"; - EXPECT_EQ(std::string(json::convert_int64_value_to_string("/a", json, false)), + EXPECT_EQ(json::convert_int64_value_to_string("/a", json, false), R"({"a":"0"})"); json = R"({"a": 0.1 })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"a": "1" })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"a": "" })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"a": -1.0 })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"a": "a" })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"b": 1 })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"a": [] })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); json = R"({"a": {} })"; - EXPECT_TRUE( - std::string(json::convert_int64_value_to_string("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, false).empty()); // Optional, unchanged if path not found or value is null. json = R"({"b": 1 })"; - EXPECT_EQ(std::string(json::convert_int64_value_to_string("/a", json, true)), - json); + EXPECT_EQ(json::convert_int64_value_to_string("/a", json, true), json); json = R"({"a": null })"; - EXPECT_EQ(std::string(json::convert_int64_value_to_string("/a", json, true)), - json); + EXPECT_EQ(json::convert_int64_value_to_string("/a", json, true), json); // Wrong value type should still fail. json = R"({"a": "1" })"; - EXPECT_TRUE(std::string(json::convert_int64_value_to_string("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_int64_value_to_string("/a", json, true).empty()); } TEST(JsonParser, ConvertStringToUint64) { - // UINT64_MAX + // std::numeric_limits::max std::string json = R"({"a":"18446744073709551615"})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a", json, false)), - R"({"a":18446744073709551615})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a", json, false), + R"({"a":18446744073709551615})"); json = R"({"a":"-1"})"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, true)) - .empty()); - - // UINT64_MAX + 1 + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, true).empty()); + + // std::numeric_limits::max + 1 json = R"({\"a\":18446744073709551616})"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, true).empty()); json = R"({"a": { "b": "1", "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a/b", json, false)), - R"({"a":{"b":1,"c":2},"d":"string"})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a/b", json, false), + R"({"a":{"b":1,"c":2},"d":"string"})"); json = R"({"a": { "b": [{"e":"1"}], "c": 2 }, "d": "string"})"; - EXPECT_EQ(std::string( - json::convert_string_value_to_uint64("/a/b/0/e", json, false)), + EXPECT_EQ(json::convert_string_value_to_uint64("/a/b/0/e", json, false), R"({"a":{"b":[{"e":1}],"c":2},"d":"string"})"); json = R"({"a~c": { "b": "1", "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a~0c/b", json, false)), - R"({"a~c":{"b":1,"c":2},"d":"string"})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a~0c/b", json, false), + R"({"a~c":{"b":1,"c":2},"d":"string"})"); json = R"({"a/d": { "b": [{"e":"1"}], "c": 2 }, "d": "string"})"; - EXPECT_EQ(std::string(json::convert_string_value_to_uint64("/a~1d/b/0/e", - json, false)), + EXPECT_EQ(json::convert_string_value_to_uint64("/a~1d/b/0/e", json, false), R"({"a/d":{"b":[{"e":1}],"c":2},"d":"string"})"); json = R"({"a": { "b": "1" }})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a/b", json, false)), - R"({"a":{"b":1}})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a/b", json, false), + R"({"a":{"b":1}})"); json = R"({"a":[{"b":"1"}]})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a/0/b", json, false)), - R"({"a":[{"b":1}]})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a/0/b", json, false), + R"({"a":[{"b":1}]})"); json = R"({"a":["1"]})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a/0", json, false)), - R"({"a":[1]})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a/0", json, false), + R"({"a":[1]})"); json = R"({"a": "0" })"; - EXPECT_EQ( - std::string(json::convert_string_value_to_uint64("/a", json, false)), - R"({"a":0})"); + EXPECT_EQ(json::convert_string_value_to_uint64("/a", json, false), + R"({"a":0})"); json = R"({"a": 1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"a": 0.1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"a": "" })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"a": -1.0 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"a": "a" })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"b": 1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"a": [] })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); json = R"({"a": {} })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, false).empty()); // Optional, unchanged if path not found or value is null. json = R"({"b": "1" })"; - EXPECT_EQ(std::string(json::convert_string_value_to_uint64("/a", json, true)), - json); + EXPECT_EQ(json::convert_string_value_to_uint64("/a", json, true), json); json = R"({"a": null })"; - EXPECT_EQ(std::string(json::convert_string_value_to_uint64("/a", json, true)), - json); + EXPECT_EQ(json::convert_string_value_to_uint64("/a", json, true), json); // Wrong value type should still fail. json = R"({"a": 1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_uint64("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_uint64("/a", json, true).empty()); } TEST(JsonParser, ConvertStringToInt64) { - // INT64_MIN + // std::numeric_limits::min() std::string json = R"({"a":"-9223372036854775808"})"; - EXPECT_EQ(std::string(json::convert_string_value_to_int64("/a", json, false)), + EXPECT_EQ(json::convert_string_value_to_int64("/a", json, false), R"({"a":-9223372036854775808})"); - // INT64_MIN - 1 + // std::numeric_limits::min() - 1 json = R"({"a":"-9223372036854775809"})"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); - EXPECT_TRUE(std::string(json::convert_string_value_to_int64("/a", json, true)) - .empty()); - - // INT64_MAX - json = "{\"a\": \"" + std::to_string(INT64_MAX) + "\"}"; - EXPECT_EQ(std::string(json::convert_string_value_to_int64("/a", json, false)), + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, true).empty()); + + // std::numeric_limits::max() + json = "{\"a\": \"" + + base::NumberToString(std::numeric_limits::max()) + "\"}"; + EXPECT_EQ(json::convert_string_value_to_int64("/a", json, false), R"({"a":9223372036854775807})"); - // INT64_MAX + 1 + // std::numeric_limits::max() + 1 json = "{\"a\": \"9223372036854775808\"}"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = "{\"a\": \"9223372036854775808\"}"; - EXPECT_TRUE(std::string(json::convert_string_value_to_int64("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, true).empty()); json = R"({"a": { "b": "1", "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_int64("/a/b", json, false)), - R"({"a":{"b":1,"c":2},"d":"string"})"); + EXPECT_EQ(json::convert_string_value_to_int64("/a/b", json, false), + R"({"a":{"b":1,"c":2},"d":"string"})"); json = R"({"a": { "b": [{"e":"1"}], "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_int64("/a/b/0/e", json, false)), - R"({"a":{"b":[{"e":1}],"c":2},"d":"string"})"); + EXPECT_EQ(json::convert_string_value_to_int64("/a/b/0/e", json, false), + R"({"a":{"b":[{"e":1}],"c":2},"d":"string"})"); json = R"({"a~e": { "b": "1", "c": 2 }, "d": "string"})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_int64("/a~0e/b", json, false)), - R"({"a~e":{"b":1,"c":2},"d":"string"})"); + EXPECT_EQ(json::convert_string_value_to_int64("/a~0e/b", json, false), + R"({"a~e":{"b":1,"c":2},"d":"string"})"); json = R"({"a/e": { "b": [{"e":"1"}], "c": 2 }, "d": "string"})"; - EXPECT_EQ(std::string(json::convert_string_value_to_int64("/a~1e/b/0/e", json, - false)), + EXPECT_EQ(json::convert_string_value_to_int64("/a~1e/b/0/e", json, false), R"({"a/e":{"b":[{"e":1}],"c":2},"d":"string"})"); json = R"({"a": { "b": "1" }})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_int64("/a/b", json, false)), - R"({"a":{"b":1}})"); + EXPECT_EQ(json::convert_string_value_to_int64("/a/b", json, false), + R"({"a":{"b":1}})"); json = R"({"a":[{"b":"1"}]})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_int64("/a/0/b", json, false)), - R"({"a":[{"b":1}]})"); + EXPECT_EQ(json::convert_string_value_to_int64("/a/0/b", json, false), + R"({"a":[{"b":1}]})"); json = R"({"a":["1"]})"; - EXPECT_EQ( - std::string(json::convert_string_value_to_int64("/a/0", json, false)), - R"({"a":[1]})"); + EXPECT_EQ(json::convert_string_value_to_int64("/a/0", json, false), + R"({"a":[1]})"); json = R"({"a": "0" })"; - EXPECT_EQ(std::string(json::convert_string_value_to_int64("/a", json, false)), + EXPECT_EQ(json::convert_string_value_to_int64("/a", json, false), R"({"a":0})"); json = R"({"a": 1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"a": 0.1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"a": "" })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"a": -1.0 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"a": "a" })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"b": 1 })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"a": [] })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); json = R"({"a": {} })"; - EXPECT_TRUE( - std::string(json::convert_string_value_to_int64("/a", json, false)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty()); // Optional, unchanged if path not found or value is null. json = R"({"b": "1" })"; - EXPECT_EQ(std::string(json::convert_string_value_to_int64("/a", json, true)), - json); + EXPECT_EQ(json::convert_string_value_to_int64("/a", json, true), json); json = R"({"a": null })"; - EXPECT_EQ(std::string(json::convert_string_value_to_int64("/a", json, true)), - json); + EXPECT_EQ(json::convert_string_value_to_int64("/a", json, true), json); // Wrong value type should still fail. json = R"({"a": 1 })"; - EXPECT_TRUE(std::string(json::convert_string_value_to_int64("/a", json, true)) - .empty()); + EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, true).empty()); } TEST(JsonParser, ConvertUint64InObjectArrayToString) { std::string json( R"({"a":[{"key":18446744073709551615},{"key":2},{"key":3}]})"); EXPECT_EQ( - std::string(json::convert_uint64_in_object_array_to_string("/a", "", - "key", json)), + json::convert_uint64_in_object_array_to_string("/a", "", "key", json), R"({"a":[{"key":"18446744073709551615"},{"key":"2"},{"key":"3"}]})"); json = R"({"a":{"b":[{"key":18446744073709551615},{"key":2}]}})"; - EXPECT_EQ(std::string(json::convert_uint64_in_object_array_to_string( - "/a/b", "", "key", json)), - R"({"a":{"b":[{"key":"18446744073709551615"},{"key":"2"}]}})"); + EXPECT_EQ( + json::convert_uint64_in_object_array_to_string("/a/b", "", "key", json), + R"({"a":{"b":[{"key":"18446744073709551615"},{"key":"2"}]}})"); // Null value support. json = R"({"a":[{"key":18446744073709551615},{"key":null}]})"; - EXPECT_EQ(std::string(json::convert_uint64_in_object_array_to_string( - "/a", "", "key", json)), - R"({"a":[{"key":"18446744073709551615"},{"key":null}]})"); + EXPECT_EQ( + json::convert_uint64_in_object_array_to_string("/a", "", "key", json), + R"({"a":[{"key":"18446744073709551615"},{"key":null}]})"); json = R"({"a":[{"b":{"key":18446744073709551615}},{"b":{"key":null}}]})"; EXPECT_EQ( - std::string(json::convert_uint64_in_object_array_to_string("/a", "/b", - "key", json)), + json::convert_uint64_in_object_array_to_string("/a", "/b", "key", json), R"({"a":[{"b":{"key":"18446744073709551615"}},{"b":{"key":null}}]})"); // Empty object array, nothing to convert. json = R"({"a":[]})"; - EXPECT_EQ(std::string(json::convert_uint64_in_object_array_to_string( - "/a", "", "key", json)), - json); + EXPECT_EQ( + json::convert_uint64_in_object_array_to_string("/a", "", "key", json), + json); // Unchanged when path is not found. json = R"({"b":[{"key":1},{"key":2}]})"; - EXPECT_EQ(std::string(json::convert_uint64_in_object_array_to_string( - "/a", "", "key", json)), - json); + EXPECT_EQ( + json::convert_uint64_in_object_array_to_string("/a", "", "key", json), + json); json = R"({"b":[{"c": {"key":1}},{"c": {"key":2}}]})"; - EXPECT_EQ(std::string(json::convert_uint64_in_object_array_to_string( - "/a", "", "key", json)), - json); + EXPECT_EQ( + json::convert_uint64_in_object_array_to_string("/a", "", "key", json), + json); // When key is not found in some of the objects in the array, no need to // convert those objects. json = R"({"a":[{"key":1},{"diff-key":1},{"key":2}]})"; - EXPECT_EQ(std::string(json::convert_uint64_in_object_array_to_string( - "/a", "", "key", json)), - R"({"a":[{"key":"1"},{"diff-key":1},{"key":"2"}]})"); + EXPECT_EQ( + json::convert_uint64_in_object_array_to_string("/a", "", "key", json), + R"({"a":[{"key":"1"},{"diff-key":1},{"key":"2"}]})"); json = R"({"a":[{"b":{"key":1}},{"b":{"diff-key":1}},{"b":{"key":2}}]})"; EXPECT_EQ( - std::string(json::convert_uint64_in_object_array_to_string("/a", "/b", - "key", json)), + json::convert_uint64_in_object_array_to_string("/a", "/b", "key", json), R"({"a":[{"b":{"key":"1"}},{"b":{"diff-key":1}},{"b":{"key":"2"}}]})"); std::vector invalid_cases = { @@ -504,13 +379,14 @@ TEST(JsonParser, ConvertUint64InObjectArrayToString) { R"({"a":[{"key":1}, [], {"key":2}})", // Value at key is not uint64 or null. R"({"a":[{"key":"1"}]})", - // UINT64_MAX + 1 + // std::numeric_limits::max + 1 R"("{a":[{"key":18446744073709551616}]})", - // INT64_MIN - R"("{a":[{"key":)" + std::to_string(INT64_MIN) + "}]}"}; + // std::numeric_limits::min() + R"("{a":[{"key":)" + + base::NumberToString(std::numeric_limits::min()) + "}]}"}; for (const auto& invalid_case : invalid_cases) { - EXPECT_EQ("", std::string(json::convert_uint64_in_object_array_to_string( - "/a", "", "key", invalid_case))) + EXPECT_EQ("", json::convert_uint64_in_object_array_to_string( + "/a", "", "key", invalid_case)) << invalid_case; } @@ -521,13 +397,14 @@ TEST(JsonParser, ConvertUint64InObjectArrayToString) { R"({"a":[{"key":1}, [], {"key":2}})", // Value at key is not uint64 or null. R"({"a":[{"b": {"key":"1"}}]})", - // UINT64_MAX + 1 + // std::numeric_limits::max + 1 R"("{a":[{"b": {"key":18446744073709551616}}]})", - // INT64_MIN - R"("{a":[{"b": {"key":)" + std::to_string(INT64_MIN) + "}}]}"}; + // std::numeric_limits::min() + R"("{a":[{"b": {"key":)" + + base::NumberToString(std::numeric_limits::min()) + "}}]}"}; for (const auto& invalid_case : invalid_cases) { - EXPECT_EQ("", std::string(json::convert_uint64_in_object_array_to_string( - "/a", "/b", "key", invalid_case))) + EXPECT_EQ("", json::convert_uint64_in_object_array_to_string( + "/a", "/b", "key", invalid_case)) << invalid_case; } @@ -535,16 +412,14 @@ TEST(JsonParser, ConvertUint64InObjectArrayToString) { json = R"({"a":[{"b":{"key":18446744073709551615}},{"b":{"key":2}},{"b":{"key":3}}]})"; EXPECT_EQ( - std::string(json::convert_uint64_in_object_array_to_string("/a", "/b", - "key", json)), + json::convert_uint64_in_object_array_to_string("/a", "/b", "key", json), R"({"a":[{"b":{"key":"18446744073709551615"}},{"b":{"key":"2"}},{"b":{"key":"3"}}]})"); // Object array where key is nested deeper json = R"({"a":[{"b":{"c":{"key":18446744073709551615}}},{"b":{"c":{"key":2}}}]})"; EXPECT_EQ( - std::string(json::convert_uint64_in_object_array_to_string("/a", "/b/c", - "key", json)), + json::convert_uint64_in_object_array_to_string("/a", "/b/c", "key", json), R"({"a":[{"b":{"c":{"key":"18446744073709551615"}}},{"b":{"c":{"key":"2"}}}]})"); } @@ -553,61 +428,62 @@ TEST(JsonParser, ConvertAllNumbersToString) { std::string json( R"({"a":[{"key":18446744073709551615},{"key":-2},{"key":3.14}]})"); EXPECT_EQ( - std::string(json::convert_all_numbers_to_string(json, "")), + json::convert_all_numbers_to_string(json, ""), R"({"a":[{"key":"18446744073709551615"},{"key":"-2"},{"key":"3.14"}]})"); // OK: convert deeply nested value to string json = R"({"some":[{"deeply":{"nested":[{"path":123}]}}]})"; - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "")), + EXPECT_EQ(json::convert_all_numbers_to_string(json, ""), R"({"some":[{"deeply":{"nested":[{"path":"123"}]}}]})"); // OK: values other than u64/f64/i64 are unchanged json = R"({"a":[{"key":18446744073709551615},{"key":null},{"key":true}]})"; EXPECT_EQ( - std::string(json::convert_all_numbers_to_string(json, "")), + json::convert_all_numbers_to_string(json, ""), R"({"a":[{"key":"18446744073709551615"},{"key":null},{"key":true}]})"); // OK: empty object array, nothing to convert json = R"({"a":[]})"; - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "")), json); + EXPECT_EQ(json::convert_all_numbers_to_string(json, ""), json); // OK: empty array json, nothing to convert json = R"([])"; - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "")), json); + EXPECT_EQ(json::convert_all_numbers_to_string(json, ""), json); // OK: floating point values in scientific notation are unchanged json = R"({"a": 1.196568750220778e-7})"; - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "")), + EXPECT_EQ(json::convert_all_numbers_to_string(json, ""), R"({"a":"1.196568750220778e-7"})"); // OK: convert under specified JSON path only json = R"({"a":1,"outer":{"inner": 2}})"; - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "/outer")), + EXPECT_EQ(json::convert_all_numbers_to_string(json, "/outer"), R"({"a":1,"outer":{"inner":"2"}})"); - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "/a")), + EXPECT_EQ(json::convert_all_numbers_to_string(json, "/a"), R"({"a":"1","outer":{"inner":2}})"); // KO: invalid path has no effect on the JSON json = R"({"a":1,"outer":{"inner":2}})"; - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "/invalid")), - json); - EXPECT_EQ(std::string(json::convert_all_numbers_to_string(json, "/")), json); + EXPECT_EQ(json::convert_all_numbers_to_string(json, "/invalid"), json); + EXPECT_EQ(json::convert_all_numbers_to_string(json, "/"), json); // KO: invalid cases std::vector invalid_cases = { // invalid json R"({"a": hello})", - // UINT64_MAX + 1 + // std::numeric_limits::max + 1 R"("{a":[{"key":18446744073709551616}]})", - // INT64_MIN - R"("{a":[{"key":)" + std::to_string(INT64_MIN) + "}]}", - // DBL_MIN - R"("{a":[{"key":)" + std::to_string(DBL_MIN) + "}]}", - // DBL_MAX - R"("{a":[{"key":)" + std::to_string(DBL_MAX + 1) + "}]}"}; + // std::numeric_limits::min() + R"("{a":[{"key":)" + + base::NumberToString(std::numeric_limits::min()) + "}]}", + // std::numeric_limits::min() + R"("{a":[{"key":)" + + base::NumberToString(std::numeric_limits::min()) + "}]}", + // std::numeric_limits::max() + R"("{a":[{"key":)" + + base::NumberToString(std::numeric_limits::max() + 1) + "}]}"}; for (const auto& invalid_case : invalid_cases) { - EXPECT_EQ( - "", std::string(json::convert_all_numbers_to_string(invalid_case, ""))) + EXPECT_EQ("", json::convert_all_numbers_to_string(invalid_case, "")) << invalid_case; } } @@ -618,43 +494,37 @@ TEST(JsonParser, ConvertAllNumbersToStringAndRemoveNullValues) { R"({"a":[{"key":18446744073709551615},{"key":-2},{"key":3.14}, {"key":null}]})"); EXPECT_EQ( - std::string( - json::convert_all_numbers_to_string_and_remove_null_values(json, "")), + json::convert_all_numbers_to_string_and_remove_null_values(json, ""), R"({"a":[{"key":"18446744073709551615"},{"key":"-2"},{"key":"3.14"}]})"); json = R"({"some":[{"deeply":{"nested":[{"path":123, "nullprop1": null}]}}], "nullprop2": null})"; EXPECT_EQ( - std::string( - json::convert_all_numbers_to_string_and_remove_null_values(json, "")), + json::convert_all_numbers_to_string_and_remove_null_values(json, ""), R"({"some":[{"deeply":{"nested":[{"path":"123"}]}}]})"); // OK: remove null values, empty strings, arrays, null values in array json = R"({"a":1,"outer":{"inner":2,"nullprop1":null,"arr_with_nulls":[null], "empty_string":"","empty_array":[]},"nulprop2":null})"; EXPECT_EQ( - std::string( - json::convert_all_numbers_to_string_and_remove_null_values(json, "")), + json::convert_all_numbers_to_string_and_remove_null_values(json, ""), R"({"a":"1","outer":{"inner":"2"}})"); // OK: convert under specified JSON path only json = R"({"a":1,"outer":{"inner": 2, "nullprop1": null}, "nulprop2": null})"; - EXPECT_EQ( - std::string(json::convert_all_numbers_to_string_and_remove_null_values( - json, "/outer")), - R"({"a":1,"nulprop2":null,"outer":{"inner":"2"}})"); + EXPECT_EQ(json::convert_all_numbers_to_string_and_remove_null_values( + json, "/outer"), + R"({"a":1,"nulprop2":null,"outer":{"inner":"2"}})"); // OK: invalid path has no effect on the JSON json = R"({"a":1,"outer":{"inner":2}})"; + EXPECT_EQ(json::convert_all_numbers_to_string_and_remove_null_values( + json, "/invalid"), + json); EXPECT_EQ( - std::string(json::convert_all_numbers_to_string_and_remove_null_values( - json, "/invalid")), - json); - EXPECT_EQ( - std::string(json::convert_all_numbers_to_string_and_remove_null_values( - json, "/")), + json::convert_all_numbers_to_string_and_remove_null_values(json, "/"), json); } diff --git a/components/json/rs/BUILD.gn b/components/json/rs/BUILD.gn index 6d0c31c74493..7bdc9476cd65 100644 --- a/components/json/rs/BUILD.gn +++ b/components/json/rs/BUILD.gn @@ -6,6 +6,8 @@ import("//build/rust/rust_static_library.gni") rust_static_library("rust_lib") { + visibility = [ "//brave/components/json" ] + crate_name = "json_cxx" crate_root = "src/lib.rs" allow_unsafe = true diff --git a/components/json/rs/src/lib.rs b/components/json/rs/src/lib.rs index cd36864e4bd3..4fed933e633d 100644 --- a/components/json/rs/src/lib.rs +++ b/components/json/rs/src/lib.rs @@ -4,7 +4,7 @@ // You can obtain one at https://mozilla.org/MPL/2.0/. #[allow(unsafe_op_in_unsafe_fn)] -#[cxx::bridge(namespace = json)] +#[cxx::bridge(namespace = rust_json)] mod ffi { extern "Rust" { fn convert_uint64_value_to_string(path: &str, json: &str, optional: bool) -> String;