Inside Bitcoin
Opening the selected file…
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 <cstring>9#include <locale>10#include <optional>11#include <sstream>12#include <stdexcept>13#include <string>14#include <vector>16namespace17{18static bool ParsePrechecks(const std::string& str)19{20if (str.empty()) // No empty string allowed21return false;22if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed23return false;24if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed25return false;26return true;27}29std::optional<double> ParseDouble(const std::string& str)30{31if (!ParsePrechecks(str))32return std::nullopt;33if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed34return std::nullopt;35std::istringstream text(str);36text.imbue(std::locale::classic());37double result;38text >> result;39if (!text.eof() || text.fail()) {40return std::nullopt;41}42return result;43}44}46const std::vector<std::string>& UniValue::getKeys() const47{48checkType(VOBJ);49return keys;50}52const std::vector<UniValue>& UniValue::getValues() const53{54if (typ != VOBJ && typ != VARR)55throw std::runtime_error("JSON value is not an object or array as expected");56return values;57}59bool UniValue::get_bool() const60{61checkType(VBOOL);62return isTrue();63}65const std::string& UniValue::get_str() const66{67checkType(VSTR);68return getValStr();69}71double UniValue::get_real() const72{73checkType(VNUM);74if (const auto retval{ParseDouble(getValStr())}) {75return *retval;76}77throw std::runtime_error("JSON double out of range");78}80const UniValue& UniValue::get_obj() const81{82checkType(VOBJ);83return *this;84}86const UniValue& UniValue::get_array() const87{88checkType(VARR);89return *this;90}
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.