Coverage Report

Created: 2026-06-01 18:35

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