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