Remove duplicate include
[bitcoinplatinum.git] / src / univalue / include / univalue.h
blobe8ce283519fed9d3068f605855e18e34e2aad708
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.
6 #ifndef __UNIVALUE_H__
7 #define __UNIVALUE_H__
9 #include <stdint.h>
11 #include <string>
12 #include <vector>
13 #include <map>
14 #include <cassert>
16 #include <sstream> // .get_int64()
17 #include <utility> // std::pair
19 class UniValue {
20 public:
21 enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
23 UniValue() { typ = VNULL; }
24 UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
25 typ = initialType;
26 val = initialStr;
28 UniValue(uint64_t val_) {
29 setInt(val_);
31 UniValue(int64_t val_) {
32 setInt(val_);
34 UniValue(bool val_) {
35 setBool(val_);
37 UniValue(int val_) {
38 setInt(val_);
40 UniValue(double val_) {
41 setFloat(val_);
43 UniValue(const std::string& val_) {
44 setStr(val_);
46 UniValue(const char *val_) {
47 std::string s(val_);
48 setStr(s);
50 ~UniValue() {}
52 void clear();
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 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();
65 enum VType getType() const { return typ; }
66 const std::string& getValStr() const { return val; }
67 bool empty() const { return (values.size() == 0); }
69 size_t size() const { return values.size(); }
71 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 bool exists(const std::string& key) const { return (findKey(key) >= 0); }
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); }
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);
91 bool push_back(const char *val_) {
92 std::string s(val_);
93 return push_back(s);
95 bool push_backV(const std::vector<UniValue>& vec);
97 bool pushKV(const std::string& key, const UniValue& val);
98 bool pushKV(const std::string& key, const std::string& val_) {
99 UniValue tmpVal(VSTR, val_);
100 return pushKV(key, tmpVal);
102 bool pushKV(const std::string& key, const char *val_) {
103 std::string _val(val_);
104 return pushKV(key, _val);
106 bool pushKV(const std::string& key, int64_t val_) {
107 UniValue tmpVal(val_);
108 return pushKV(key, tmpVal);
110 bool pushKV(const std::string& key, uint64_t val_) {
111 UniValue tmpVal(val_);
112 return pushKV(key, tmpVal);
114 bool pushKV(const std::string& key, int val_) {
115 UniValue tmpVal((int64_t)val_);
116 return pushKV(key, tmpVal);
118 bool pushKV(const std::string& key, double val_) {
119 UniValue tmpVal(val_);
120 return pushKV(key, tmpVal);
122 bool pushKVs(const UniValue& obj);
124 std::string write(unsigned int prettyIndent = 0,
125 unsigned int indentLevel = 0) const;
127 bool read(const char *raw);
128 bool read(const std::string& rawStr) {
129 return read(rawStr.c_str());
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;
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;
142 public:
143 // Strict type-specific getters, these throw std::runtime_error if the
144 // value is of unexpected type
145 const std::vector<std::string>& getKeys() const;
146 const std::vector<UniValue>& getValues() const;
147 bool get_bool() const;
148 const 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;
155 enum VType type() const { return getType(); }
156 bool push_back(std::pair<std::string,UniValue> pear) {
157 return pushKV(pear.first, pear.second);
159 friend const UniValue& find_value( const UniValue& obj, const std::string& name);
163 // The following were added for compatibility with json_spirit.
164 // Most duplicate other methods, and should be removed.
166 static inline std::pair<std::string,UniValue> Pair(const char *cKey, const char *cVal)
168 std::string key(cKey);
169 UniValue uVal(cVal);
170 return std::make_pair(key, uVal);
173 static inline std::pair<std::string,UniValue> Pair(const char *cKey, std::string strVal)
175 std::string key(cKey);
176 UniValue uVal(strVal);
177 return std::make_pair(key, uVal);
180 static inline std::pair<std::string,UniValue> Pair(const char *cKey, uint64_t u64Val)
182 std::string key(cKey);
183 UniValue uVal(u64Val);
184 return std::make_pair(key, uVal);
187 static inline std::pair<std::string,UniValue> Pair(const char *cKey, int64_t i64Val)
189 std::string key(cKey);
190 UniValue uVal(i64Val);
191 return std::make_pair(key, uVal);
194 static inline std::pair<std::string,UniValue> Pair(const char *cKey, bool iVal)
196 std::string key(cKey);
197 UniValue uVal(iVal);
198 return std::make_pair(key, uVal);
201 static inline std::pair<std::string,UniValue> Pair(const char *cKey, int iVal)
203 std::string key(cKey);
204 UniValue uVal(iVal);
205 return std::make_pair(key, uVal);
208 static inline std::pair<std::string,UniValue> Pair(const char *cKey, double dVal)
210 std::string key(cKey);
211 UniValue uVal(dVal);
212 return std::make_pair(key, uVal);
215 static inline std::pair<std::string,UniValue> Pair(const char *cKey, const UniValue& uVal)
217 std::string key(cKey);
218 return std::make_pair(key, uVal);
221 static inline std::pair<std::string,UniValue> Pair(std::string key, const UniValue& uVal)
223 return std::make_pair(key, uVal);
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,
242 extern enum jtokentype getJsonToken(std::string& tokenVal,
243 unsigned int& consumed, const char *raw);
244 extern const char *uvTypeName(UniValue::VType t);
246 static inline bool jsonTokenIsValue(enum jtokentype jtt)
248 switch (jtt) {
249 case JTOK_KW_NULL:
250 case JTOK_KW_TRUE:
251 case JTOK_KW_FALSE:
252 case JTOK_NUMBER:
253 case JTOK_STRING:
254 return true;
256 default:
257 return false;
260 // not reached
263 static inline bool json_isspace(int ch)
265 switch (ch) {
266 case 0x20:
267 case 0x09:
268 case 0x0a:
269 case 0x0d:
270 return true;
272 default:
273 return false;
276 // not reached
279 extern const UniValue NullUniValue;
281 const UniValue& find_value( const UniValue& obj, const std::string& name);
283 #endif // __UNIVALUE_H__