Bitcoin Core  0.20.99
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(int val_) {
104  UniValue tmpVal(val_);
105  return push_back(tmpVal);
106  }
107  bool push_back(double val_) {
108  UniValue tmpVal(val_);
109  return push_back(tmpVal);
110  }
111  bool push_backV(const std::vector<UniValue>& vec);
112 
113  void __pushKV(const std::string& key, const UniValue& val);
114  bool pushKV(const std::string& key, const UniValue& val);
115  bool pushKV(const std::string& key, const std::string& val_) {
116  UniValue tmpVal(VSTR, val_);
117  return pushKV(key, tmpVal);
118  }
119  bool pushKV(const std::string& key, const char *val_) {
120  std::string _val(val_);
121  return pushKV(key, _val);
122  }
123  bool pushKV(const std::string& key, int64_t val_) {
124  UniValue tmpVal(val_);
125  return pushKV(key, tmpVal);
126  }
127  bool pushKV(const std::string& key, uint64_t val_) {
128  UniValue tmpVal(val_);
129  return pushKV(key, tmpVal);
130  }
131  bool pushKV(const std::string& key, bool val_) {
132  UniValue tmpVal((bool)val_);
133  return pushKV(key, tmpVal);
134  }
135  bool pushKV(const std::string& key, int val_) {
136  UniValue tmpVal((int64_t)val_);
137  return pushKV(key, tmpVal);
138  }
139  bool pushKV(const std::string& key, double val_) {
140  UniValue tmpVal(val_);
141  return pushKV(key, tmpVal);
142  }
143  bool pushKVs(const UniValue& obj);
144 
145  std::string write(unsigned int prettyIndent = 0,
146  unsigned int indentLevel = 0) const;
147 
148  bool read(const char *raw, size_t len);
149  bool read(const char *raw) { return read(raw, strlen(raw)); }
150  bool read(const std::string& rawStr) {
151  return read(rawStr.data(), rawStr.size());
152  }
153 
154 private:
156  std::string val; // numbers are stored as C++ strings
157  std::vector<std::string> keys;
158  std::vector<UniValue> values;
159 
160  bool findKey(const std::string& key, size_t& retIdx) const;
161  void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
162  void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
163 
164 public:
165  // Strict type-specific getters, these throw std::runtime_error if the
166  // value is of unexpected type
167  const std::vector<std::string>& getKeys() const;
168  const std::vector<UniValue>& getValues() const;
169  bool get_bool() const;
170  const std::string& get_str() const;
171  int get_int() const;
172  int64_t get_int64() const;
173  double get_real() const;
174  const UniValue& get_obj() const;
175  const UniValue& get_array() const;
176 
177  enum VType type() const { return getType(); }
178  friend const UniValue& find_value( const UniValue& obj, const std::string& name);
179 };
180 
182  JTOK_ERR = -1,
183  JTOK_NONE = 0, // eof
195 };
196 
197 extern enum jtokentype getJsonToken(std::string& tokenVal,
198  unsigned int& consumed, const char *raw, const char *end);
199 extern const char *uvTypeName(UniValue::VType t);
200 
201 static inline bool jsonTokenIsValue(enum jtokentype jtt)
202 {
203  switch (jtt) {
204  case JTOK_KW_NULL:
205  case JTOK_KW_TRUE:
206  case JTOK_KW_FALSE:
207  case JTOK_NUMBER:
208  case JTOK_STRING:
209  return true;
210 
211  default:
212  return false;
213  }
214 
215  // not reached
216 }
217 
218 static inline bool json_isspace(int ch)
219 {
220  switch (ch) {
221  case 0x20:
222  case 0x09:
223  case 0x0a:
224  case 0x0d:
225  return true;
226 
227  default:
228  return false;
229  }
230 
231  // not reached
232 }
233 
234 extern const UniValue NullUniValue;
235 
236 const UniValue& find_value( const UniValue& obj, const std::string& name);
237 
238 #endif // __UNIVALUE_H__
bool isObject() const
Definition: univalue.h:84
static bool jsonTokenIsValue(enum jtokentype jtt)
Definition: univalue.h:201
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:150
const std::vector< UniValue > & getValues() const
bool pushKV(const std::string &key, int val_)
Definition: univalue.h:135
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:119
bool read(const char *raw)
Definition: univalue.h:149
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:158
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:218
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:103
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:131
bool empty() const
Definition: univalue.h:66
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
jtokentype
Definition: univalue.h:181
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:139
bool pushKV(const std::string &key, const std::string &val_)
Definition: univalue.h:115
bool getBool() const
Definition: univalue.h:70
std::string val
Definition: univalue.h:156
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:155
enum VType type() const
Definition: univalue.h:177
bool setInt(uint64_t val)
Definition: univalue.cpp:57
bool pushKV(const std::string &key, uint64_t val_)
Definition: univalue.h:127
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:157
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:123
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
bool push_back(double val_)
Definition: univalue.h:107
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