diff options
author | stickies-v <stickies-v@protonmail.com> | 2023-03-03 15:07:31 +0000 |
---|---|---|
committer | stickies-v <stickies-v@protonmail.com> | 2023-03-23 18:18:46 +0000 |
commit | cfbc8a623b5133f1d0b0c0c9be73b2b107e0d687 (patch) | |
tree | 3926d900923a28d8e8f8bcef609ccc26bd8e9338 | |
parent | 6c8bde6d54d03224709dce54b8ba32b8c3e37ac7 (diff) | |
download | bitcoin-cfbc8a623b5133f1d0b0c0c9be73b2b107e0d687.tar.xz |
refactor: rpc: hide and rename ParseNonRFCJSONValue()
As per https://github.com/bitcoin/bitcoin/pull/26506#pullrequestreview-1211984059,
this function is no longer necessary and we can use UniValue::read() directly.
To avoid code duplication, we keep the function to throw on invalid input data
but rename it to Parse() and remove it from the header.
-rw-r--r-- | src/rpc/client.cpp | 22 | ||||
-rw-r--r-- | src/rpc/client.h | 5 |
2 files changed, 10 insertions, 17 deletions
diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 9449b9d197..2b517e77c8 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -222,6 +222,14 @@ static const CRPCConvertParam vRPCConvertParams[] = }; // clang-format on +/** Parse string to UniValue or throw runtime_error if string contains invalid JSON */ +static UniValue Parse(std::string_view raw) +{ + UniValue parsed; + if (!parsed.read(raw)) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw)); + return parsed; +} + class CRPCConvertTable { private: @@ -234,13 +242,13 @@ public: /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */ UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, int param_idx) { - return members.count({method, param_idx}) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value; + return members.count({method, param_idx}) > 0 ? Parse(arg_value) : arg_value; } /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */ UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, const std::string& param_name) { - return membersByName.count({method, param_name}) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value; + return membersByName.count({method, param_name}) > 0 ? Parse(arg_value) : arg_value; } }; @@ -254,16 +262,6 @@ CRPCConvertTable::CRPCConvertTable() static CRPCConvertTable rpcCvtTable; -/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) - * as well as objects and arrays. - */ -UniValue ParseNonRFCJSONValue(std::string_view raw) -{ - UniValue parsed; - if (!parsed.read(raw)) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw)); - return parsed; -} - UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams) { UniValue params(UniValue::VARR); diff --git a/src/rpc/client.h b/src/rpc/client.h index 3c5c4fc4d6..b67cd27fdf 100644 --- a/src/rpc/client.h +++ b/src/rpc/client.h @@ -17,9 +17,4 @@ UniValue RPCConvertValues(const std::string& strMethod, const std::vector<std::s /** Convert named arguments to command-specific RPC representation */ UniValue RPCConvertNamedValues(const std::string& strMethod, const std::vector<std::string>& strParams); -/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) - * as well as objects and arrays. - */ -UniValue ParseNonRFCJSONValue(std::string_view raw); - #endif // BITCOIN_RPC_CLIENT_H |