Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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 {
20static bool ParsePrechecks(const std::string &str) {
21 if (str.empty()) {
22 // No empty string allowed
23 return false;
24 }
25 if (str.size() >= 1 &&
26 (json_isspace(str[0]) || json_isspace(str[str.size() - 1]))) {
27 // No padding allowed
28 return false;
29 }
30 if (str.size() != strlen(str.c_str())) {
31 // No embedded NUL characters allowed
32 return false;
33 }
34 return true;
35}
36
37bool ParseDouble(const std::string &str, double *out) {
38 if (!ParsePrechecks(str)) {
39 return false;
40 }
41 if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') {
42 // No hexadecimal floats allowed
43 return false;
44 }
45 std::istringstream text(str);
46 text.imbue(std::locale::classic());
47 double result;
48 text >> result;
49 if (out) {
50 *out = result;
51 }
52 return text.eof() && !text.fail();
53}
54} // namespace
55
56const std::vector<std::string> &UniValue::getKeys() const {
58 return keys;
59}
60
61const std::vector<UniValue> &UniValue::getValues() const {
62 if (typ != VOBJ && typ != VARR) {
63 throw std::runtime_error(
64 "JSON value is not an object or array as expected");
65 }
66 return values;
67}
68
69bool UniValue::get_bool() const {
71 return isTrue();
72}
73
74const std::string &UniValue::get_str() const {
76 return getValStr();
77}
78
79double UniValue::get_real() const {
81 double retval;
82 if (!ParseDouble(getValStr(), &retval)) {
83 throw std::runtime_error("JSON double out of range");
84 }
85 return retval;
86}
87
90 return *this;
91}
92
95 return *this;
96}
UniValue::VType typ
Definition univalue.h:127
const std::string & get_str() const
bool isTrue() const
Definition univalue.h:105
const std::string & getValStr() const
Definition univalue.h:89
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:130
std::vector< std::string > keys
Definition univalue.h:129
void checkType(const VType &expected) const
Definition univalue.cpp:201
const UniValue & get_array() const
double get_real() const
bool get_bool() const
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static bool ParsePrechecks(const std::string &str)
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
static bool json_isspace(int ch)
Definition univalue.h:207