Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright 2014 BitPay Inc.2// Copyright (c) 2015-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or https://opensource.org/licenses/mit-license.php.6#include <univalue.h>8#include <iomanip>9#include <map>10#include <sstream>11#include <string>12#include <utility>13#include <vector>15const UniValue NullUniValue;17void UniValue::clear()18{19typ = VNULL;20val.clear();21keys.clear();22values.clear();23}25void UniValue::setNull()26{27clear();28}30void UniValue::setBool(bool val_)31{32clear();33typ = VBOOL;34if (val_)35val = "1";36}38static bool validNumStr(const std::string& s)39{40std::string tokenVal;41unsigned int consumed;42enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());43return (tt == JTOK_NUMBER);44}46void UniValue::setNumStr(std::string str)47{48if (!validNumStr(str)) {49throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};50}52clear();53typ = VNUM;54val = std::move(str);55}57void UniValue::setInt(uint64_t val_)58{59std::ostringstream oss;61oss << val_;63return setNumStr(oss.str());64}66void UniValue::setInt(int64_t val_)67{68std::ostringstream oss;70oss << val_;72return setNumStr(oss.str());73}75void UniValue::setFloat(double val_)76{77std::ostringstream oss;79oss << std::setprecision(16) << val_;81return setNumStr(oss.str());82}84void UniValue::setStr(std::string str)85{86clear();87typ = VSTR;88val = std::move(str);89}91void UniValue::setArray()92{93clear();94typ = VARR;95}97void UniValue::setObject()98{99clear();100typ = VOBJ;101}103void UniValue::push_back(UniValue val)104{105checkType(VARR);107values.push_back(std::move(val));108}110void UniValue::push_backV(const std::vector<UniValue>& vec)111{112checkType(VARR);114values.insert(values.end(), vec.begin(), vec.end());115}117void UniValue::pushKVEnd(std::string key, UniValue val)118{119checkType(VOBJ);121keys.push_back(std::move(key));122values.push_back(std::move(val));123}125void UniValue::pushKV(std::string key, UniValue val)126{127checkType(VOBJ);129size_t idx;130if (findKey(key, idx))131values[idx] = std::move(val);132else133pushKVEnd(std::move(key), std::move(val));134}136void UniValue::pushKVs(UniValue obj)137{138checkType(VOBJ);139obj.checkType(VOBJ);141for (size_t i = 0; i < obj.keys.size(); i++)142pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));143}145void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const146{147if (typ != VOBJ)148return;150kv.clear();151for (size_t i = 0; i < keys.size(); i++)152kv[keys[i]] = values[i];153}155bool UniValue::findKey(const std::string& key, size_t& retIdx) const156{157for (size_t i = 0; i < keys.size(); i++) {158if (keys[i] == key) {159retIdx = i;160return true;161}162}164return false;165}167bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const168{169if (typ != VOBJ) {170return false;171}173for (const auto& object: t) {174size_t idx = 0;175if (!findKey(object.first, idx)) {176return false;177}179if (values.at(idx).getType() != object.second) {180return false;181}182}184return true;185}187const UniValue& UniValue::operator[](const std::string& key) const188{189if (typ != VOBJ)190return NullUniValue;192size_t index = 0;193if (!findKey(key, index))194return NullUniValue;196return values.at(index);197}199const UniValue& UniValue::operator[](size_t index) const200{201if (typ != VOBJ && typ != VARR)202return NullUniValue;203if (index >= values.size())204return NullUniValue;206return values.at(index);207}209void UniValue::checkType(const VType& expected) const210{211if (typ != expected) {212throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +213std::string{uvTypeName(expected)}};214}215}217const char *uvTypeName(UniValue::VType t)218{219switch (t) {220case UniValue::VNULL: return "null";221case UniValue::VBOOL: return "bool";222case UniValue::VOBJ: return "object";223case UniValue::VARR: return "array";224case UniValue::VSTR: return "string";225case UniValue::VNUM: return "number";226}228// not reached229return nullptr;230}232const UniValue& UniValue::find_value(std::string_view key) const233{234for (unsigned int i = 0; i < keys.size(); ++i) {235if (keys[i] == key) {236return values.at(i);237}238}239return NullUniValue;240}242void UniValue::reserve(size_t new_cap)243{244values.reserve(new_cap);245if (typ == VOBJ) {246keys.reserve(new_cap);247}248}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.