Branch data Line data Source code
# 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 : : #ifndef __UNIVALUE_H__
# 7 : : #define __UNIVALUE_H__
# 8 : :
# 9 : : #include <stdint.h>
# 10 : : #include <string.h>
# 11 : :
# 12 : : #include <string>
# 13 : : #include <vector>
# 14 : : #include <map>
# 15 : : #include <cassert>
# 16 : :
# 17 : : #include <sstream> // .get_int64()
# 18 : :
# 19 : : class UniValue {
# 20 : : public:
# 21 : : enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
# 22 : :
# 23 : : UniValue() { typ = VNULL; }
# 24 : : UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
# 25 : : typ = initialType;
# 26 : : val = initialStr;
# 27 : : }
# 28 : : UniValue(uint64_t val_) {
# 29 : : setInt(val_);
# 30 : : }
# 31 : : UniValue(int64_t val_) {
# 32 : : setInt(val_);
# 33 : : }
# 34 : : UniValue(bool val_) {
# 35 : : setBool(val_);
# 36 : : }
# 37 : : UniValue(int val_) {
# 38 : : setInt(val_);
# 39 : : }
# 40 : : UniValue(double val_) {
# 41 : : setFloat(val_);
# 42 : : }
# 43 : : UniValue(const std::string& val_) {
# 44 : : setStr(val_);
# 45 : : }
# 46 : : UniValue(const char *val_) {
# 47 : : std::string s(val_);
# 48 : : setStr(s);
# 49 : : }
# 50 : :
# 51 : : void clear();
# 52 : :
# 53 : : bool setNull();
# 54 : : bool setBool(bool val);
# 55 : : bool setNumStr(const std::string& val);
# 56 : : bool setInt(uint64_t val);
# 57 : : bool setInt(int64_t val);
# 58 : : bool setInt(int val_) { return setInt((int64_t)val_); }
# 59 : : bool setFloat(double val);
# 60 : : bool setStr(const std::string& val);
# 61 : : bool setArray();
# 62 : : bool setObject();
# 63 : :
# 64 : : enum VType getType() const { return typ; }
# 65 : : const std::string& getValStr() const { return val; }
# 66 : : bool empty() const { return (values.size() == 0); }
# 67 : :
# 68 : : size_t size() const { return values.size(); }
# 69 : :
# 70 : 4668 : bool getBool() const { return isTrue(); }
# 71 : : void getObjMap(std::map<std::string,UniValue>& kv) const;
# 72 : : bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
# 73 : : const UniValue& operator[](const std::string& key) const;
# 74 : : const UniValue& operator[](size_t index) const;
# 75 : : bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
# 76 : :
# 77 : : bool isNull() const { return (typ == VNULL); }
# 78 : : bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
# 79 : : bool isFalse() const { return (typ == VBOOL) && (val != "1"); }
# 80 : : bool isBool() const { return (typ == VBOOL); }
# 81 : : bool isStr() const { return (typ == VSTR); }
# 82 : : bool isNum() const { return (typ == VNUM); }
# 83 : : bool isArray() const { return (typ == VARR); }
# 84 : : bool isObject() const { return (typ == VOBJ); }
# 85 : :
# 86 : : bool push_back(const UniValue& val);
# 87 : : bool push_back(const std::string& val_) {
# 88 : : UniValue tmpVal(VSTR, val_);
# 89 : : return push_back(tmpVal);
# 90 : : }
# 91 : : bool push_back(const char *val_) {
# 92 : : std::string s(val_);
# 93 : : return push_back(s);
# 94 : : }
# 95 : : bool push_back(uint64_t val_) {
# 96 : : UniValue tmpVal(val_);
# 97 : : return push_back(tmpVal);
# 98 : : }
# 99 : : bool push_back(int64_t val_) {
# 100 : : UniValue tmpVal(val_);
# 101 : : return push_back(tmpVal);
# 102 : : }
# 103 : : bool push_back(bool val_) {
# 104 : : UniValue tmpVal(val_);
# 105 : : return push_back(tmpVal);
# 106 : : }
# 107 : : bool push_back(int val_) {
# 108 : : UniValue tmpVal(val_);
# 109 : : return push_back(tmpVal);
# 110 : : }
# 111 : : bool push_back(double val_) {
# 112 : : UniValue tmpVal(val_);
# 113 : : return push_back(tmpVal);
# 114 : : }
# 115 : : bool push_backV(const std::vector<UniValue>& vec);
# 116 : :
# 117 : : void __pushKV(const std::string& key, const UniValue& val);
# 118 : : bool pushKV(const std::string& key, const UniValue& val);
# 119 : : bool pushKV(const std::string& key, const std::string& val_) {
# 120 : : UniValue tmpVal(VSTR, val_);
# 121 : : return pushKV(key, tmpVal);
# 122 : : }
# 123 : : bool pushKV(const std::string& key, const char *val_) {
# 124 : : std::string _val(val_);
# 125 : : return pushKV(key, _val);
# 126 : : }
# 127 : : bool pushKV(const std::string& key, int64_t val_) {
# 128 : : UniValue tmpVal(val_);
# 129 : : return pushKV(key, tmpVal);
# 130 : : }
# 131 : : bool pushKV(const std::string& key, uint64_t val_) {
# 132 : : UniValue tmpVal(val_);
# 133 : : return pushKV(key, tmpVal);
# 134 : : }
# 135 : : bool pushKV(const std::string& key, bool val_) {
# 136 : : UniValue tmpVal(val_);
# 137 : : return pushKV(key, tmpVal);
# 138 : : }
# 139 : : bool pushKV(const std::string& key, int val_) {
# 140 : : UniValue tmpVal((int64_t)val_);
# 141 : : return pushKV(key, tmpVal);
# 142 : : }
# 143 : : bool pushKV(const std::string& key, double val_) {
# 144 : : UniValue tmpVal(val_);
# 145 : : return pushKV(key, tmpVal);
# 146 : : }
# 147 : : bool pushKVs(const UniValue& obj);
# 148 : :
# 149 : : std::string write(unsigned int prettyIndent = 0,
# 150 : : unsigned int indentLevel = 0) const;
# 151 : :
# 152 : : bool read(const char *raw, size_t len);
# 153 : : bool read(const char *raw) { return read(raw, strlen(raw)); }
# 154 : : bool read(const std::string& rawStr) {
# 155 : : return read(rawStr.data(), rawStr.size());
# 156 : : }
# 157 : :
# 158 : : private:
# 159 : : UniValue::VType typ;
# 160 : : std::string val; // numbers are stored as C++ strings
# 161 : : std::vector<std::string> keys;
# 162 : : std::vector<UniValue> values;
# 163 : :
# 164 : : bool findKey(const std::string& key, size_t& retIdx) const;
# 165 : : void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
# 166 : : void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
# 167 : :
# 168 : : public:
# 169 : : // Strict type-specific getters, these throw std::runtime_error if the
# 170 : : // value is of unexpected type
# 171 : : const std::vector<std::string>& getKeys() const;
# 172 : : const std::vector<UniValue>& getValues() const;
# 173 : : bool get_bool() const;
# 174 : : const std::string& get_str() const;
# 175 : : int get_int() const;
# 176 : : int64_t get_int64() const;
# 177 : : double get_real() const;
# 178 : : const UniValue& get_obj() const;
# 179 : : const UniValue& get_array() const;
# 180 : :
# 181 : : enum VType type() const { return getType(); }
# 182 : : friend const UniValue& find_value( const UniValue& obj, const std::string& name);
# 183 : : };
# 184 : :
# 185 : : enum jtokentype {
# 186 : : JTOK_ERR = -1,
# 187 : : JTOK_NONE = 0, // eof
# 188 : : JTOK_OBJ_OPEN,
# 189 : : JTOK_OBJ_CLOSE,
# 190 : : JTOK_ARR_OPEN,
# 191 : : JTOK_ARR_CLOSE,
# 192 : : JTOK_COLON,
# 193 : : JTOK_COMMA,
# 194 : : JTOK_KW_NULL,
# 195 : : JTOK_KW_TRUE,
# 196 : : JTOK_KW_FALSE,
# 197 : : JTOK_NUMBER,
# 198 : : JTOK_STRING,
# 199 : : };
# 200 : :
# 201 : : extern enum jtokentype getJsonToken(std::string& tokenVal,
# 202 : : unsigned int& consumed, const char *raw, const char *end);
# 203 : : extern const char *uvTypeName(UniValue::VType t);
# 204 : :
# 205 : : static inline bool jsonTokenIsValue(enum jtokentype jtt)
# 206 : 2518440 : {
# 207 : 2518440 : switch (jtt) {
# 208 [ + + ]: 4129 : case JTOK_KW_NULL:
# 209 [ + + ]: 7773 : case JTOK_KW_TRUE:
# 210 [ + + ]: 9549 : case JTOK_KW_FALSE:
# 211 [ + + ]: 193387 : case JTOK_NUMBER:
# 212 [ + + ]: 1046211 : case JTOK_STRING:
# 213 : 1046211 : return true;
# 214 : :
# 215 [ + + ]: 1472229 : default:
# 216 : 1472229 : return false;
# 217 : 2518440 : }
# 218 : :
# 219 : : // not reached
# 220 : 2518440 : }
# 221 : :
# 222 : : static inline bool json_isspace(int ch)
# 223 : 4797708 : {
# 224 : 4797708 : switch (ch) {
# 225 [ - + ][ + + ]: 990650 : case 0x20:
# 226 [ - + ][ + + ]: 991652 : case 0x09:
# 227 [ - + ][ + + ]: 1010441 : case 0x0a:
# 228 [ - + ][ - + ]: 1010441 : case 0x0d:
# 229 : 1010441 : return true;
# 230 : :
# 231 [ + - ][ + + ]: 3787267 : default:
# 232 : 3787267 : return false;
# 233 : 4797708 : }
# 234 : :
# 235 : : // not reached
# 236 : 4797708 : }
# 237 : :
# 238 : : extern const UniValue NullUniValue;
# 239 : :
# 240 : : const UniValue& find_value( const UniValue& obj, const std::string& name);
# 241 : :
# 242 : : #endif // __UNIVALUE_H__
|