Bitcoin Core 31.99.0
P2P Digital Currency
univalue_get.cpp
Go to the documentation of this file.
1// Copyright 2014 BitPay Inc.
2// Copyright 2015 Bitcoin Core Developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or https://opensource.org/licenses/mit-license.php.
5
6#include <univalue.h>
7
8#include <cstring>
9#include <locale>
10#include <optional>
11#include <sstream>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
16namespace
17{
18static bool ParsePrechecks(const std::string& str)
19{
20 if (str.empty()) // No empty string allowed
21 return false;
22 if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
23 return false;
24 if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
25 return false;
26 return true;
27}
28
29std::optional<double> ParseDouble(const std::string& str)
30{
31 if (!ParsePrechecks(str))
32 return std::nullopt;
33 if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
34 return std::nullopt;
35 std::istringstream text(str);
36 text.imbue(std::locale::classic());
37 double result;
38 text >> result;
39 if (!text.eof() || text.fail()) {
40 return std::nullopt;
41 }
42 return result;
43}
44}
45
46const std::vector<std::string>& UniValue::getKeys() const
47{
49 return keys;
50}
51
52const std::vector<UniValue>& UniValue::getValues() const
53{
54 if (typ != VOBJ && typ != VARR)
55 throw std::runtime_error("JSON value is not an object or array as expected");
56 return values;
57}
58
60{
62 return isTrue();
63}
64
65const std::string& UniValue::get_str() const
66{
68 return getValStr();
69}
70
71double UniValue::get_real() const
72{
74 if (const auto retval{ParseDouble(getValStr())}) {
75 return *retval;
76 }
77 throw std::runtime_error("JSON double out of range");
78}
79
81{
83 return *this;
84}
85
87{
89 return *this;
90}
UniValue::VType typ
Definition: univalue.h:105
const std::string & get_str() const
bool isTrue() const
Definition: univalue.h:82
@ VOBJ
Definition: univalue.h:24
@ VSTR
Definition: univalue.h:24
@ VARR
Definition: univalue.h:24
@ VNUM
Definition: univalue.h:24
@ VBOOL
Definition: univalue.h:24
const std::string & getValStr() const
Definition: univalue.h:68
const UniValue & get_obj() const
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
std::vector< UniValue > values
Definition: univalue.h:108
std::vector< std::string > keys
Definition: univalue.h:107
void checkType(const VType &expected) const
Definition: univalue.cpp:209
const UniValue & get_array() const
double get_real() const
bool get_bool() const
static bool json_isspace(int ch)
Definition: univalue.h:189