Bitcoin Core  22.99.0
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 class UniValue {
18 public:
19  enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
20 
21  UniValue() { typ = VNULL; }
22  UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
23  typ = initialType;
24  val = initialStr;
25  }
26  UniValue(uint64_t val_) {
27  setInt(val_);
28  }
29  UniValue(int64_t val_) {
30  setInt(val_);
31  }
32  UniValue(bool val_) {
33  setBool(val_);
34  }
35  UniValue(int val_) {
36  setInt(val_);
37  }
38  UniValue(double val_) {
39  setFloat(val_);
40  }
41  UniValue(const std::string& val_) {
42  setStr(val_);
43  }
44  UniValue(const char *val_) {
45  std::string s(val_);
46  setStr(s);
47  }
48 
49  void clear();
50 
51  bool setNull();
52  bool setBool(bool val);
53  bool setNumStr(const std::string& val);
54  bool setInt(uint64_t val);
55  bool setInt(int64_t val);
56  bool setInt(int val_) { return setInt((int64_t)val_); }
57  bool setFloat(double val);
58  bool setStr(const std::string& val);
59  bool setArray();
60  bool setObject();
61 
62  enum VType getType() const { return typ; }
63  const std::string& getValStr() const { return val; }
64  bool empty() const { return (values.size() == 0); }
65 
66  size_t size() const { return values.size(); }
67 
68  bool getBool() const { return isTrue(); }
69  void getObjMap(std::map<std::string,UniValue>& kv) const;
70  bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
71  const UniValue& operator[](const std::string& key) const;
72  const UniValue& operator[](size_t index) const;
73  bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
74 
75  bool isNull() const { return (typ == VNULL); }
76  bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
77  bool isFalse() const { return (typ == VBOOL) && (val != "1"); }
78  bool isBool() const { return (typ == VBOOL); }
79  bool isStr() const { return (typ == VSTR); }
80  bool isNum() const { return (typ == VNUM); }
81  bool isArray() const { return (typ == VARR); }
82  bool isObject() const { return (typ == VOBJ); }
83 
84  bool push_back(const UniValue& val);
85  bool push_back(const std::string& val_) {
86  UniValue tmpVal(VSTR, val_);
87  return push_back(tmpVal);
88  }
89  bool push_back(const char *val_) {
90  std::string s(val_);
91  return push_back(s);
92  }
93  bool push_back(uint64_t val_) {
94  UniValue tmpVal(val_);
95  return push_back(tmpVal);
96  }
97  bool push_back(int64_t val_) {
98  UniValue tmpVal(val_);
99  return push_back(tmpVal);
100  }
101  bool push_back(bool 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, bool val_) {
134  UniValue tmpVal(val_);
135  return pushKV(key, tmpVal);
136  }
137  bool pushKV(const std::string& key, int val_) {
138  UniValue tmpVal((int64_t)val_);
139  return pushKV(key, tmpVal);
140  }
141  bool pushKV(const std::string& key, double val_) {
142  UniValue tmpVal(val_);
143  return pushKV(key, tmpVal);
144  }
145  bool pushKVs(const UniValue& obj);
146 
147  std::string write(unsigned int prettyIndent = 0,
148  unsigned int indentLevel = 0) const;
149 
150  bool read(const char *raw, size_t len);
151  bool read(const char *raw) { return read(raw, strlen(raw)); }
152  bool read(const std::string& rawStr) {
153  return read(rawStr.data(), rawStr.size());
154  }
155 
156 private:
158  std::string val; // numbers are stored as C++ strings
159  std::vector<std::string> keys;
160  std::vector<UniValue> values;
161 
162  bool findKey(const std::string& key, size_t& retIdx) const;
163  void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
164  void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
165 
166 public:
167  // Strict type-specific getters, these throw std::runtime_error if the
168  // value is of unexpected type
169  const std::vector<std::string>& getKeys() const;
170  const std::vector<UniValue>& getValues() const;
171  bool get_bool() const;
172  const std::string& get_str() const;
173  int get_int() const;
174  int64_t get_int64() const;
175  double get_real() const;
176  const UniValue& get_obj() const;
177  const UniValue& get_array() const;
178 
179  enum VType type() const { return getType(); }
180  friend const UniValue& find_value( const UniValue& obj, const std::string& name);
181 };
182 
184  JTOK_ERR = -1,
185  JTOK_NONE = 0, // eof
197 };
198 
199 extern enum jtokentype getJsonToken(std::string& tokenVal,
200  unsigned int& consumed, const char *raw, const char *end);
201 extern const char *uvTypeName(UniValue::VType t);
202 
203 static inline bool jsonTokenIsValue(enum jtokentype jtt)
204 {
205  switch (jtt) {
206  case JTOK_KW_NULL:
207  case JTOK_KW_TRUE:
208  case JTOK_KW_FALSE:
209  case JTOK_NUMBER:
210  case JTOK_STRING:
211  return true;
212 
213  default:
214  return false;
215  }
216 
217  // not reached
218 }
219 
220 static inline bool json_isspace(int ch)
221 {
222  switch (ch) {
223  case 0x20:
224  case 0x09:
225  case 0x0a:
226  case 0x0d:
227  return true;
228 
229  default:
230  return false;
231  }
232 
233  // not reached
234 }
235 
236 extern const UniValue NullUniValue;
237 
238 const UniValue& find_value( const UniValue& obj, const std::string& name);
239 
240 #endif // __UNIVALUE_H__
JTOK_NONE
@ JTOK_NONE
Definition: univalue.h:185
UniValue::pushKV
bool pushKV(const std::string &key, int64_t val_)
Definition: univalue.h:125
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:78
UniValue::typ
UniValue::VType typ
Definition: univalue.h:157
JTOK_ARR_CLOSE
@ JTOK_ARR_CLOSE
Definition: univalue.h:189
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:19
UniValue::writeObject
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
Definition: univalue_write.cpp:89
UniValue::get_bool
bool get_bool() const
Definition: univalue_get.cpp:91
JTOK_KW_TRUE
@ JTOK_KW_TRUE
Definition: univalue.h:193
UniValue::UniValue
UniValue(UniValue::VType initialType, const std::string &initialStr="")
Definition: univalue.h:22
UniValue::UniValue
UniValue(bool val_)
Definition: univalue.h:32
UniValue::pushKV
bool pushKV(const std::string &key, int val_)
Definition: univalue.h:137
UniValue::clear
void clear()
Definition: univalue.cpp:15
UniValue::pushKV
bool pushKV(const std::string &key, const char *val_)
Definition: univalue.h:121
JTOK_ARR_OPEN
@ JTOK_ARR_OPEN
Definition: univalue.h:188
UniValue::VType
VType
Definition: univalue.h:19
NullUniValue
const UniValue NullUniValue
Definition: univalue.cpp:13
string.h
UniValue::UniValue
UniValue(const std::string &val_)
Definition: univalue.h:41
UniValue::values
std::vector< UniValue > values
Definition: univalue.h:160
UniValue::operator[]
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:199
UniValue::read
bool read(const char *raw, size_t len)
Definition: univalue_read.cpp:259
UniValue::isNull
bool isNull() const
Definition: univalue.h:75
JTOK_OBJ_CLOSE
@ JTOK_OBJ_CLOSE
Definition: univalue.h:187
json_isspace
static bool json_isspace(int ch)
Definition: univalue.h:220
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:28
JTOK_COLON
@ JTOK_COLON
Definition: univalue.h:190
UniValue::isNum
bool isNum() const
Definition: univalue.h:80
UniValue::getType
enum VType getType() const
Definition: univalue.h:62
UniValue::val
std::string val
Definition: univalue.h:158
UniValue::getBool
bool getBool() const
Definition: univalue.h:68
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:152
UniValue
Definition: univalue.h:17
UniValue::setNull
bool setNull()
Definition: univalue.cpp:23
UniValue::isFalse
bool isFalse() const
Definition: univalue.h:77
UniValue::type
enum VType type() const
Definition: univalue.h:179
UniValue::pushKV
bool pushKV(const std::string &key, uint64_t val_)
Definition: univalue.h:129
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:98
UniValue::UniValue
UniValue(double val_)
Definition: univalue.h:38
UniValue::isStr
bool isStr() const
Definition: univalue.h:79
jsonTokenIsValue
static bool jsonTokenIsValue(enum jtokentype jtt)
Definition: univalue.h:203
UniValue::pushKV
bool pushKV(const std::string &key, const std::string &val_)
Definition: univalue.h:117
UniValue::get_int64
int64_t get_int64() const
Definition: univalue_get.cpp:115
JTOK_NUMBER
@ JTOK_NUMBER
Definition: univalue.h:195
UniValue::get_obj
const UniValue & get_obj() const
Definition: univalue_get.cpp:135
UniValue::pushKVs
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:146
UniValue::setInt
bool setInt(uint64_t val)
Definition: univalue.cpp:57
UniValue::push_back
bool push_back(int val_)
Definition: univalue.h:105
UniValue::push_back
bool push_back(uint64_t val_)
Definition: univalue.h:93
UniValue::read
bool read(const char *raw)
Definition: univalue.h:151
UniValue::setInt
bool setInt(int val_)
Definition: univalue.h:56
UniValue::UniValue
UniValue(uint64_t val_)
Definition: univalue.h:26
UniValue::exists
bool exists(const std::string &key) const
Definition: univalue.h:73
UniValue::VBOOL
@ VBOOL
Definition: univalue.h:19
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:186
UniValue::UniValue
UniValue(const char *val_)
Definition: univalue.h:44
UniValue::getKeys
const std::vector< std::string > & getKeys() const
Definition: univalue_get.cpp:77
uvTypeName
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:221
UniValue::UniValue
UniValue()
Definition: univalue.h:21
UniValue::VNUM
@ VNUM
Definition: univalue.h:19
JTOK_COMMA
@ JTOK_COMMA
Definition: univalue.h:191
JTOK_KW_NULL
@ JTOK_KW_NULL
Definition: univalue.h:192
UniValue::isArray
bool isArray() const
Definition: univalue.h:81
UniValue::pushKV
bool pushKV(const std::string &key, double val_)
Definition: univalue.h:141
UniValue::getObjMap
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:157
UniValue::push_back
bool push_back(int64_t val_)
Definition: univalue.h:97
name
const char * name
Definition: rest.cpp:52
UniValue::push_back
bool push_back(const std::string &val_)
Definition: univalue.h:85
UniValue::push_back
bool push_back(double val_)
Definition: univalue.h:109
JTOK_KW_FALSE
@ JTOK_KW_FALSE
Definition: univalue.h:194
UniValue::getValStr
const std::string & getValStr() const
Definition: univalue.h:63
UniValue::setNumStr
bool setNumStr(const std::string &val)
Definition: univalue.cpp:46
UniValue::writeArray
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
Definition: univalue_write.cpp:67
UniValue::get_int
int get_int() const
Definition: univalue_get.cpp:105
UniValue::push_back
bool push_back(const char *val_)
Definition: univalue.h:89
UniValue::find_value
const friend UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:236
UniValue::isTrue
bool isTrue() const
Definition: univalue.h:76
JTOK_STRING
@ JTOK_STRING
Definition: univalue.h:196
UniValue::keys
std::vector< std::string > keys
Definition: univalue.h:159
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
UniValue::UniValue
UniValue(int val_)
Definition: univalue.h:35
UniValue::pushKV
bool pushKV(const std::string &key, bool val_)
Definition: univalue.h:133
UniValue::empty
bool empty() const
Definition: univalue.h:64
JTOK_ERR
@ JTOK_ERR
Definition: univalue.h:184
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:66
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:29
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:236
jtokentype
jtokentype
Definition: univalue.h:183
UniValue::get_array
const UniValue & get_array() const
Definition: univalue_get.cpp:142
UniValue::VARR
@ VARR
Definition: univalue.h:19
UniValue::VNULL
@ VNULL
Definition: univalue.h:19
UniValue::VSTR
@ VSTR
Definition: univalue.h:19
UniValue::setObject
bool setObject()
Definition: univalue.cpp:101
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::push_back
bool push_back(bool val_)
Definition: univalue.h:101
UniValue::isObject
bool isObject() const
Definition: univalue.h:82