Bitcoin ABC  0.26.3
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 
19 namespace {
20 static 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 
37 bool 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 
56 const std::vector<std::string> &UniValue::getKeys() const {
57  checkType(VOBJ);
58  return keys;
59 }
60 
61 const 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 
69 bool UniValue::get_bool() const {
71  return isTrue();
72 }
73 
74 const std::string &UniValue::get_str() const {
75  checkType(VSTR);
76  return getValStr();
77 }
78 
79 double UniValue::get_real() const {
80  checkType(VNUM);
81  double retval;
82  if (!ParseDouble(getValStr(), &retval)) {
83  throw std::runtime_error("JSON double out of range");
84  }
85  return retval;
86 }
87 
88 const UniValue &UniValue::get_obj() const {
89  checkType(VOBJ);
90  return *this;
91 }
92 
93 const UniValue &UniValue::get_array() const {
94  checkType(VARR);
95  return *this;
96 }
UniValue::VType typ
Definition: univalue.h:127
const std::string & get_str() const
bool isTrue() const
Definition: univalue.h:105
@ VOBJ
Definition: univalue.h:31
@ VSTR
Definition: univalue.h:33
@ VARR
Definition: univalue.h:32
@ VNUM
Definition: univalue.h:34
@ VBOOL
Definition: univalue.h:35
const UniValue & get_obj() const
const std::string & getValStr() const
Definition: univalue.h:89
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
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