Fabcoin Core  0.16.2
P2P Digital Currency
univalue.h
Go to the documentation of this file.
1 // Copyright 2014 BitPay Inc.
2 // Copyright 2015 Fabcoin 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 #include <utility> // std::pair
19 
20 class UniValue {
21 public:
22  enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
23 
24  UniValue() { typ = VNULL; }
25  UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
26  typ = initialType;
27  val = initialStr;
28  }
29  UniValue(uint64_t val_) {
30  setInt(val_);
31  }
32  UniValue(int64_t val_) {
33  setInt(val_);
34  }
35  UniValue(bool val_) {
36  setBool(val_);
37  }
38  UniValue(int val_) {
39  setInt(val_);
40  }
41  UniValue(double val_) {
42  setFloat(val_);
43  }
44  UniValue(const std::string& val_) {
45  setStr(val_);
46  }
47  UniValue(const char *val_) {
48  std::string s(val_);
49  setStr(s);
50  }
51  ~UniValue() {}
52 
53  void clear();
54 
55  bool setNull();
56  bool setBool(bool val);
57  bool setNumStr(const std::string& val);
58  bool setInt(uint64_t val);
59  bool setInt(int64_t val);
60  bool setInt(int val_) { return setInt((int64_t)val_); }
61  bool setFloat(double val);
62  bool setStr(const std::string& val);
63  bool setArray();
64  bool setObject();
65 
66  enum VType getType() const { return typ; }
67  const std::string& getValStr() const { return val; }
68  bool empty() const { return (values.size() == 0); }
69 
70  size_t size() const { return values.size(); }
71 
72  bool getBool() const { return isTrue(); }
73  void getObjMap(std::map<std::string,UniValue>& kv) const;
74  bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
75  const UniValue& operator[](const std::string& key) const;
76  const UniValue& operator[](size_t index) const;
77  bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
78 
79  bool isNull() const { return (typ == VNULL); }
80  bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
81  bool isFalse() const { return (typ == VBOOL) && (val != "1"); }
82  bool isBool() const { return (typ == VBOOL); }
83  bool isStr() const { return (typ == VSTR); }
84  bool isNum() const { return (typ == VNUM); }
85  bool isArray() const { return (typ == VARR); }
86  bool isObject() const { return (typ == VOBJ); }
87 
88  bool push_back(const UniValue& val);
89  bool push_back(const std::string& val_) {
90  UniValue tmpVal(VSTR, val_);
91  return push_back(tmpVal);
92  }
93  bool push_back(const char *val_) {
94  std::string s(val_);
95  return push_back(s);
96  }
97  bool push_back(uint64_t val_) {
98  UniValue tmpVal(val_);
99  return push_back(tmpVal);
100  }
101  bool push_back(int64_t val_) {
102  UniValue tmpVal(val_);
103  return push_back(tmpVal);
104  }
105  bool push_back(int val_) {
106  UniValue tmpVal(val_);
107  return push_back(tmpVal);
108  }
109  bool push_back(double val_) {
110  UniValue tmpVal(val_);
111  return push_back(tmpVal);
112  }
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  bool pushKV(const std::string& key, const std::string& val_) {
118  UniValue tmpVal(VSTR, val_);
119  return pushKV(key, tmpVal);
120  }
121  bool pushKV(const std::string& key, const char *val_) {
122  std::string _val(val_);
123  return pushKV(key, _val);
124  }
125  bool pushKV(const std::string& key, int64_t val_) {
126  UniValue tmpVal(val_);
127  return pushKV(key, tmpVal);
128  }
129  bool pushKV(const std::string& key, uint64_t val_) {
130  UniValue tmpVal(val_);
131  return pushKV(key, tmpVal);
132  }
133  bool pushKV(const std::string& key, int val_) {
134  UniValue tmpVal((int64_t)val_);
135  return pushKV(key, tmpVal);
136  }
137  bool pushKV(const std::string& key, double val_) {
138  UniValue tmpVal(val_);
139  return pushKV(key, tmpVal);
140  }
141  bool pushKVs(const UniValue& obj);
142 
143  std::string write(unsigned int prettyIndent = 0,
144  unsigned int indentLevel = 0) const;
145 
146  bool read(const char *raw, size_t len);
147  bool read(const char *raw) { return read(raw, strlen(raw)); }
148  bool read(const std::string& rawStr) {
149  return read(rawStr.data(), rawStr.size());
150  }
151 
152 private:
154  std::string val; // numbers are stored as C++ strings
155  std::vector<std::string> keys;
156  std::vector<UniValue> values;
157 
158  bool findKey(const std::string& key, size_t& retIdx) const;
159  void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
160  void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
161 
162 public:
163  // Strict type-specific getters, these throw std::runtime_error if the
164  // value is of unexpected type
165  const std::vector<std::string>& getKeys() const;
166  const std::vector<UniValue>& getValues() const;
167  bool get_bool() const;
168  const std::string& get_str() const;
169  int get_int() const;
170  int64_t get_int64() const;
171  double get_real() const;
172  const UniValue& get_obj() const;
173  const UniValue& get_array() const;
174 
175  enum VType type() const { return getType(); }
176  bool push_back(std::pair<std::string,UniValue> pear) {
177  return pushKV(pear.first, pear.second);
178  }
179  friend const UniValue& find_value( const UniValue& obj, const std::string& name);
180 };
181 
182 //
183 // The following were added for compatibility with json_spirit.
184 // Most duplicate other methods, and should be removed.
185 //
186 static inline std::pair<std::string,UniValue> Pair(const char *cKey, const char *cVal)
187 {
188  std::string key(cKey);
189  UniValue uVal(cVal);
190  return std::make_pair(key, uVal);
191 }
192 
193 static inline std::pair<std::string,UniValue> Pair(const char *cKey, std::string strVal)
194 {
195  std::string key(cKey);
196  UniValue uVal(strVal);
197  return std::make_pair(key, uVal);
198 }
199 
200 static inline std::pair<std::string,UniValue> Pair(const char *cKey, uint64_t u64Val)
201 {
202  std::string key(cKey);
203  UniValue uVal(u64Val);
204  return std::make_pair(key, uVal);
205 }
206 
207 static inline std::pair<std::string,UniValue> Pair(const char *cKey, int64_t i64Val)
208 {
209  std::string key(cKey);
210  UniValue uVal(i64Val);
211  return std::make_pair(key, uVal);
212 }
213 
214 static inline std::pair<std::string,UniValue> Pair(const char *cKey, bool iVal)
215 {
216  std::string key(cKey);
217  UniValue uVal(iVal);
218  return std::make_pair(key, uVal);
219 }
220 
221 static inline std::pair<std::string,UniValue> Pair(const char *cKey, int iVal)
222 {
223  std::string key(cKey);
224  UniValue uVal(iVal);
225  return std::make_pair(key, uVal);
226 }
227 
228 static inline std::pair<std::string,UniValue> Pair(const char *cKey, double dVal)
229 {
230  std::string key(cKey);
231  UniValue uVal(dVal);
232  return std::make_pair(key, uVal);
233 }
234 
235 static inline std::pair<std::string,UniValue> Pair(const char *cKey, const UniValue& uVal)
236 {
237  std::string key(cKey);
238  return std::make_pair(key, uVal);
239 }
240 
241 static inline std::pair<std::string,UniValue> Pair(std::string key, const UniValue& uVal)
242 {
243  return std::make_pair(key, uVal);
244 }
245 
247  JTOK_ERR = -1,
248  JTOK_NONE = 0, // eof
260 };
261 
262 extern enum jtokentype getJsonToken(std::string& tokenVal,
263  unsigned int& consumed, const char *raw, const char *end);
264 extern const char *uvTypeName(UniValue::VType t);
265 
266 static inline bool jsonTokenIsValue(enum jtokentype jtt)
267 {
268  switch (jtt) {
269  case JTOK_KW_NULL:
270  case JTOK_KW_TRUE:
271  case JTOK_KW_FALSE:
272  case JTOK_NUMBER:
273  case JTOK_STRING:
274  return true;
275 
276  default:
277  return false;
278  }
279 
280  // not reached
281 }
282 
283 static inline bool json_isspace(int ch)
284 {
285  switch (ch) {
286  case 0x20:
287  case 0x09:
288  case 0x0a:
289  case 0x0d:
290  return true;
291 
292  default:
293  return false;
294  }
295 
296  // not reached
297 }
298 
299 extern const UniValue NullUniValue;
300 
301 const UniValue& find_value( const UniValue& obj, const std::string& name);
302 
303 #endif // __UNIVALUE_H__
int get_int() const
const std::string & get_str() const
size_t size() const
Definition: univalue.h:70
bool setNumStr(const std::string &val)
Definition: univalue.cpp:48
bool read(const std::string &rawStr)
Definition: univalue.h:148
double get_real() const
bool pushKV(const std::string &key, int val_)
Definition: univalue.h:133
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
UniValue(double val_)
Definition: univalue.h:41
UniValue(int64_t val_)
Definition: univalue.h:32
bool read(const char *raw, size_t len)
UniValue(const std::string &val_)
Definition: univalue.h:44
bool pushKV(const std::string &key, const char *val_)
Definition: univalue.h:121
~UniValue()
Definition: univalue.h:51
bool empty() const
Definition: univalue.h:68
bool read(const char *raw)
Definition: univalue.h:147
const std::vector< UniValue > & getValues() const
bool push_back(uint64_t val_)
Definition: univalue.h:97
UniValue(const char *val_)
Definition: univalue.h:47
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:159
bool isArray() const
Definition: univalue.h:85
std::vector< UniValue > values
Definition: univalue.h:156
bool isBool() const
Definition: univalue.h:82
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:148
enum VType type() const
Definition: univalue.h:175
UniValue(uint64_t val_)
Definition: univalue.h:29
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:199
enum VType getType() const
Definition: univalue.h:66
bool push_back(const std::string &val_)
Definition: univalue.h:89
enum jtokentype getJsonToken(std::string &tokenVal, unsigned int &consumed, const char *raw, const char *end)
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:221
int64_t get_int64() const
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
bool push_back(int val_)
Definition: univalue.h:105
const char * name
Definition: rest.cpp:36
bool isNum() const
Definition: univalue.h:84
UniValue()
Definition: univalue.h:24
bool getBool() const
Definition: univalue.h:72
jtokentype
Definition: univalue.h:246
bool setFloat(double val)
Definition: univalue.cpp:77
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
bool setNull()
Definition: univalue.cpp:25
bool push_back(int64_t val_)
Definition: univalue.h:101
bool pushKV(const std::string &key, double val_)
Definition: univalue.h:137
bool pushKV(const std::string &key, const std::string &val_)
Definition: univalue.h:117
std::string val
Definition: univalue.h:154
bool isFalse() const
Definition: univalue.h:81
bool setStr(const std::string &val)
Definition: univalue.cpp:88
UniValue::VType typ
Definition: univalue.h:153
bool setInt(uint64_t val)
Definition: univalue.cpp:59
bool pushKV(const std::string &key, uint64_t val_)
Definition: univalue.h:129
const std::string & getValStr() const
Definition: univalue.h:67
bool setArray()
Definition: univalue.cpp:96
const UniValue & get_array() const
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:119
UniValue(int val_)
Definition: univalue.h:38
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
bool setObject()
Definition: univalue.cpp:103
bool setBool(bool val)
Definition: univalue.cpp:31
std::vector< std::string > keys
Definition: univalue.h:155
const UniValue NullUniValue
Definition: univalue.cpp:15
bool setInt(int val_)
Definition: univalue.h:60
bool push_back(std::pair< std::string, UniValue > pear)
Definition: univalue.h:176
bool isObject() const
Definition: univalue.h:86
friend const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:236
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:181
bool get_bool() const
void clear()
Definition: univalue.cpp:17
bool exists(const std::string &key) const
Definition: univalue.h:77
const std::vector< std::string > & getKeys() const
bool findKey(const std::string &key, size_t &retIdx) const
Definition: univalue.cpp:169
bool pushKV(const std::string &key, int64_t val_)
Definition: univalue.h:125
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:129
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
Definition: picosha2.h:73
const UniValue & get_obj() const
bool isNull() const
Definition: univalue.h:79
bool push_back(double val_)
Definition: univalue.h:109
bool isTrue() const
Definition: univalue.h:80
bool isStr() const
Definition: univalue.h:83
UniValue(bool val_)
Definition: univalue.h:35
UniValue(UniValue::VType initialType, const std::string &initialStr="")
Definition: univalue.h:25
bool push_back(const char *val_)
Definition: univalue.h:93