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