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// Distributed under the MIT software license, see the accompanying3// file COPYING or https://opensource.org/licenses/mit-license.php.5#include <univalue.h>6#include <univalue_escapes.h>8#include <string>9#include <vector>11static std::string json_escape(const std::string& inS)12{13std::string outS;14outS.reserve(inS.size() * 2);16for (unsigned int i = 0; i < inS.size(); i++) {17unsigned char ch = static_cast<unsigned char>(inS[i]);18const char *escStr = escapes[ch];20if (escStr)21outS += escStr;22else23outS += static_cast<char>(ch);24}26return outS;27}29// NOLINTNEXTLINE(misc-no-recursion)30std::string UniValue::write(unsigned int prettyIndent,31unsigned int indentLevel) const32{33std::string s;34s.reserve(1024);36unsigned int modIndent = indentLevel;37if (modIndent == 0)38modIndent = 1;40switch (typ) {41case VNULL:42s += "null";43break;44case VOBJ:45writeObject(prettyIndent, modIndent, s);46break;47case VARR:48writeArray(prettyIndent, modIndent, s);49break;50case VSTR:51s += "\"" + json_escape(val) + "\"";52break;53case VNUM:54s += val;55break;56case VBOOL:57s += (val == "1" ? "true" : "false");58break;59}61return s;62}64static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)65{66s.append(prettyIndent * indentLevel, ' ');67}69// NOLINTNEXTLINE(misc-no-recursion)70void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const71{72s += "[";73if (prettyIndent)74s += "\n";76for (unsigned int i = 0; i < values.size(); i++) {77if (prettyIndent)78indentStr(prettyIndent, indentLevel, s);79s += values[i].write(prettyIndent, indentLevel + 1);80if (i != (values.size() - 1)) {81s += ",";82}83if (prettyIndent)84s += "\n";85}87if (prettyIndent)88indentStr(prettyIndent, indentLevel - 1, s);89s += "]";90}92// NOLINTNEXTLINE(misc-no-recursion)93void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const94{95s += "{";96if (prettyIndent)97s += "\n";99for (unsigned int i = 0; i < keys.size(); i++) {100if (prettyIndent)101indentStr(prettyIndent, indentLevel, s);102s += "\"" + json_escape(keys[i]) + "\":";103if (prettyIndent)104s += " ";105s += values.at(i).write(prettyIndent, indentLevel + 1);106if (i != (values.size() - 1))107s += ",";108if (prettyIndent)109s += "\n";110}112if (prettyIndent)113indentStr(prettyIndent, indentLevel - 1, s);114s += "}";115}
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.