Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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