Skip to content

Commit 106bab1

Browse files
codablockUdjinM6
authored andcommitted
Add new ParseXXX methods to easily parse UniValue values (#2211)
1 parent 566fa5e commit 106bab1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/rpc/server.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <boost/thread.hpp>
2525
#include <boost/algorithm/string/case_conv.hpp> // for to_upper()
2626

27+
#include <algorithm>
2728
#include <memory> // for unique_ptr
2829
#include <unordered_map>
2930

@@ -172,6 +173,53 @@ std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
172173
return ParseHexV(find_value(o, strKey), strKey);
173174
}
174175

176+
int32_t ParseInt32V(const UniValue& v, const std::string &strName)
177+
{
178+
std::string strNum = v.getValStr();
179+
int32_t num;
180+
if (!ParseInt32(strNum, &num))
181+
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be a 32bit integer (not '"+strNum+"')");
182+
return num;
183+
}
184+
185+
int64_t ParseInt64V(const UniValue& v, const std::string &strName)
186+
{
187+
std::string strNum = v.getValStr();
188+
int64_t num;
189+
if (!ParseInt64(strNum, &num))
190+
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be a 64bit integer (not '"+strNum+"')");
191+
return num;
192+
}
193+
194+
double ParseDoubleV(const UniValue& v, const std::string &strName)
195+
{
196+
std::string strNum = v.getValStr();
197+
double num;
198+
if (!ParseDouble(strNum, &num))
199+
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be a be number (not '"+strNum+"')");
200+
return num;
201+
}
202+
203+
bool ParseBoolV(const UniValue& v, const std::string &strName)
204+
{
205+
std::string strBool;
206+
if (v.isBool())
207+
return v.get_bool();
208+
else if (v.isNum())
209+
strBool = itostr(v.get_int());
210+
else if (v.isStr())
211+
strBool = v.get_str();
212+
213+
std::transform(strBool.begin(), strBool.end(), strBool.begin(), ::tolower);
214+
215+
if (strBool == "true" || strBool == "yes" || strBool == "1") {
216+
return true;
217+
} else if (strBool == "false" || strBool == "no" || strBool == "0") {
218+
return false;
219+
}
220+
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be true, false, yes, no, 1 or 0 (not '"+strBool+"')");
221+
}
222+
175223
/**
176224
* Note: This interface may still be subject to change.
177225
*/

src/rpc/server.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ extern uint256 ParseHashO(const UniValue& o, std::string strKey);
187187
extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
188188
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
189189

190+
extern int32_t ParseInt32V(const UniValue& v, const std::string &strName);
191+
extern int64_t ParseInt64V(const UniValue& v, const std::string &strName);
192+
extern double ParseDoubleV(const UniValue& v, const std::string &strName);
193+
extern bool ParseBoolV(const UniValue& v, const std::string &strName);
194+
190195
extern int64_t nWalletUnlockTime;
191196
extern CAmount AmountFromValue(const UniValue& value);
192197
extern UniValue ValueFromAmount(const CAmount& amount);

0 commit comments

Comments
 (0)