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 :
11 : #include <string>
12 : #include <vector>
13 : #include <map>
14 : #include <cassert>
15 :
16 : #include <sstream> // .get_int64()
17 : #include <utility> // std::pair
18 :
19 225524 : class UniValue {
20 : public:
21 : enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
22 :
23 42796 : UniValue() { typ = VNULL; }
24 33426 : UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
25 11142 : typ = initialType;
26 11142 : val = initialStr;
27 11142 : }
28 3429 : UniValue(uint64_t val_) {
29 1143 : setInt(val_);
30 1143 : }
31 9048 : UniValue(int64_t val_) {
32 3016 : setInt(val_);
33 3016 : }
34 4392 : UniValue(bool val_) {
35 1464 : setBool(val_);
36 1464 : }
37 16386 : UniValue(int val_) {
38 : setInt(val_);
39 5462 : }
40 3414 : UniValue(double val_) {
41 1138 : setFloat(val_);
42 1138 : }
43 13077 : UniValue(const std::string& val_) {
44 4359 : setStr(val_);
45 4359 : }
46 1245 : UniValue(const char *val_) {
47 830 : std::string s(val_);
48 415 : setStr(s);
49 415 : }
50 889438 : ~UniValue() {}
51 :
52 : void clear();
53 :
54 : bool setNull();
55 : bool setBool(bool val);
56 : bool setNumStr(const std::string& val);
57 : bool setInt(uint64_t val);
58 : bool setInt(int64_t val);
59 5464 : bool setInt(int val) { return setInt((int64_t)val); }
60 : bool setFloat(double val);
61 : bool setStr(const std::string& val);
62 : bool setArray();
63 : bool setObject();
64 :
65 0 : enum VType getType() const { return typ; }
66 251 : const std::string& getValStr() const { return val; }
67 214 : bool empty() const { return (values.size() == 0); }
68 :
69 13525 : size_t size() const { return values.size(); }
70 :
71 2 : bool getBool() const { return isTrue(); }
72 : bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes);
73 : const UniValue& operator[](const std::string& key) const;
74 : const UniValue& operator[](unsigned int index) const;
75 10 : bool exists(const std::string& key) const { return (findKey(key) >= 0); }
76 :
77 3 : bool isNull() const { return (typ == VNULL); }
78 10 : bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
79 4 : bool isFalse() const { return (typ == VBOOL) && (val != "1"); }
80 2 : bool isBool() const { return (typ == VBOOL); }
81 6 : bool isStr() const { return (typ == VSTR); }
82 12 : bool isNum() const { return (typ == VNUM); }
83 3 : bool isArray() const { return (typ == VARR); }
84 4 : bool isObject() const { return (typ == VOBJ); }
85 :
86 : bool push_back(const UniValue& val);
87 2505 : bool push_back(const std::string& val_) {
88 2505 : UniValue tmpVal(VSTR, val_);
89 2505 : return push_back(tmpVal);
90 : }
91 11 : bool push_back(const char *val_) {
92 22 : std::string s(val_);
93 22 : return push_back(s);
94 : }
95 : bool push_backV(const std::vector<UniValue>& vec);
96 :
97 : bool pushKV(const std::string& key, const UniValue& val);
98 2 : bool pushKV(const std::string& key, const std::string& val) {
99 2 : UniValue tmpVal(VSTR, val);
100 2 : return pushKV(key, tmpVal);
101 : }
102 1 : bool pushKV(const std::string& key, const char *val_) {
103 2 : std::string val(val_);
104 2 : return pushKV(key, val);
105 : }
106 1 : bool pushKV(const std::string& key, int64_t val) {
107 1 : UniValue tmpVal(val);
108 1 : return pushKV(key, tmpVal);
109 : }
110 1 : bool pushKV(const std::string& key, uint64_t val) {
111 1 : UniValue tmpVal(val);
112 1 : return pushKV(key, tmpVal);
113 : }
114 3 : bool pushKV(const std::string& key, int val) {
115 3 : UniValue tmpVal((int64_t)val);
116 3 : return pushKV(key, tmpVal);
117 : }
118 1 : bool pushKV(const std::string& key, double val) {
119 1 : UniValue tmpVal(val);
120 1 : return pushKV(key, tmpVal);
121 : }
122 : bool pushKVs(const UniValue& obj);
123 :
124 : std::string write(unsigned int prettyIndent = 0,
125 : unsigned int indentLevel = 0) const;
126 :
127 : bool read(const char *raw);
128 : bool read(const std::string& rawStr) {
129 3713 : return read(rawStr.c_str());
130 : }
131 :
132 : private:
133 : UniValue::VType typ;
134 : std::string val; // numbers are stored as C++ strings
135 : std::vector<std::string> keys;
136 : std::vector<UniValue> values;
137 :
138 : int findKey(const std::string& key) const;
139 : void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
140 : void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
141 :
142 : public:
143 : // Strict type-specific getters, these throw std::runtime_error if the
144 : // value is of unexpected type
145 : std::vector<std::string> getKeys() const;
146 : std::vector<UniValue> getValues() const;
147 : bool get_bool() const;
148 : std::string get_str() const;
149 : int get_int() const;
150 : int64_t get_int64() const;
151 : double get_real() const;
152 : const UniValue& get_obj() const;
153 : const UniValue& get_array() const;
154 :
155 1121 : enum VType type() const { return getType(); }
156 : bool push_back(std::pair<std::string,UniValue> pear) {
157 28241 : return pushKV(pear.first, pear.second);
158 : }
159 : friend const UniValue& find_value( const UniValue& obj, const std::string& name);
160 : };
161 :
162 : //
163 : // The following were added for compatibility with json_spirit.
164 : // Most duplicate other methods, and should be removed.
165 : //
166 321 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, const char *cVal)
167 : {
168 642 : std::string key(cKey);
169 642 : UniValue uVal(cVal);
170 963 : return std::make_pair(key, uVal);
171 : }
172 :
173 3927 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, std::string strVal)
174 : {
175 7854 : std::string key(cKey);
176 7854 : UniValue uVal(strVal);
177 11781 : return std::make_pair(key, uVal);
178 : }
179 :
180 1141 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, uint64_t u64Val)
181 : {
182 2282 : std::string key(cKey);
183 2282 : UniValue uVal(u64Val);
184 3423 : return std::make_pair(key, uVal);
185 : }
186 :
187 3010 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, int64_t i64Val)
188 : {
189 6020 : std::string key(cKey);
190 6020 : UniValue uVal(i64Val);
191 9030 : return std::make_pair(key, uVal);
192 : }
193 :
194 1459 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, bool iVal)
195 : {
196 2918 : std::string key(cKey);
197 2918 : UniValue uVal(iVal);
198 4377 : return std::make_pair(key, uVal);
199 : }
200 :
201 4431 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, int iVal)
202 : {
203 8862 : std::string key(cKey);
204 8862 : UniValue uVal(iVal);
205 13293 : return std::make_pair(key, uVal);
206 : }
207 :
208 1136 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, double dVal)
209 : {
210 2272 : std::string key(cKey);
211 2272 : UniValue uVal(dVal);
212 3408 : return std::make_pair(key, uVal);
213 : }
214 :
215 12800 : static inline std::pair<std::string,UniValue> Pair(const char *cKey, const UniValue& uVal)
216 : {
217 25600 : std::string key(cKey);
218 38400 : return std::make_pair(key, uVal);
219 : }
220 :
221 16 : static inline std::pair<std::string,UniValue> Pair(std::string key, const UniValue& uVal)
222 : {
223 32 : return std::make_pair(key, uVal);
224 : }
225 :
226 : enum jtokentype {
227 : JTOK_ERR = -1,
228 : JTOK_NONE = 0, // eof
229 : JTOK_OBJ_OPEN,
230 : JTOK_OBJ_CLOSE,
231 : JTOK_ARR_OPEN,
232 : JTOK_ARR_CLOSE,
233 : JTOK_COLON,
234 : JTOK_COMMA,
235 : JTOK_KW_NULL,
236 : JTOK_KW_TRUE,
237 : JTOK_KW_FALSE,
238 : JTOK_NUMBER,
239 : JTOK_STRING,
240 : };
241 :
242 : extern enum jtokentype getJsonToken(std::string& tokenVal,
243 : unsigned int& consumed, const char *raw);
244 : extern const char *uvTypeName(UniValue::VType t);
245 :
246 : extern const UniValue NullUniValue;
247 :
248 : const UniValue& find_value( const UniValue& obj, const std::string& name);
249 :
250 : #endif // __UNIVALUE_H__
|