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