Squashed 'src/univalue/' changes from daf1285..16a1f7f
[bitcoinplatinum.git] / lib / univalue.cpp
blob5a2860c13f8d6c69e91c203dc26df80444e709f7
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.
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>
15 #include "univalue.h"
17 namespace
19 static bool ParsePrechecks(const std::string& str)
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;
30 bool ParseInt32(const std::string& str, int32_t *out)
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();
46 bool ParseInt64(const std::string& str, int64_t *out)
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();
61 bool ParseDouble(const std::string& str, double *out)
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();
76 using namespace std;
78 const UniValue NullUniValue;
80 void UniValue::clear()
82 typ = VNULL;
83 val.clear();
84 keys.clear();
85 values.clear();
88 bool UniValue::setNull()
90 clear();
91 return true;
94 bool UniValue::setBool(bool val_)
96 clear();
97 typ = VBOOL;
98 if (val_)
99 val = "1";
100 return true;
103 static bool validNumStr(const string& s)
105 string tokenVal;
106 unsigned int consumed;
107 enum jtokentype tt = getJsonToken(tokenVal, consumed, s.c_str());
108 return (tt == JTOK_NUMBER);
111 bool UniValue::setNumStr(const string& val_)
113 if (!validNumStr(val_))
114 return false;
116 clear();
117 typ = VNUM;
118 val = val_;
119 return true;
122 bool UniValue::setInt(uint64_t val_)
124 ostringstream oss;
126 oss << val_;
128 return setNumStr(oss.str());
131 bool UniValue::setInt(int64_t val_)
133 ostringstream oss;
135 oss << val_;
137 return setNumStr(oss.str());
140 bool UniValue::setFloat(double val_)
142 ostringstream oss;
144 oss << std::setprecision(16) << val_;
146 bool ret = setNumStr(oss.str());
147 typ = VNUM;
148 return ret;
151 bool UniValue::setStr(const string& val_)
153 clear();
154 typ = VSTR;
155 val = val_;
156 return true;
159 bool UniValue::setArray()
161 clear();
162 typ = VARR;
163 return true;
166 bool UniValue::setObject()
168 clear();
169 typ = VOBJ;
170 return true;
173 bool UniValue::push_back(const UniValue& val_)
175 if (typ != VARR)
176 return false;
178 values.push_back(val_);
179 return true;
182 bool UniValue::push_backV(const std::vector<UniValue>& vec)
184 if (typ != VARR)
185 return false;
187 values.insert(values.end(), vec.begin(), vec.end());
189 return true;
192 bool UniValue::pushKV(const std::string& key, const UniValue& val_)
194 if (typ != VOBJ)
195 return false;
197 keys.push_back(key);
198 values.push_back(val_);
199 return true;
202 bool UniValue::pushKVs(const UniValue& obj)
204 if (typ != VOBJ || obj.typ != VOBJ)
205 return false;
207 for (unsigned int i = 0; i < obj.keys.size(); i++) {
208 keys.push_back(obj.keys[i]);
209 values.push_back(obj.values.at(i));
212 return true;
215 int UniValue::findKey(const std::string& key) const
217 for (unsigned int i = 0; i < keys.size(); i++) {
218 if (keys[i] == key)
219 return (int) i;
222 return -1;
225 bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
227 for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
228 it != t.end(); ++it) {
229 int idx = findKey(it->first);
230 if (idx < 0)
231 return false;
233 if (values.at(idx).getType() != it->second)
234 return false;
237 return true;
240 const UniValue& UniValue::operator[](const std::string& key) const
242 if (typ != VOBJ)
243 return NullUniValue;
245 int index = findKey(key);
246 if (index < 0)
247 return NullUniValue;
249 return values.at(index);
252 const UniValue& UniValue::operator[](unsigned int index) const
254 if (typ != VOBJ && typ != VARR)
255 return NullUniValue;
256 if (index >= values.size())
257 return NullUniValue;
259 return values.at(index);
262 const char *uvTypeName(UniValue::VType t)
264 switch (t) {
265 case UniValue::VNULL: return "null";
266 case UniValue::VBOOL: return "bool";
267 case UniValue::VOBJ: return "object";
268 case UniValue::VARR: return "array";
269 case UniValue::VSTR: return "string";
270 case UniValue::VNUM: return "number";
273 // not reached
274 return NULL;
277 const UniValue& find_value(const UniValue& obj, const std::string& name)
279 for (unsigned int i = 0; i < obj.keys.size(); i++)
280 if (obj.keys[i] == name)
281 return obj.values.at(i);
283 return NullUniValue;
286 const std::vector<std::string>& UniValue::getKeys() const
288 if (typ != VOBJ)
289 throw std::runtime_error("JSON value is not an object as expected");
290 return keys;
293 const std::vector<UniValue>& UniValue::getValues() const
295 if (typ != VOBJ && typ != VARR)
296 throw std::runtime_error("JSON value is not an object or array as expected");
297 return values;
300 bool UniValue::get_bool() const
302 if (typ != VBOOL)
303 throw std::runtime_error("JSON value is not a boolean as expected");
304 return getBool();
307 const std::string& UniValue::get_str() const
309 if (typ != VSTR)
310 throw std::runtime_error("JSON value is not a string as expected");
311 return getValStr();
314 int UniValue::get_int() const
316 if (typ != VNUM)
317 throw std::runtime_error("JSON value is not an integer as expected");
318 int32_t retval;
319 if (!ParseInt32(getValStr(), &retval))
320 throw std::runtime_error("JSON integer out of range");
321 return retval;
324 int64_t UniValue::get_int64() const
326 if (typ != VNUM)
327 throw std::runtime_error("JSON value is not an integer as expected");
328 int64_t retval;
329 if (!ParseInt64(getValStr(), &retval))
330 throw std::runtime_error("JSON integer out of range");
331 return retval;
334 double UniValue::get_real() const
336 if (typ != VNUM)
337 throw std::runtime_error("JSON value is not a number as expected");
338 double retval;
339 if (!ParseDouble(getValStr(), &retval))
340 throw std::runtime_error("JSON double out of range");
341 return retval;
344 const UniValue& UniValue::get_obj() const
346 if (typ != VOBJ)
347 throw std::runtime_error("JSON value is not an object as expected");
348 return *this;
351 const UniValue& UniValue::get_array() const
353 if (typ != VARR)
354 throw std::runtime_error("JSON value is not an array as expected");
355 return *this;