Bitcoin Core 30.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 <sstream>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
15namespace
16{
17static bool ParsePrechecks(const std::string& str)
18{
19 if (str.empty()) // No empty string allowed
20 return false;
21 if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
22 return false;
23 if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
24 return false;
25 return true;
26}
27
28bool ParseDouble(const std::string& str, double *out)
29{
30 if (!ParsePrechecks(str))
31 return false;
32 if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
33 return false;
34 std::istringstream text(str);
35 text.imbue(std::locale::classic());
36 double result;
37 text >> result;
38 if(out) *out = result;
39 return text.eof() && !text.fail();
40}
41}
42
43const std::vector<std::string>& UniValue::getKeys() const
44{
46 return keys;
47}
48
49const std::vector<UniValue>& UniValue::getValues() const
50{
51 if (typ != VOBJ && typ != VARR)
52 throw std::runtime_error("JSON value is not an object or array as expected");
53 return values;
54}
55
57{
59 return isTrue();
60}
61
62const std::string& UniValue::get_str() const
63{
65 return getValStr();
66}
67
68double UniValue::get_real() const
69{
71 double retval;
72 if (!ParseDouble(getValStr(), &retval))
73 throw std::runtime_error("JSON double out of range");
74 return retval;
75}
76
78{
80 return *this;
81}
82
84{
86 return *this;
87}
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