Bitcoin Core  23.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 BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
7 #define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
8 
9 #include <charconv>
10 #include <cstdint>
11 #include <cstring>
12 #include <map>
13 #include <stdexcept>
14 #include <string>
15 #include <type_traits>
16 #include <vector>
17 
18 class UniValue {
19 public:
20  enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
21 
22  UniValue() { typ = VNULL; }
23  UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
24  typ = initialType;
25  val = initialStr;
26  }
27  template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,
28  std::enable_if_t<std::is_floating_point_v<T> || // setFloat
29  std::is_same_v<bool, T> || // setBool
30  std::is_signed_v<T> || std::is_unsigned_v<T> || // setInt
31  std::is_constructible_v<std::string, T>, // setStr
32  bool> = true>
33  UniValue(Ref&& val)
34  {
35  if constexpr (std::is_floating_point_v<T>) {
36  setFloat(val);
37  } else if constexpr (std::is_same_v<bool, T>) {
38  setBool(val);
39  } else if constexpr (std::is_signed_v<T>) {
40  setInt(int64_t{val});
41  } else if constexpr (std::is_unsigned_v<T>) {
42  setInt(uint64_t{val});
43  } else {
44  setStr(std::string{std::forward<Ref>(val)});
45  }
46  }
47 
48  void clear();
49 
50  void setNull();
51  void setBool(bool val);
52  void setNumStr(const std::string& val);
53  void setInt(uint64_t val);
54  void setInt(int64_t val);
55  void setInt(int val_) { return setInt(int64_t{val_}); }
56  void setFloat(double val);
57  void setStr(const std::string& val);
58  void setArray();
59  void setObject();
60 
61  enum VType getType() const { return typ; }
62  const std::string& getValStr() const { return val; }
63  bool empty() const { return (values.size() == 0); }
64 
65  size_t size() const { return values.size(); }
66 
67  bool getBool() const { return isTrue(); }
68  void getObjMap(std::map<std::string,UniValue>& kv) const;
69  bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
70  const UniValue& operator[](const std::string& key) const;
71  const UniValue& operator[](size_t index) const;
72  bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
73 
74  bool isNull() const { return (typ == VNULL); }
75  bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
76  bool isFalse() const { return (typ == VBOOL) && (val != "1"); }
77  bool isBool() const { return (typ == VBOOL); }
78  bool isStr() const { return (typ == VSTR); }
79  bool isNum() const { return (typ == VNUM); }
80  bool isArray() const { return (typ == VARR); }
81  bool isObject() const { return (typ == VOBJ); }
82 
83  void push_back(const UniValue& val);
84  void push_backV(const std::vector<UniValue>& vec);
85  template <class It>
86  void push_backV(It first, It last);
87 
88  void __pushKV(const std::string& key, const UniValue& val);
89  void pushKV(const std::string& key, const UniValue& val);
90  void pushKVs(const UniValue& obj);
91 
92  std::string write(unsigned int prettyIndent = 0,
93  unsigned int indentLevel = 0) const;
94 
95  bool read(const char *raw, size_t len);
96  bool read(const char *raw) { return read(raw, strlen(raw)); }
97  bool read(const std::string& rawStr) {
98  return read(rawStr.data(), rawStr.size());
99  }
100 
101 private:
103  std::string val; // numbers are stored as C++ strings
104  std::vector<std::string> keys;
105  std::vector<UniValue> values;
106 
107  void checkType(const VType& expected) const;
108  bool findKey(const std::string& key, size_t& retIdx) const;
109  void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
110  void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
111 
112 public:
113  // Strict type-specific getters, these throw std::runtime_error if the
114  // value is of unexpected type
115  const std::vector<std::string>& getKeys() const;
116  const std::vector<UniValue>& getValues() const;
117  template <typename Int>
118  Int getInt() const;
119  bool get_bool() const;
120  const std::string& get_str() const;
121  double get_real() const;
122  const UniValue& get_obj() const;
123  const UniValue& get_array() const;
124 
125  enum VType type() const { return getType(); }
126  friend const UniValue& find_value( const UniValue& obj, const std::string& name);
127 };
128 
129 template <class It>
130 void UniValue::push_backV(It first, It last)
131 {
132  checkType(VARR);
133  values.insert(values.end(), first, last);
134 }
135 
136 template <typename Int>
137 Int UniValue::getInt() const
138 {
139  static_assert(std::is_integral<Int>::value);
140  checkType(VNUM);
141  Int result;
142  const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result);
143  if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) {
144  throw std::runtime_error("JSON integer out of range");
145  }
146  return result;
147 }
148 
150  JTOK_ERR = -1,
151  JTOK_NONE = 0, // eof
163 };
164 
165 extern enum jtokentype getJsonToken(std::string& tokenVal,
166  unsigned int& consumed, const char *raw, const char *end);
167 extern const char *uvTypeName(UniValue::VType t);
168 
169 static inline bool jsonTokenIsValue(enum jtokentype jtt)
170 {
171  switch (jtt) {
172  case JTOK_KW_NULL:
173  case JTOK_KW_TRUE:
174  case JTOK_KW_FALSE:
175  case JTOK_NUMBER:
176  case JTOK_STRING:
177  return true;
178 
179  default:
180  return false;
181  }
182 
183  // not reached
184 }
185 
186 static inline bool json_isspace(int ch)
187 {
188  switch (ch) {
189  case 0x20:
190  case 0x09:
191  case 0x0a:
192  case 0x0d:
193  return true;
194 
195  default:
196  return false;
197  }
198 
199  // not reached
200 }
201 
202 extern const UniValue NullUniValue;
203 
204 const UniValue& find_value( const UniValue& obj, const std::string& name);
205 
206 #endif // BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
UniValue::setStr
void setStr(const std::string &val)
Definition: univalue.cpp:85
UniValue::pushKV
void pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:126
JTOK_NONE
@ JTOK_NONE
Definition: univalue.h:151
getJsonToken
enum jtokentype getJsonToken(std::string &tokenVal, unsigned int &consumed, const char *raw, const char *end)
Definition: univalue_read.cpp:54
UniValue::isBool
bool isBool() const
Definition: univalue.h:77
UniValue::typ
UniValue::VType typ
Definition: univalue.h:102
JTOK_ARR_CLOSE
@ JTOK_ARR_CLOSE
Definition: univalue.h:155
UniValue::setInt
void setInt(int val_)
Definition: univalue.h:55
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:20
UniValue::writeObject
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
Definition: univalue_write.cpp:91
UniValue::get_bool
bool get_bool() const
Definition: univalue_get.cpp:60
JTOK_KW_TRUE
@ JTOK_KW_TRUE
Definition: univalue.h:159
UniValue::UniValue
UniValue(UniValue::VType initialType, const std::string &initialStr="")
Definition: univalue.h:23
UniValue::setBool
void setBool(bool val)
Definition: univalue.cpp:31
UniValue::clear
void clear()
Definition: univalue.cpp:18
JTOK_ARR_OPEN
@ JTOK_ARR_OPEN
Definition: univalue.h:154
UniValue::VType
VType
Definition: univalue.h:20
UniValue::setArray
void setArray()
Definition: univalue.cpp:92
NullUniValue
const UniValue NullUniValue
Definition: univalue.cpp:16
UniValue::UniValue
UniValue(Ref &&val)
Definition: univalue.h:33
UniValue::values
std::vector< UniValue > values
Definition: univalue.h:105
UniValue::operator[]
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:188
UniValue::setObject
void setObject()
Definition: univalue.cpp:98
UniValue::read
bool read(const char *raw, size_t len)
Definition: univalue_read.cpp:262
UniValue::getInt
Int getInt() const
Definition: univalue.h:137
UniValue::isNull
bool isNull() const
Definition: univalue.h:74
JTOK_OBJ_CLOSE
@ JTOK_OBJ_CLOSE
Definition: univalue.h:153
json_isspace
static bool json_isspace(int ch)
Definition: univalue.h:186
UniValue::setNull
void setNull()
Definition: univalue.cpp:26
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:30
JTOK_COLON
@ JTOK_COLON
Definition: univalue.h:156
UniValue::isNum
bool isNum() const
Definition: univalue.h:79
UniValue::getType
enum VType getType() const
Definition: univalue.h:61
UniValue::val
std::string val
Definition: univalue.h:103
UniValue::getBool
bool getBool() const
Definition: univalue.h:67
UniValue::checkObject
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:168
UniValue::read
bool read(const std::string &rawStr)
Definition: univalue.h:97
UniValue
Definition: univalue.h:18
UniValue::isFalse
bool isFalse() const
Definition: univalue.h:76
UniValue::type
enum VType type() const
Definition: univalue.h:125
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:66
UniValue::isStr
bool isStr() const
Definition: univalue.h:78
jsonTokenIsValue
static bool jsonTokenIsValue(enum jtokentype jtt)
Definition: univalue.h:169
JTOK_NUMBER
@ JTOK_NUMBER
Definition: univalue.h:161
UniValue::get_obj
const UniValue & get_obj() const
Definition: univalue_get.cpp:81
UniValue::read
bool read(const char *raw)
Definition: univalue.h:96
UniValue::exists
bool exists(const std::string &key) const
Definition: univalue.h:72
UniValue::VBOOL
@ VBOOL
Definition: univalue.h:20
UniValue::__pushKV
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:118
JTOK_OBJ_OPEN
@ JTOK_OBJ_OPEN
Definition: univalue.h:152
UniValue::getKeys
const std::vector< std::string > & getKeys() const
Definition: univalue_get.cpp:47
uvTypeName
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:218
UniValue::UniValue
UniValue()
Definition: univalue.h:22
UniValue::VNUM
@ VNUM
Definition: univalue.h:20
JTOK_COMMA
@ JTOK_COMMA
Definition: univalue.h:157
JTOK_KW_NULL
@ JTOK_KW_NULL
Definition: univalue.h:158
UniValue::isArray
bool isArray() const
Definition: univalue.h:80
UniValue::push_back
void push_back(const UniValue &val)
Definition: univalue.cpp:104
UniValue::checkType
void checkType(const VType &expected) const
Definition: univalue.cpp:210
UniValue::getObjMap
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:146
name
const char * name
Definition: rest.cpp:46
UniValue::setFloat
void setFloat(double val)
Definition: univalue.cpp:76
UniValue::pushKVs
void pushKVs(const UniValue &obj)
Definition: univalue.cpp:137
JTOK_KW_FALSE
@ JTOK_KW_FALSE
Definition: univalue.h:160
UniValue::getValStr
const std::string & getValStr() const
Definition: univalue.h:62
UniValue::writeArray
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
Definition: univalue_write.cpp:69
UniValue::setNumStr
void setNumStr(const std::string &val)
Definition: univalue.cpp:47
UniValue::find_value
const friend UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:233
UniValue::isTrue
bool isTrue() const
Definition: univalue.h:75
JTOK_STRING
@ JTOK_STRING
Definition: univalue.h:162
UniValue::keys
std::vector< std::string > keys
Definition: univalue.h:104
UniValue::empty
bool empty() const
Definition: univalue.h:63
JTOK_ERR
@ JTOK_ERR
Definition: univalue.h:150
UniValue::getValues
const std::vector< UniValue > & getValues() const
Definition: univalue_get.cpp:53
UniValue::get_real
double get_real() const
Definition: univalue_get.cpp:72
UniValue::size
size_t size() const
Definition: univalue.h:65
UniValue::findKey
bool findKey(const std::string &key, size_t &retIdx) const
Definition: univalue.cpp:156
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:233
jtokentype
jtokentype
Definition: univalue.h:149
UniValue::get_array
const UniValue & get_array() const
Definition: univalue_get.cpp:87
UniValue::VARR
@ VARR
Definition: univalue.h:20
UniValue::VNULL
@ VNULL
Definition: univalue.h:20
UniValue::setInt
void setInt(uint64_t val)
Definition: univalue.cpp:58
UniValue::VSTR
@ VSTR
Definition: univalue.h:20
UniValue::push_backV
void push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:111
UniValue::isObject
bool isObject() const
Definition: univalue.h:81