19 static bool ParsePrechecks(
const std::string& str)
23 if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1])))
25 if (str.size() != strlen(str.c_str()))
30 bool ParseInt32(
const std::string& str, int32_t *out)
32 if (!ParsePrechecks(str))
36 long int n = strtol(str.c_str(), &endp, 10);
37 if(out) *out = (int32_t)n;
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))
52 long long int n = strtoll(str.c_str(), &endp, 10);
53 if(out) *out = (int64_t)n;
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))
65 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
67 std::istringstream text(str);
68 text.imbue(std::locale::classic());
71 if(out) *out = result;
72 return text.eof() && !text.fail();
103 static bool validNumStr(
const string& s)
106 unsigned int consumed;
113 if (!validNumStr(val_))
128 return setNumStr(oss.str());
137 return setNumStr(oss.str());
144 oss << std::setprecision(16) << val_;
146 bool ret = setNumStr(oss.str());
178 values.push_back(val_);
187 values.insert(values.end(), vec.begin(), vec.end());
198 values.push_back(val_);
204 if (typ != VOBJ || obj.
typ != VOBJ)
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));
217 for (
size_t i = 0; i < keys.size(); i++) {
218 if (keys[i] ==
key) {
229 for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
230 it != t.end(); ++it) {
232 if (!findKey(it->first, idx))
235 if (values.at(idx).getType() != it->second)
248 if (!findKey(
key, index))
251 return values.at(index);
256 if (typ != VOBJ && typ != VARR)
258 if (index >= values.size())
261 return values.at(index);
281 for (
unsigned int i = 0; i < obj.
keys.size(); i++)
291 throw std::runtime_error(
"JSON value is not an object as expected");
297 if (typ != VOBJ && typ != VARR)
298 throw std::runtime_error(
"JSON value is not an object or array as expected");
305 throw std::runtime_error(
"JSON value is not a boolean as expected");
312 throw std::runtime_error(
"JSON value is not a string as expected");
319 throw std::runtime_error(
"JSON value is not an integer as expected");
322 throw std::runtime_error(
"JSON integer out of range");
329 throw std::runtime_error(
"JSON value is not an integer as expected");
332 throw std::runtime_error(
"JSON integer out of range");
339 throw std::runtime_error(
"JSON value is not a number as expected");
342 throw std::runtime_error(
"JSON double out of range");
349 throw std::runtime_error(
"JSON value is not an object as expected");
356 throw std::runtime_error(
"JSON value is not an array as expected");