Bitcoin ABC  0.24.7
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__
JTOK_NONE
@ JTOK_NONE
Definition: univalue.h:208
getJsonToken
enum jtokentype getJsonToken(std::string &tokenVal, unsigned int &consumed, const char *raw, const char *end)
Definition: univalue_read.cpp:51
UniValue::isBool
bool isBool() const
Definition: univalue.h:92
UniValue::typ
UniValue::VType typ
Definition: univalue.h:115
JTOK_ARR_CLOSE
@ JTOK_ARR_CLOSE
Definition: univalue.h:212
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:27
UniValue::get_bool
bool get_bool() const
Definition: univalue_get.cpp:91
JTOK_KW_TRUE
@ JTOK_KW_TRUE
Definition: univalue.h:216
UniValue::UniValue
UniValue(bool val_)
Definition: univalue.h:37
UniValue::clear
void clear()
Definition: univalue.cpp:15
JTOK_ARR_OPEN
@ JTOK_ARR_OPEN
Definition: univalue.h:211
UniValue::VType
VType
Definition: univalue.h:27
NullUniValue
const UniValue NullUniValue
Definition: univalue.cpp:13
string.h
UniValue::UniValue
UniValue(const std::string &val_)
Definition: univalue.h:46
UniValue::push_back
bool push_back(std::pair< std::string, UniValue > pear)
Definition: univalue.h:136
UniValue::values
std::vector< UniValue > values
Definition: univalue.h:118
UniValue::operator[]
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:197
UniValue::read
bool read(const char *raw, size_t len)
Definition: univalue_read.cpp:259
UniValue::isNull
bool isNull() const
Definition: univalue.h:89
JTOK_OBJ_CLOSE
@ JTOK_OBJ_CLOSE
Definition: univalue.h:210
json_isspace
static bool json_isspace(int ch)
Definition: univalue.h:243
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:159
JTOK_COLON
@ JTOK_COLON
Definition: univalue.h:213
UniValue::isNum
bool isNum() const
Definition: univalue.h:94
UniValue::getType
enum VType getType() const
Definition: univalue.h:76
UniValue::val
std::string val
Definition: univalue.h:116
UniValue::getBool
bool getBool() const
Definition: univalue.h:82
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue::checkObject
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:179
UniValue::read
bool read(const std::string &rawStr)
Definition: univalue.h:110
UniValue
Definition: univalue.h:23
UniValue::setNull
bool setNull()
Definition: univalue.cpp:23
UniValue::isFalse
bool isFalse() const
Definition: univalue.h:91
UniValue::type
enum VType type() const
Definition: univalue.h:135
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:98
UniValue::UniValue
UniValue(double val_)
Definition: univalue.h:43
UniValue::isStr
bool isStr() const
Definition: univalue.h:93
jsonTokenIsValue
static bool jsonTokenIsValue(enum jtokentype jtt)
Definition: univalue.h:226
UniValue::get_int64
int64_t get_int64() const
Definition: univalue_get.cpp:115
JTOK_NUMBER
@ JTOK_NUMBER
Definition: univalue.h:218
UniValue::get_obj
const UniValue & get_obj() const
Definition: univalue_get.cpp:135
Pair
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:146
UniValue::pushKVs
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:146
UniValue::setInt
bool setInt(uint64_t val)
Definition: univalue.cpp:57
UniValue::read
bool read(const char *raw)
Definition: univalue.h:109
UniValue::setInt
bool setInt(int val_)
Definition: univalue.h:70
UniValue::UniValue
UniValue(uint64_t val_)
Definition: univalue.h:31
UniValue::exists
bool exists(const std::string &key) const
Definition: univalue.h:87
UniValue::VBOOL
@ VBOOL
Definition: univalue.h:27
UniValue::push_backV
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:117
UniValue::__pushKV
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
JTOK_OBJ_OPEN
@ JTOK_OBJ_OPEN
Definition: univalue.h:209
UniValue::UniValue
UniValue(const char *val_)
Definition: univalue.h:49
UniValue::getKeys
const std::vector< std::string > & getKeys() const
Definition: univalue_get.cpp:77
uvTypeName
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:219
UniValue::UniValue
UniValue()
Definition: univalue.h:29
UniValue::VNUM
@ VNUM
Definition: univalue.h:27
JTOK_COMMA
@ JTOK_COMMA
Definition: univalue.h:214
UniValue::UniValue
UniValue(UniValue::VType type, const std::string &value=std::string())
Definition: univalue.h:30
JTOK_KW_NULL
@ JTOK_KW_NULL
Definition: univalue.h:215
UniValue::isArray
bool isArray() const
Definition: univalue.h:95
UniValue::getObjMap
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:157
name
const char * name
Definition: rest.cpp:43
JTOK_KW_FALSE
@ JTOK_KW_FALSE
Definition: univalue.h:217
UniValue::getValStr
const std::string & getValStr() const
Definition: univalue.h:77
UniValue::setNumStr
bool setNumStr(const std::string &val)
Definition: univalue.cpp:46
UniValue::get_int
int get_int() const
Definition: univalue_get.cpp:105
UniValue::find_value
const friend UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
UniValue::isTrue
bool isTrue() const
Definition: univalue.h:90
JTOK_STRING
@ JTOK_STRING
Definition: univalue.h:219
UniValue::keys
std::vector< std::string > keys
Definition: univalue.h:117
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
UniValue::UniValue
UniValue(int val_)
Definition: univalue.h:40
UniValue::empty
bool empty() const
Definition: univalue.h:78
JTOK_ERR
@ JTOK_ERR
Definition: univalue.h:207
UniValue::getValues
const std::vector< UniValue > & getValues() const
Definition: univalue_get.cpp:84
UniValue::get_real
double get_real() const
Definition: univalue_get.cpp:125
UniValue::setBool
bool setBool(bool val)
Definition: univalue.cpp:29
UniValue::size
size_t size() const
Definition: univalue.h:80
UniValue::findKey
bool findKey(const std::string &key, size_t &retIdx) const
Definition: univalue.cpp:167
UniValue::UniValue
UniValue(int64_t val_)
Definition: univalue.h:34
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
jtokentype
jtokentype
Definition: univalue.h:206
UniValue::get_array
const UniValue & get_array() const
Definition: univalue_get.cpp:142
UniValue::VARR
@ VARR
Definition: univalue.h:27
UniValue::VNULL
@ VNULL
Definition: univalue.h:27
UniValue::VSTR
@ VSTR
Definition: univalue.h:27
UniValue::setObject
bool setObject()
Definition: univalue.cpp:101
UniValue::reserve
void reserve(size_t n)
Definition: univalue.h:55
UniValue::setFloat
bool setFloat(double val)
Definition: univalue.cpp:75
UniValue::setStr
bool setStr(const std::string &val)
Definition: univalue.cpp:86
UniValue::setArray
bool setArray()
Definition: univalue.cpp:94
UniValue::isObject
bool isObject() const
Definition: univalue.h:96