Dash Core  0.12.2.1
P2P Digital Currency
univalue.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 http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <stdint.h>
7 #include <errno.h>
8 #include <iomanip>
9 #include <limits>
10 #include <sstream>
11 #include <stdexcept>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "univalue.h"
16 
17 namespace
18 {
19 static bool ParsePrechecks(const std::string& str)
20 {
21  if (str.empty()) // No empty string allowed
22  return false;
23  if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
24  return false;
25  if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
26  return false;
27  return true;
28 }
29 
30 bool ParseInt32(const std::string& str, int32_t *out)
31 {
32  if (!ParsePrechecks(str))
33  return false;
34  char *endp = NULL;
35  errno = 0; // strtol will not set errno if valid
36  long int n = strtol(str.c_str(), &endp, 10);
37  if(out) *out = (int32_t)n;
38  // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow
39  // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
40  // platforms the size of these types may be different.
41  return endp && *endp == 0 && !errno &&
42  n >= std::numeric_limits<int32_t>::min() &&
43  n <= std::numeric_limits<int32_t>::max();
44 }
45 
46 bool ParseInt64(const std::string& str, int64_t *out)
47 {
48  if (!ParsePrechecks(str))
49  return false;
50  char *endp = NULL;
51  errno = 0; // strtoll will not set errno if valid
52  long long int n = strtoll(str.c_str(), &endp, 10);
53  if(out) *out = (int64_t)n;
54  // Note that strtoll returns a *long long int*, so even if strtol doesn't report a over/underflow
55  // we still have to check that the returned value is within the range of an *int64_t*.
56  return endp && *endp == 0 && !errno &&
57  n >= std::numeric_limits<int64_t>::min() &&
58  n <= std::numeric_limits<int64_t>::max();
59 }
60 
61 bool ParseDouble(const std::string& str, double *out)
62 {
63  if (!ParsePrechecks(str))
64  return false;
65  if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
66  return false;
67  std::istringstream text(str);
68  text.imbue(std::locale::classic());
69  double result;
70  text >> result;
71  if(out) *out = result;
72  return text.eof() && !text.fail();
73 }
74 }
75 
76 using namespace std;
77 
79 
81 {
82  typ = VNULL;
83  val.clear();
84  keys.clear();
85  values.clear();
86 }
87 
89 {
90  clear();
91  return true;
92 }
93 
94 bool UniValue::setBool(bool val_)
95 {
96  clear();
97  typ = VBOOL;
98  if (val_)
99  val = "1";
100  return true;
101 }
102 
103 static bool validNumStr(const string& s)
104 {
105  string tokenVal;
106  unsigned int consumed;
107  enum jtokentype tt = getJsonToken(tokenVal, consumed, s.c_str());
108  return (tt == JTOK_NUMBER);
109 }
110 
111 bool UniValue::setNumStr(const string& val_)
112 {
113  if (!validNumStr(val_))
114  return false;
115 
116  clear();
117  typ = VNUM;
118  val = val_;
119  return true;
120 }
121 
122 bool UniValue::setInt(uint64_t val)
123 {
124  string s;
125  ostringstream oss;
126 
127  oss << val;
128 
129  return setNumStr(oss.str());
130 }
131 
132 bool UniValue::setInt(int64_t val)
133 {
134  string s;
135  ostringstream oss;
136 
137  oss << val;
138 
139  return setNumStr(oss.str());
140 }
141 
142 bool UniValue::setFloat(double val)
143 {
144  string s;
145  ostringstream oss;
146 
147  oss << std::setprecision(16) << val;
148 
149  bool ret = setNumStr(oss.str());
150  typ = VNUM;
151  return ret;
152 }
153 
154 bool UniValue::setStr(const string& val_)
155 {
156  clear();
157  typ = VSTR;
158  val = val_;
159  return true;
160 }
161 
163 {
164  clear();
165  typ = VARR;
166  return true;
167 }
168 
170 {
171  clear();
172  typ = VOBJ;
173  return true;
174 }
175 
177 {
178  if (typ != VARR)
179  return false;
180 
181  values.push_back(val);
182  return true;
183 }
184 
185 bool UniValue::push_backV(const std::vector<UniValue>& vec)
186 {
187  if (typ != VARR)
188  return false;
189 
190  values.insert(values.end(), vec.begin(), vec.end());
191 
192  return true;
193 }
194 
195 bool UniValue::pushKV(const std::string& key, const UniValue& val)
196 {
197  if (typ != VOBJ)
198  return false;
199 
200  keys.push_back(key);
201  values.push_back(val);
202  return true;
203 }
204 
205 bool UniValue::pushKVs(const UniValue& obj)
206 {
207  if (typ != VOBJ || obj.typ != VOBJ)
208  return false;
209 
210  for (unsigned int i = 0; i < obj.keys.size(); i++) {
211  keys.push_back(obj.keys[i]);
212  values.push_back(obj.values.at(i));
213  }
214 
215  return true;
216 }
217 
218 int UniValue::findKey(const std::string& key) const
219 {
220  for (unsigned int i = 0; i < keys.size(); i++) {
221  if (keys[i] == key)
222  return (int) i;
223  }
224 
225  return -1;
226 }
227 
228 bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
229 {
230  for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
231  it != t.end(); it++) {
232  int idx = findKey(it->first);
233  if (idx < 0)
234  return false;
235 
236  if (values.at(idx).getType() != it->second)
237  return false;
238  }
239 
240  return true;
241 }
242 
243 const UniValue& UniValue::operator[](const std::string& key) const
244 {
245  if (typ != VOBJ)
246  return NullUniValue;
247 
248  int index = findKey(key);
249  if (index < 0)
250  return NullUniValue;
251 
252  return values.at(index);
253 }
254 
255 const UniValue& UniValue::operator[](unsigned int index) const
256 {
257  if (typ != VOBJ && typ != VARR)
258  return NullUniValue;
259  if (index >= values.size())
260  return NullUniValue;
261 
262  return values.at(index);
263 }
264 
266 {
267  switch (t) {
268  case UniValue::VNULL: return "null";
269  case UniValue::VBOOL: return "bool";
270  case UniValue::VOBJ: return "object";
271  case UniValue::VARR: return "array";
272  case UniValue::VSTR: return "string";
273  case UniValue::VNUM: return "number";
274  }
275 
276  // not reached
277  return NULL;
278 }
279 
280 const UniValue& find_value(const UniValue& obj, const std::string& name)
281 {
282  for (unsigned int i = 0; i < obj.keys.size(); i++)
283  if (obj.keys[i] == name)
284  return obj.values.at(i);
285 
286  return NullUniValue;
287 }
288 
289 std::vector<std::string> UniValue::getKeys() const
290 {
291  if (typ != VOBJ)
292  throw std::runtime_error("JSON value is not an object as expected");
293  return keys;
294 }
295 
296 std::vector<UniValue> UniValue::getValues() const
297 {
298  if (typ != VOBJ && typ != VARR)
299  throw std::runtime_error("JSON value is not an object or array as expected");
300  return values;
301 }
302 
303 bool UniValue::get_bool() const
304 {
305  if (typ != VBOOL)
306  throw std::runtime_error("JSON value is not a boolean as expected");
307  return getBool();
308 }
309 
310 std::string UniValue::get_str() const
311 {
312  if (typ != VSTR)
313  throw std::runtime_error("JSON value is not a string as expected");
314  return getValStr();
315 }
316 
317 int UniValue::get_int() const
318 {
319  if (typ != VNUM)
320  throw std::runtime_error("JSON value is not an integer as expected");
321  int32_t retval;
322  if (!ParseInt32(getValStr(), &retval))
323  throw std::runtime_error("JSON integer out of range");
324  return retval;
325 }
326 
327 int64_t UniValue::get_int64() const
328 {
329  if (typ != VNUM)
330  throw std::runtime_error("JSON value is not an integer as expected");
331  int64_t retval;
332  if (!ParseInt64(getValStr(), &retval))
333  throw std::runtime_error("JSON integer out of range");
334  return retval;
335 }
336 
337 double UniValue::get_real() const
338 {
339  if (typ != VNUM)
340  throw std::runtime_error("JSON value is not a number as expected");
341  double retval;
342  if (!ParseDouble(getValStr(), &retval))
343  throw std::runtime_error("JSON double out of range");
344  return retval;
345 }
346 
348 {
349  if (typ != VOBJ)
350  throw std::runtime_error("JSON value is not an object as expected");
351  return *this;
352 }
353 
355 {
356  if (typ != VARR)
357  throw std::runtime_error("JSON value is not an array as expected");
358  return *this;
359 }
360 
bool setObject()
Definition: univalue.cpp:169
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:185
bool setFloat(double val)
Definition: univalue.cpp:142
double get_real() const
Definition: univalue.cpp:337
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:265
bool ParseDouble(const std::string &str, double *out)
bool push_back(const UniValue &val)
Definition: univalue.cpp:176
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:195
static bool json_isspace(int ch)
Definition: univalue.h:263
int64_t get_int64() const
Definition: univalue.cpp:327
bool setInt(uint64_t val)
Definition: univalue.cpp:122
bool ParseInt64(const std::string &str, int64_t *out)
UniValue::VType typ
Definition: univalue.h:133
const UniValue & get_obj() const
Definition: univalue.cpp:347
std::vector< std::string > getKeys() const
Definition: univalue.cpp:289
bool ParseInt32(const std::string &str, int32_t *out)
enum jtokentype getJsonToken(std::string &tokenVal, unsigned int &consumed, const char *raw)
const char * name
Definition: rest.cpp:37
bool setBool(bool val)
Definition: univalue.cpp:94
int findKey(const std::string &key) const
Definition: univalue.cpp:218
void clear()
Definition: univalue.cpp:80
std::vector< std::string > keys
Definition: univalue.h:135
bool setStr(const std::string &val)
Definition: univalue.cpp:154
jtokentype
Definition: univalue.h:226
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:243
static bool ParsePrechecks(const std::string &str)
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:205
static bool validNumStr(const string &s)
Definition: univalue.cpp:103
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:280
bool setNull()
Definition: univalue.cpp:88
bool setArray()
Definition: univalue.cpp:162
bool get_bool() const
Definition: univalue.cpp:303
int get_int() const
Definition: univalue.cpp:317
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes)
Definition: univalue.cpp:228
bool setNumStr(const std::string &val)
Definition: univalue.cpp:111
std::vector< UniValue > values
Definition: univalue.h:136
const UniValue & get_array() const
Definition: univalue.cpp:354
const UniValue NullUniValue
Definition: univalue.cpp:78
std::string get_str() const
Definition: univalue.cpp:310
result
Definition: rpcuser.py:37
std::vector< UniValue > getValues() const
Definition: univalue.cpp:296