Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/univalue/lib/univalue.cpp
Line
Count
Source
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 <sstream>
11
#include <string>
12
#include <utility>
13
#include <vector>
14
15
const UniValue NullUniValue;
16
17
void UniValue::clear()
18
10.9M
{
19
10.9M
    typ = VNULL;
20
10.9M
    val.clear();
21
10.9M
    keys.clear();
22
10.9M
    values.clear();
23
10.9M
}
24
25
void UniValue::setNull()
26
178k
{
27
178k
    clear();
28
178k
}
29
30
void UniValue::setBool(bool val_)
31
6.12M
{
32
6.12M
    clear();
33
6.12M
    typ = VBOOL;
34
6.12M
    if (val_)
35
5.71M
        val = "1";
36
6.12M
}
37
38
static bool validNumStr(const std::string& s)
39
1.64M
{
40
1.64M
    std::string tokenVal;
41
1.64M
    unsigned int consumed;
42
1.64M
    enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
43
1.64M
    return (tt == JTOK_NUMBER);
44
1.64M
}
45
46
void UniValue::setNumStr(std::string str)
47
1.64M
{
48
1.64M
    if (!validNumStr(str)) {
49
1
        throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
50
1
    }
51
52
1.64M
    clear();
53
1.64M
    typ = VNUM;
54
1.64M
    val = std::move(str);
55
1.64M
}
56
57
void UniValue::setInt(uint64_t val_)
58
904k
{
59
904k
    std::ostringstream oss;
60
61
904k
    oss << val_;
62
63
904k
    return setNumStr(oss.str());
64
904k
}
65
66
void UniValue::setInt(int64_t val_)
67
679k
{
68
679k
    std::ostringstream oss;
69
70
679k
    oss << val_;
71
72
679k
    return setNumStr(oss.str());
73
679k
}
74
75
void UniValue::setFloat(double val_)
76
63.5k
{
77
63.5k
    std::ostringstream oss;
78
79
63.5k
    oss << std::setprecision(16) << val_;
80
81
63.5k
    return setNumStr(oss.str());
82
63.5k
}
83
84
void UniValue::setStr(std::string str)
85
2.68M
{
86
2.68M
    clear();
87
2.68M
    typ = VSTR;
88
2.68M
    val = std::move(str);
89
2.68M
}
90
91
void UniValue::setArray()
92
758
{
93
758
    clear();
94
758
    typ = VARR;
95
758
}
96
97
void UniValue::setObject()
98
172k
{
99
172k
    clear();
100
172k
    typ = VOBJ;
101
172k
}
102
103
void UniValue::push_back(UniValue val)
104
1.14M
{
105
1.14M
    checkType(VARR);
106
107
1.14M
    values.push_back(std::move(val));
108
1.14M
}
109
110
void UniValue::push_backV(const std::vector<UniValue>& vec)
111
2
{
112
2
    checkType(VARR);
113
114
2
    values.insert(values.end(), vec.begin(), vec.end());
115
2
}
116
117
void UniValue::pushKVEnd(std::string key, UniValue val)
118
4.39M
{
119
4.39M
    checkType(VOBJ);
120
121
4.39M
    keys.push_back(std::move(key));
122
4.39M
    values.push_back(std::move(val));
123
4.39M
}
124
125
void UniValue::pushKV(std::string key, UniValue val)
126
4.37M
{
127
4.37M
    checkType(VOBJ);
128
129
4.37M
    size_t idx;
130
4.37M
    if (findKey(key, idx))
131
2
        values[idx] = std::move(val);
132
4.37M
    else
133
4.37M
        pushKVEnd(std::move(key), std::move(val));
134
4.37M
}
135
136
void UniValue::pushKVs(UniValue obj)
137
10.3k
{
138
10.3k
    checkType(VOBJ);
139
10.3k
    obj.checkType(VOBJ);
140
141
26.2k
    for (size_t i = 0; i < obj.keys.size(); i++)
142
15.8k
        pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
143
10.3k
}
144
145
void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
146
414k
{
147
414k
    if (typ != VOBJ)
148
0
        return;
149
150
414k
    kv.clear();
151
3.88M
    for (size_t i = 0; i < keys.size(); i++)
152
3.47M
        kv[keys[i]] = values[i];
153
414k
}
154
155
bool UniValue::findKey(const std::string& key, size_t& retIdx) const
156
4.64M
{
157
37.7M
    for (size_t i = 0; i < keys.size(); i++) {
158
33.3M
        if (keys[i] == key) {
159
238k
            retIdx = i;
160
238k
            return true;
161
238k
        }
162
33.3M
    }
163
164
4.40M
    return false;
165
4.64M
}
166
167
bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
168
11
{
169
11
    if (typ != VOBJ) {
170
0
        return false;
171
0
    }
172
173
42
    for (const auto& object: t) {
174
42
        size_t idx = 0;
175
42
        if (!findKey(object.first, idx)) {
176
0
            return false;
177
0
        }
178
179
42
        if (values.at(idx).getType() != object.second) {
180
2
            return false;
181
2
        }
182
42
    }
183
184
9
    return true;
185
11
}
186
187
const UniValue& UniValue::operator[](const std::string& key) const
188
50.4k
{
189
50.4k
    if (typ != VOBJ)
190
39
        return NullUniValue;
191
192
50.4k
    size_t index = 0;
193
50.4k
    if (!findKey(key, index))
194
3.37k
        return NullUniValue;
195
196
47.0k
    return values.at(index);
197
50.4k
}
198
199
const UniValue& UniValue::operator[](size_t index) const
200
2.23M
{
201
2.23M
    if (typ != VOBJ && typ != VARR)
202
0
        return NullUniValue;
203
2.23M
    if (index >= values.size())
204
279k
        return NullUniValue;
205
206
1.95M
    return values.at(index);
207
2.23M
}
208
209
void UniValue::checkType(const VType& expected) const
210
14.4M
{
211
14.4M
    if (typ != expected) {
212
45
        throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
213
45
                                 std::string{uvTypeName(expected)}};
214
45
    }
215
14.4M
}
216
217
const char *uvTypeName(UniValue::VType t)
218
28.1k
{
219
28.1k
    switch (t) {
220
5.19k
    case UniValue::VNULL: return "null";
221
17
    case UniValue::VBOOL: return "bool";
222
10.9k
    case UniValue::VOBJ: return "object";
223
1.21k
    case UniValue::VARR: return "array";
224
10.7k
    case UniValue::VSTR: return "string";
225
75
    case UniValue::VNUM: return "number";
226
28.1k
    }
227
228
    // not reached
229
0
    return nullptr;
230
28.1k
}
231
232
const UniValue& UniValue::find_value(std::string_view key) const
233
784k
{
234
1.99M
    for (unsigned int i = 0; i < keys.size(); ++i) {
235
1.96M
        if (keys[i] == key) {
236
759k
            return values.at(i);
237
759k
        }
238
1.96M
    }
239
25.6k
    return NullUniValue;
240
784k
}
241
242
void UniValue::reserve(size_t new_cap)
243
28.9k
{
244
28.9k
    values.reserve(new_cap);
245
28.9k
    if (typ == VOBJ) {
246
0
        keys.reserve(new_cap);
247
0
    }
248
28.9k
}