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