Bitcoin Core 28.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 <cerrno>
9#include <cstdint>
10#include <cstdlib>
11#include <cstring>
12#include <limits>
13#include <locale>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
19namespace
20{
21static bool ParsePrechecks(const std::string& str)
22{
23 if (str.empty()) // No empty string allowed
24 return false;
25 if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
26 return false;
27 if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
28 return false;
29 return true;
30}
31
32bool ParseDouble(const std::string& str, double *out)
33{
34 if (!ParsePrechecks(str))
35 return false;
36 if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
37 return false;
38 std::istringstream text(str);
39 text.imbue(std::locale::classic());
40 double result;
41 text >> result;
42 if(out) *out = result;
43 return text.eof() && !text.fail();
44}
45}
46
47const std::vector<std::string>& UniValue::getKeys() const
48{
50 return keys;
51}
52
53const std::vector<UniValue>& UniValue::getValues() const
54{
55 if (typ != VOBJ && typ != VARR)
56 throw std::runtime_error("JSON value is not an object or array as expected");
57 return values;
58}
59
61{
63 return isTrue();
64}
65
66const std::string& UniValue::get_str() const
67{
69 return getValStr();
70}
71
72double UniValue::get_real() const
73{
75 double retval;
76 if (!ParseDouble(getValStr(), &retval))
77 throw std::runtime_error("JSON double out of range");
78 return retval;
79}
80
82{
84 return *this;
85}
86
88{
90 return *this;
91}
UniValue::VType typ
Definition: univalue.h:103
const std::string & get_str() const
bool isTrue() const
Definition: univalue.h:80
@ 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:106
std::vector< std::string > keys
Definition: univalue.h:105
void checkType(const VType &expected) const
Definition: univalue.cpp:210
const UniValue & get_array() const
double get_real() const
bool get_bool() const
static bool json_isspace(int ch)
Definition: univalue.h:187