|
| 1 | +// Copyright 2014 BitPay Inc. |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <stdint.h> |
| 6 | +#include <ctype.h> |
| 7 | +#include <iomanip> |
| 8 | +#include <sstream> |
| 9 | +#include <stdexcept> // std::runtime_error |
| 10 | + |
| 11 | +#include "univalue.h" |
| 12 | + |
| 13 | +#include "utilstrencodings.h" // ParseXX |
| 14 | + |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +const UniValue NullUniValue; |
| 18 | + |
| 19 | +void UniValue::clear() |
| 20 | +{ |
| 21 | + typ = VNULL; |
| 22 | + val.clear(); |
| 23 | + keys.clear(); |
| 24 | + values.clear(); |
| 25 | +} |
| 26 | + |
| 27 | +bool UniValue::setNull() |
| 28 | +{ |
| 29 | + clear(); |
| 30 | + return true; |
| 31 | +} |
| 32 | + |
| 33 | +bool UniValue::setBool(bool val_) |
| 34 | +{ |
| 35 | + clear(); |
| 36 | + typ = VBOOL; |
| 37 | + if (val_) |
| 38 | + val = "1"; |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +static bool validNumStr(const string& s) |
| 43 | +{ |
| 44 | + string tokenVal; |
| 45 | + unsigned int consumed; |
| 46 | + enum jtokentype tt = getJsonToken(tokenVal, consumed, s.c_str()); |
| 47 | + return (tt == JTOK_NUMBER); |
| 48 | +} |
| 49 | + |
| 50 | +bool UniValue::setNumStr(const string& val_) |
| 51 | +{ |
| 52 | + if (!validNumStr(val_)) |
| 53 | + return false; |
| 54 | + |
| 55 | + clear(); |
| 56 | + typ = VNUM; |
| 57 | + val = val_; |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +bool UniValue::setInt(uint64_t val) |
| 62 | +{ |
| 63 | + string s; |
| 64 | + ostringstream oss; |
| 65 | + |
| 66 | + oss << val; |
| 67 | + |
| 68 | + return setNumStr(oss.str()); |
| 69 | +} |
| 70 | + |
| 71 | +bool UniValue::setInt(int64_t val) |
| 72 | +{ |
| 73 | + string s; |
| 74 | + ostringstream oss; |
| 75 | + |
| 76 | + oss << val; |
| 77 | + |
| 78 | + return setNumStr(oss.str()); |
| 79 | +} |
| 80 | + |
| 81 | +bool UniValue::setFloat(double val) |
| 82 | +{ |
| 83 | + string s; |
| 84 | + ostringstream oss; |
| 85 | + |
| 86 | + oss << std::setprecision(16) << val; |
| 87 | + |
| 88 | + bool ret = setNumStr(oss.str()); |
| 89 | + typ = VNUM; |
| 90 | + return ret; |
| 91 | +} |
| 92 | + |
| 93 | +bool UniValue::setStr(const string& val_) |
| 94 | +{ |
| 95 | + clear(); |
| 96 | + typ = VSTR; |
| 97 | + val = val_; |
| 98 | + return true; |
| 99 | +} |
| 100 | + |
| 101 | +bool UniValue::setArray() |
| 102 | +{ |
| 103 | + clear(); |
| 104 | + typ = VARR; |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | +bool UniValue::setObject() |
| 109 | +{ |
| 110 | + clear(); |
| 111 | + typ = VOBJ; |
| 112 | + return true; |
| 113 | +} |
| 114 | + |
| 115 | +bool UniValue::push_back(const UniValue& val) |
| 116 | +{ |
| 117 | + if (typ != VARR) |
| 118 | + return false; |
| 119 | + |
| 120 | + values.push_back(val); |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +bool UniValue::push_backV(const std::vector<UniValue>& vec) |
| 125 | +{ |
| 126 | + if (typ != VARR) |
| 127 | + return false; |
| 128 | + |
| 129 | + values.insert(values.end(), vec.begin(), vec.end()); |
| 130 | + |
| 131 | + return true; |
| 132 | +} |
| 133 | + |
| 134 | +bool UniValue::pushKV(const std::string& key, const UniValue& val) |
| 135 | +{ |
| 136 | + if (typ != VOBJ) |
| 137 | + return false; |
| 138 | + |
| 139 | + keys.push_back(key); |
| 140 | + values.push_back(val); |
| 141 | + return true; |
| 142 | +} |
| 143 | + |
| 144 | +bool UniValue::pushKVs(const UniValue& obj) |
| 145 | +{ |
| 146 | + if (typ != VOBJ || obj.typ != VOBJ) |
| 147 | + return false; |
| 148 | + |
| 149 | + for (unsigned int i = 0; i < obj.keys.size(); i++) { |
| 150 | + keys.push_back(obj.keys[i]); |
| 151 | + values.push_back(obj.values[i]); |
| 152 | + } |
| 153 | + |
| 154 | + return true; |
| 155 | +} |
| 156 | + |
| 157 | +int UniValue::findKey(const std::string& key) const |
| 158 | +{ |
| 159 | + for (unsigned int i = 0; i < keys.size(); i++) { |
| 160 | + if (keys[i] == key) |
| 161 | + return (int) i; |
| 162 | + } |
| 163 | + |
| 164 | + return -1; |
| 165 | +} |
| 166 | + |
| 167 | +bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) |
| 168 | +{ |
| 169 | + for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin(); |
| 170 | + it != t.end(); it++) { |
| 171 | + int idx = findKey(it->first); |
| 172 | + if (idx < 0) |
| 173 | + return false; |
| 174 | + |
| 175 | + if (values[idx].getType() != it->second) |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + return true; |
| 180 | +} |
| 181 | + |
| 182 | +const UniValue& UniValue::operator[](const std::string& key) const |
| 183 | +{ |
| 184 | + if (typ != VOBJ) |
| 185 | + return NullUniValue; |
| 186 | + |
| 187 | + int index = findKey(key); |
| 188 | + if (index < 0) |
| 189 | + return NullUniValue; |
| 190 | + |
| 191 | + return values[index]; |
| 192 | +} |
| 193 | + |
| 194 | +const UniValue& UniValue::operator[](unsigned int index) const |
| 195 | +{ |
| 196 | + if (typ != VOBJ && typ != VARR) |
| 197 | + return NullUniValue; |
| 198 | + if (index >= values.size()) |
| 199 | + return NullUniValue; |
| 200 | + |
| 201 | + return values[index]; |
| 202 | +} |
| 203 | + |
| 204 | +const char *uvTypeName(UniValue::VType t) |
| 205 | +{ |
| 206 | + switch (t) { |
| 207 | + case UniValue::VNULL: return "null"; |
| 208 | + case UniValue::VBOOL: return "bool"; |
| 209 | + case UniValue::VOBJ: return "object"; |
| 210 | + case UniValue::VARR: return "array"; |
| 211 | + case UniValue::VSTR: return "string"; |
| 212 | + case UniValue::VNUM: return "number"; |
| 213 | + } |
| 214 | + |
| 215 | + // not reached |
| 216 | + return NULL; |
| 217 | +} |
| 218 | + |
| 219 | +const UniValue& find_value( const UniValue& obj, const std::string& name) |
| 220 | +{ |
| 221 | + for (unsigned int i = 0; i < obj.keys.size(); i++) |
| 222 | + { |
| 223 | + if( obj.keys[i] == name ) |
| 224 | + { |
| 225 | + return obj.values[i]; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + return NullUniValue; |
| 230 | +} |
| 231 | + |
| 232 | +std::vector<std::string> UniValue::getKeys() const |
| 233 | +{ |
| 234 | + if (typ != VOBJ) |
| 235 | + throw std::runtime_error("JSON value is not an object as expected"); |
| 236 | + return keys; |
| 237 | +} |
| 238 | + |
| 239 | +std::vector<UniValue> UniValue::getValues() const |
| 240 | +{ |
| 241 | + if (typ != VOBJ && typ != VARR) |
| 242 | + throw std::runtime_error("JSON value is not an object or array as expected"); |
| 243 | + return values; |
| 244 | +} |
| 245 | + |
| 246 | +bool UniValue::get_bool() const |
| 247 | +{ |
| 248 | + if (typ != VBOOL) |
| 249 | + throw std::runtime_error("JSON value is not a boolean as expected"); |
| 250 | + return getBool(); |
| 251 | +} |
| 252 | + |
| 253 | +std::string UniValue::get_str() const |
| 254 | +{ |
| 255 | + if (typ != VSTR) |
| 256 | + throw std::runtime_error("JSON value is not a string as expected"); |
| 257 | + return getValStr(); |
| 258 | +} |
| 259 | + |
| 260 | +int UniValue::get_int() const |
| 261 | +{ |
| 262 | + if (typ != VNUM) |
| 263 | + throw std::runtime_error("JSON value is not an integer as expected"); |
| 264 | + int32_t retval; |
| 265 | + if (!ParseInt32(getValStr(), &retval)) |
| 266 | + throw std::runtime_error("JSON integer out of range"); |
| 267 | + return retval; |
| 268 | +} |
| 269 | + |
| 270 | +int64_t UniValue::get_int64() const |
| 271 | +{ |
| 272 | + if (typ != VNUM) |
| 273 | + throw std::runtime_error("JSON value is not an integer as expected"); |
| 274 | + int64_t retval; |
| 275 | + if (!ParseInt64(getValStr(), &retval)) |
| 276 | + throw std::runtime_error("JSON integer out of range"); |
| 277 | + return retval; |
| 278 | +} |
| 279 | + |
| 280 | +double UniValue::get_real() const |
| 281 | +{ |
| 282 | + if (typ != VNUM) |
| 283 | + throw std::runtime_error("JSON value is not a number as expected"); |
| 284 | + double retval; |
| 285 | + if (!ParseDouble(getValStr(), &retval)) |
| 286 | + throw std::runtime_error("JSON double out of range"); |
| 287 | + return retval; |
| 288 | +} |
| 289 | + |
| 290 | +const UniValue& UniValue::get_obj() const |
| 291 | +{ |
| 292 | + if (typ != VOBJ) |
| 293 | + throw std::runtime_error("JSON value is not an object as expected"); |
| 294 | + return *this; |
| 295 | +} |
| 296 | + |
| 297 | +const UniValue& UniValue::get_array() const |
| 298 | +{ |
| 299 | + if (typ != VARR) |
| 300 | + throw std::runtime_error("JSON value is not an array as expected"); |
| 301 | + return *this; |
| 302 | +} |
0 commit comments