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#ifndef BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H7#define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H9#include <charconv>10#include <cstddef>11#include <cstdint>12#include <map>13#include <stdexcept>14#include <string>15#include <string_view>16#include <system_error>17#include <type_traits>18#include <utility>19#include <vector>21// NOLINTNEXTLINE(misc-no-recursion)22class UniValue {23public:24enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };26class type_error : public std::runtime_error27{28using std::runtime_error::runtime_error;29};31UniValue() { typ = VNULL; }32UniValue(UniValue::VType type, std::string str = {}) : typ{type}, val{std::move(str)} {}33template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,34std::enable_if_t<std::is_floating_point_v<T> || // setFloat35std::is_same_v<bool, T> || // setBool36std::is_signed_v<T> || std::is_unsigned_v<T> || // setInt37std::is_constructible_v<std::string, T>, // setStr38bool> = true>39UniValue(Ref&& val)40{41if constexpr (std::is_floating_point_v<T>) {42setFloat(val);43} else if constexpr (std::is_same_v<bool, T>) {44setBool(val);45} else if constexpr (std::is_signed_v<T>) {46setInt(int64_t{val});47} else if constexpr (std::is_unsigned_v<T>) {48setInt(uint64_t{val});49} else {50setStr(std::string{std::forward<Ref>(val)});51}52}54void clear();56void setNull();57void setBool(bool val);58void setNumStr(std::string str);59void setInt(uint64_t val);60void setInt(int64_t val);61void setInt(int val_) { return setInt(int64_t{val_}); }62void setFloat(double val);63void setStr(std::string str);64void setArray();65void setObject();67enum VType getType() const { return typ; }68const std::string& getValStr() const { return val; }69bool empty() const { return (values.size() == 0); }71size_t size() const { return values.size(); }73void reserve(size_t new_cap);75void getObjMap(std::map<std::string,UniValue>& kv) const;76bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;77const UniValue& operator[](const std::string& key) const;78const UniValue& operator[](size_t index) const;79bool exists(const std::string& key) const { size_t i; return findKey(key, i); }81bool isNull() const { return (typ == VNULL); }82bool isTrue() const { return (typ == VBOOL) && (val == "1"); }83bool isFalse() const { return (typ == VBOOL) && (val != "1"); }84bool isBool() const { return (typ == VBOOL); }85bool isStr() const { return (typ == VSTR); }86bool isNum() const { return (typ == VNUM); }87bool isArray() const { return (typ == VARR); }88bool isObject() const { return (typ == VOBJ); }90void push_back(UniValue val);91void push_backV(const std::vector<UniValue>& vec);92template <class It>93void push_backV(It first, It last);95void pushKVEnd(std::string key, UniValue val);96void pushKV(std::string key, UniValue val);97void pushKVs(UniValue obj);99std::string write(unsigned int prettyIndent = 0,100unsigned int indentLevel = 0) const;102/// Parse JSON; set value to null on failure.103[[nodiscard]] bool read(std::string_view json);105private:106UniValue::VType typ;107std::string val; // numbers are stored as C++ strings108std::vector<std::string> keys;109std::vector<UniValue> values;111void checkType(const VType& expected) const;112bool findKey(const std::string& key, size_t& retIdx) const;113void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;114void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;115/// Internal parser: May leave this value in an invalid state on failure.116[[nodiscard]] bool read_impl(std::string_view str_in);118public:119// Strict type-specific getters, these throw std::runtime_error if the120// value is of unexpected type121const std::vector<std::string>& getKeys() const;122const std::vector<UniValue>& getValues() const;123template <typename Int>124Int getInt() const;125bool get_bool() const;126const std::string& get_str() const;127double get_real() const;128const UniValue& get_obj() const;129const UniValue& get_array() const;131enum VType type() const { return getType(); }132const UniValue& find_value(std::string_view key) const;133};135template <class It>136void UniValue::push_backV(It first, It last)137{138checkType(VARR);139values.insert(values.end(), first, last);140}142template <typename Int>143Int UniValue::getInt() const144{145static_assert(std::is_integral_v<Int>);146checkType(VNUM);147Int result;148const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result);149if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) {150throw std::runtime_error("JSON integer out of range");151}152return result;153}155enum jtokentype {156JTOK_ERR = -1,157JTOK_NONE = 0, // eof158JTOK_OBJ_OPEN,159JTOK_OBJ_CLOSE,160JTOK_ARR_OPEN,161JTOK_ARR_CLOSE,162JTOK_COLON,163JTOK_COMMA,164JTOK_KW_NULL,165JTOK_KW_TRUE,166JTOK_KW_FALSE,167JTOK_NUMBER,168JTOK_STRING,169};171extern enum jtokentype getJsonToken(std::string& tokenVal,172unsigned int& consumed, const char *raw, const char *end);173extern const char *uvTypeName(UniValue::VType t);175static inline bool jsonTokenIsValue(enum jtokentype jtt)176{177switch (jtt) {178case JTOK_KW_NULL:179case JTOK_KW_TRUE:180case JTOK_KW_FALSE:181case JTOK_NUMBER:182case JTOK_STRING:183return true;185default:186return false;187}189// not reached190}192static inline bool json_isspace(int ch)193{194switch (ch) {195case 0x20:196case 0x09:197case 0x0a:198case 0x0d:199return true;201default:202return false;203}205// not reached206}208extern const UniValue NullUniValue;210#endif // BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
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.