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.
16 #include <sstream> // .get_int64()
17 #include <utility> // std::pair
21 enum VType
{ VNULL
, VOBJ
, VARR
, VSTR
, VNUM
, VBOOL
, };
23 UniValue() { typ
= VNULL
; }
24 UniValue(UniValue::VType initialType
, const std::string
& initialStr
= "") {
28 UniValue(uint64_t val_
) {
31 UniValue(int64_t val_
) {
40 UniValue(double val_
) {
43 UniValue(const std::string
& val_
) {
46 UniValue(const char *val_
) {
55 bool setBool(bool val
);
56 bool setNumStr(const std::string
& val
);
57 bool setInt(uint64_t val
);
58 bool setInt(int64_t val
);
59 bool setInt(int val_
) { return setInt((int64_t)val_
); }
60 bool setFloat(double val
);
61 bool setStr(const std::string
& val
);
65 enum VType
getType() const { return typ
; }
66 const std::string
& getValStr() const { return val
; }
67 bool empty() const { return (values
.size() == 0); }
69 size_t size() const { return values
.size(); }
71 bool getBool() const { return isTrue(); }
72 bool checkObject(const std::map
<std::string
,UniValue::VType
>& memberTypes
);
73 const UniValue
& operator[](const std::string
& key
) const;
74 const UniValue
& operator[](unsigned int index
) const;
75 bool exists(const std::string
& key
) const { return (findKey(key
) >= 0); }
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
); }
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
);
91 bool push_back(const char *val_
) {
95 bool push_backV(const std::vector
<UniValue
>& vec
);
97 bool pushKV(const std::string
& key
, const UniValue
& val
);
98 bool pushKV(const std::string
& key
, const std::string
& val_
) {
99 UniValue
tmpVal(VSTR
, val_
);
100 return pushKV(key
, tmpVal
);
102 bool pushKV(const std::string
& key
, const char *val_
) {
103 std::string
_val(val_
);
104 return pushKV(key
, _val
);
106 bool pushKV(const std::string
& key
, int64_t val_
) {
107 UniValue
tmpVal(val_
);
108 return pushKV(key
, tmpVal
);
110 bool pushKV(const std::string
& key
, uint64_t val_
) {
111 UniValue
tmpVal(val_
);
112 return pushKV(key
, tmpVal
);
114 bool pushKV(const std::string
& key
, int val_
) {
115 UniValue
tmpVal((int64_t)val_
);
116 return pushKV(key
, tmpVal
);
118 bool pushKV(const std::string
& key
, double val_
) {
119 UniValue
tmpVal(val_
);
120 return pushKV(key
, tmpVal
);
122 bool pushKVs(const UniValue
& obj
);
124 std::string
write(unsigned int prettyIndent
= 0,
125 unsigned int indentLevel
= 0) const;
127 bool read(const char *raw
);
128 bool read(const std::string
& rawStr
) {
129 return read(rawStr
.c_str());
134 std::string val
; // numbers are stored as C++ strings
135 std::vector
<std::string
> keys
;
136 std::vector
<UniValue
> values
;
138 int findKey(const std::string
& key
) const;
139 void writeArray(unsigned int prettyIndent
, unsigned int indentLevel
, std::string
& s
) const;
140 void writeObject(unsigned int prettyIndent
, unsigned int indentLevel
, std::string
& s
) const;
143 // Strict type-specific getters, these throw std::runtime_error if the
144 // value is of unexpected type
145 const std::vector
<std::string
>& getKeys() const;
146 const std::vector
<UniValue
>& getValues() const;
147 bool get_bool() const;
148 const std::string
& get_str() const;
150 int64_t get_int64() const;
151 double get_real() const;
152 const UniValue
& get_obj() const;
153 const UniValue
& get_array() const;
155 enum VType
type() const { return getType(); }
156 bool push_back(std::pair
<std::string
,UniValue
> pear
) {
157 return pushKV(pear
.first
, pear
.second
);
159 friend const UniValue
& find_value( const UniValue
& obj
, const std::string
& name
);
163 // The following were added for compatibility with json_spirit.
164 // Most duplicate other methods, and should be removed.
166 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, const char *cVal
)
168 std::string
key(cKey
);
170 return std::make_pair(key
, uVal
);
173 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, std::string strVal
)
175 std::string
key(cKey
);
176 UniValue
uVal(strVal
);
177 return std::make_pair(key
, uVal
);
180 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, uint64_t u64Val
)
182 std::string
key(cKey
);
183 UniValue
uVal(u64Val
);
184 return std::make_pair(key
, uVal
);
187 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, int64_t i64Val
)
189 std::string
key(cKey
);
190 UniValue
uVal(i64Val
);
191 return std::make_pair(key
, uVal
);
194 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, bool iVal
)
196 std::string
key(cKey
);
198 return std::make_pair(key
, uVal
);
201 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, int iVal
)
203 std::string
key(cKey
);
205 return std::make_pair(key
, uVal
);
208 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, double dVal
)
210 std::string
key(cKey
);
212 return std::make_pair(key
, uVal
);
215 static inline std::pair
<std::string
,UniValue
> Pair(const char *cKey
, const UniValue
& uVal
)
217 std::string
key(cKey
);
218 return std::make_pair(key
, uVal
);
221 static inline std::pair
<std::string
,UniValue
> Pair(std::string key
, const UniValue
& uVal
)
223 return std::make_pair(key
, uVal
);
228 JTOK_NONE
= 0, // eof
242 extern enum jtokentype
getJsonToken(std::string
& tokenVal
,
243 unsigned int& consumed
, const char *raw
);
244 extern const char *uvTypeName(UniValue::VType t
);
246 static inline bool jsonTokenIsValue(enum jtokentype jtt
)
263 static inline bool json_isspace(int ch
)
279 extern const UniValue NullUniValue
;
281 const UniValue
& find_value( const UniValue
& obj
, const std::string
& name
);
283 #endif // __UNIVALUE_H__