Branch data Line data Source code
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 : : #include <univalue.h>
7 : :
8 : : #include <iomanip>
9 : : #include <map>
10 : : #include <memory>
11 : : #include <sstream>
12 : : #include <string>
13 : : #include <utility>
14 : : #include <vector>
15 : :
16 : : const UniValue NullUniValue;
17 : :
18 : 12366585 : void UniValue::clear()
19 : : {
20 : 12366585 : typ = VNULL;
21 : 12366585 : val.clear();
22 : 12366585 : keys.clear();
23 : 12366585 : values.clear();
24 : 12366585 : }
25 : :
26 : 0 : void UniValue::setNull()
27 : : {
28 : 0 : clear();
29 : 0 : }
30 : :
31 : 72421 : void UniValue::setBool(bool val_)
32 : : {
33 : 72421 : clear();
34 : 72421 : typ = VBOOL;
35 [ + + ]: 72421 : if (val_)
36 : 33315 : val = "1";
37 : 72421 : }
38 : :
39 : 4262567 : static bool validNumStr(const std::string& s)
40 : : {
41 [ + - ]: 4262567 : std::string tokenVal;
42 : 4262567 : unsigned int consumed;
43 [ + - ]: 4262567 : enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
44 : 4262567 : return (tt == JTOK_NUMBER);
45 : 4262567 : }
46 : :
47 : 4262567 : void UniValue::setNumStr(std::string str)
48 : : {
49 [ - + ]: 4262567 : if (!validNumStr(str)) {
50 [ # # # # ]: 0 : throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
51 : : }
52 : :
53 : 4262567 : clear();
54 : 4262567 : typ = VNUM;
55 : 4262567 : val = std::move(str);
56 : 4262567 : }
57 : :
58 : 26924 : void UniValue::setInt(uint64_t val_)
59 : : {
60 : 26924 : std::ostringstream oss;
61 : :
62 [ + - ]: 26924 : oss << val_;
63 : :
64 [ + - + - ]: 53848 : return setNumStr(oss.str());
65 : 26924 : }
66 : :
67 : 4235568 : void UniValue::setInt(int64_t val_)
68 : : {
69 : 4235568 : std::ostringstream oss;
70 : :
71 [ + - ]: 4235568 : oss << val_;
72 : :
73 [ + - + - ]: 8471136 : return setNumStr(oss.str());
74 : 4235568 : }
75 : :
76 : 75 : void UniValue::setFloat(double val_)
77 : : {
78 : 75 : std::ostringstream oss;
79 : :
80 [ + - ]: 75 : oss << std::setprecision(16) << val_;
81 : :
82 [ + - + - ]: 150 : return setNumStr(oss.str());
83 : 75 : }
84 : :
85 : 8008627 : void UniValue::setStr(std::string str)
86 : : {
87 : 8008627 : clear();
88 : 8008627 : typ = VSTR;
89 : 8008627 : val = std::move(str);
90 : 8008627 : }
91 : :
92 : 5601 : void UniValue::setArray()
93 : : {
94 : 5601 : clear();
95 : 5601 : typ = VARR;
96 : 5601 : }
97 : :
98 : 2577 : void UniValue::setObject()
99 : : {
100 : 2577 : clear();
101 : 2577 : typ = VOBJ;
102 : 2577 : }
103 : :
104 : 2931020 : void UniValue::push_back(UniValue val)
105 : : {
106 : 2931020 : checkType(VARR);
107 : :
108 : 2931020 : values.push_back(std::move(val));
109 : 2931020 : }
110 : :
111 : 0 : void UniValue::push_backV(const std::vector<UniValue>& vec)
112 : : {
113 : 0 : checkType(VARR);
114 : :
115 : 0 : values.insert(values.end(), vec.begin(), vec.end());
116 : 0 : }
117 : :
118 : 14595142 : void UniValue::pushKVEnd(std::string key, UniValue val)
119 : : {
120 : 14595142 : checkType(VOBJ);
121 : :
122 : 14595142 : keys.push_back(std::move(key));
123 : 14595142 : values.push_back(std::move(val));
124 : 14595142 : }
125 : :
126 : 14569775 : void UniValue::pushKV(std::string key, UniValue val)
127 : : {
128 : 14569775 : checkType(VOBJ);
129 : :
130 : 14569775 : size_t idx;
131 [ + + ]: 14569775 : if (findKey(key, idx))
132 : 481 : values[idx] = std::move(val);
133 : : else
134 [ + - ]: 29138588 : pushKVEnd(std::move(key), std::move(val));
135 : 14569775 : }
136 : :
137 : 13 : void UniValue::pushKVs(UniValue obj)
138 : : {
139 : 13 : checkType(VOBJ);
140 : 13 : obj.checkType(VOBJ);
141 : :
142 [ + + ]: 52 : for (size_t i = 0; i < obj.keys.size(); i++)
143 [ + - + - ]: 78 : pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
144 : 13 : }
145 : :
146 : 0 : void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
147 : : {
148 [ # # ]: 0 : if (typ != VOBJ)
149 : : return;
150 : :
151 : 0 : kv.clear();
152 [ # # ]: 0 : for (size_t i = 0; i < keys.size(); i++)
153 : 0 : kv[keys[i]] = values[i];
154 : : }
155 : :
156 : 14668427 : bool UniValue::findKey(const std::string& key, size_t& retIdx) const
157 : : {
158 [ + + ]: 35390563 : for (size_t i = 0; i < keys.size(); i++) {
159 [ + + ]: 20821006 : if (keys[i] == key) {
160 : 98870 : retIdx = i;
161 : 98870 : return true;
162 : : }
163 : : }
164 : :
165 : : return false;
166 : : }
167 : :
168 : 0 : bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
169 : : {
170 [ # # ]: 0 : if (typ != VOBJ) {
171 : : return false;
172 : : }
173 : :
174 [ # # ]: 0 : for (const auto& object: t) {
175 : 0 : size_t idx = 0;
176 [ # # ]: 0 : if (!findKey(object.first, idx)) {
177 : : return false;
178 : : }
179 : :
180 [ # # ]: 0 : if (values.at(idx).getType() != object.second) {
181 : : return false;
182 : : }
183 : : }
184 : :
185 : : return true;
186 : : }
187 : :
188 : 98514 : const UniValue& UniValue::operator[](const std::string& key) const
189 : : {
190 [ + + ]: 98514 : if (typ != VOBJ)
191 : : return NullUniValue;
192 : :
193 : 98506 : size_t index = 0;
194 [ + + ]: 98506 : if (!findKey(key, index))
195 : : return NullUniValue;
196 : :
197 : 98389 : return values.at(index);
198 : : }
199 : :
200 : 66923 : const UniValue& UniValue::operator[](size_t index) const
201 : : {
202 [ + - ]: 66923 : if (typ != VOBJ && typ != VARR)
203 : : return NullUniValue;
204 [ + + ]: 66923 : if (index >= values.size())
205 : : return NullUniValue;
206 : :
207 : 58585 : return values.at(index);
208 : : }
209 : :
210 : 32334250 : void UniValue::checkType(const VType& expected) const
211 : : {
212 [ + + ]: 32334250 : if (typ != expected) {
213 [ + - + - : 18150 : throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
+ - ]
214 [ + - + - : 36300 : std::string{uvTypeName(expected)}};
+ - ]
215 : : }
216 : 32325175 : }
217 : :
218 : 18336 : const char *uvTypeName(UniValue::VType t)
219 : : {
220 [ + + + + : 18336 : switch (t) {
+ - + ]
221 : : case UniValue::VNULL: return "null";
222 : 51 : case UniValue::VBOOL: return "bool";
223 : 247 : case UniValue::VOBJ: return "object";
224 : 256 : case UniValue::VARR: return "array";
225 : 8889 : case UniValue::VSTR: return "string";
226 : 3095 : case UniValue::VNUM: return "number";
227 : : }
228 : :
229 : : // not reached
230 : 0 : return nullptr;
231 : : }
232 : :
233 : 15719 : const UniValue& UniValue::find_value(std::string_view key) const
234 : : {
235 [ + + ]: 190052 : for (unsigned int i = 0; i < keys.size(); ++i) {
236 [ + + ]: 178521 : if (keys[i] == key) {
237 : 4188 : return values.at(i);
238 : : }
239 : : }
240 : : return NullUniValue;
241 : : }
242 : :
|