mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-13 08:58:52 +00:00
a44caf65fe Merge bitcoin-core/univalue-subtree#28: Import fixes for sanitizer reported issues 135254331e Import fixes for sanitizer reported issues d5fb86940e refactor: use c++11 range based for loop in checkObject ff9c379304 refactor: Use nullptr (c++11) instead of NULL 08a99754d5 build: use ax_cxx_compile_stdcxx.m4 to check for C++11 support 66d3713ce7 Merge bitcoin-core/univalue#29: ci: travis -> cirrus 808d487292 ci: travis -> cirrus c390ac375f Merge bitcoin-core/univalue#19: Split sources for easier buildsystem integration 4a5b0a1c65 build: Move source entries out to sources.mk 6c7d94b33c build: cleanup wonky gen usage a222637c6d Merge #23: Merge changes from jgarzik/univalue@1ae6a23 f77d0f718d Merge commit '1ae6a231a0169938eb3972c1d48dd17cba5947e1' into HEAD 1ae6a231a0 Merge pull request #57 from MarcoFalke/test_fix 92bdd11f0b univalue_write: remove unneeded sstream.h include ffb621c130 Merge pull request #56 from drodil/remove_sstream_header f33acf9fe8 Merge commit '7890db9~' into HEAD 66e0adec4d Remove unnecessary sstream header from univalue.h 88967f6586 Version 1.0.4 1dc113dbef Merge pull request #50 from luke-jr/pushKV_bool 72392fb227 [tests] test pushKV for boolean values c23132bcf4 Pushing boolean value to univalue correctly 81faab26a1 Merge pull request #48 from fwolfst/47-UPDATE_MIT_LINK_TO_HTTPS b17634ef24 Update URLs to MIT license. 88ab64f6b5 Merge pull request #46 from jasonbcox/master 35ed96da31 Merge pull request #44 from MarcoFalke/Mf1709-univalue-cherrypick-explicit 420c226290 Merge pull request #45 from MarcoFalke/Mf1710-univalue-revert-test git-subtree-dir: src/univalue git-subtree-split: a44caf65fe55b9dd8ddb08f04c0f70409efd53b3
113 lines
2.7 KiB
C++
113 lines
2.7 KiB
C++
// Copyright 2014 BitPay Inc.
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or https://opensource.org/licenses/mit-license.php.
|
|
|
|
#include <iomanip>
|
|
#include <stdio.h>
|
|
#include "univalue.h"
|
|
#include "univalue_escapes.h"
|
|
|
|
static std::string json_escape(const std::string& inS)
|
|
{
|
|
std::string outS;
|
|
outS.reserve(inS.size() * 2);
|
|
|
|
for (unsigned int i = 0; i < inS.size(); i++) {
|
|
unsigned char ch = static_cast<unsigned char>(inS[i]);
|
|
const char *escStr = escapes[ch];
|
|
|
|
if (escStr)
|
|
outS += escStr;
|
|
else
|
|
outS += static_cast<char>(ch);
|
|
}
|
|
|
|
return outS;
|
|
}
|
|
|
|
std::string UniValue::write(unsigned int prettyIndent,
|
|
unsigned int indentLevel) const
|
|
{
|
|
std::string s;
|
|
s.reserve(1024);
|
|
|
|
unsigned int modIndent = indentLevel;
|
|
if (modIndent == 0)
|
|
modIndent = 1;
|
|
|
|
switch (typ) {
|
|
case VNULL:
|
|
s += "null";
|
|
break;
|
|
case VOBJ:
|
|
writeObject(prettyIndent, modIndent, s);
|
|
break;
|
|
case VARR:
|
|
writeArray(prettyIndent, modIndent, s);
|
|
break;
|
|
case VSTR:
|
|
s += "\"" + json_escape(val) + "\"";
|
|
break;
|
|
case VNUM:
|
|
s += val;
|
|
break;
|
|
case VBOOL:
|
|
s += (val == "1" ? "true" : "false");
|
|
break;
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
|
|
{
|
|
s.append(prettyIndent * indentLevel, ' ');
|
|
}
|
|
|
|
void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
|
|
{
|
|
s += "[";
|
|
if (prettyIndent)
|
|
s += "\n";
|
|
|
|
for (unsigned int i = 0; i < values.size(); i++) {
|
|
if (prettyIndent)
|
|
indentStr(prettyIndent, indentLevel, s);
|
|
s += values[i].write(prettyIndent, indentLevel + 1);
|
|
if (i != (values.size() - 1)) {
|
|
s += ",";
|
|
}
|
|
if (prettyIndent)
|
|
s += "\n";
|
|
}
|
|
|
|
if (prettyIndent)
|
|
indentStr(prettyIndent, indentLevel - 1, s);
|
|
s += "]";
|
|
}
|
|
|
|
void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
|
|
{
|
|
s += "{";
|
|
if (prettyIndent)
|
|
s += "\n";
|
|
|
|
for (unsigned int i = 0; i < keys.size(); i++) {
|
|
if (prettyIndent)
|
|
indentStr(prettyIndent, indentLevel, s);
|
|
s += "\"" + json_escape(keys[i]) + "\":";
|
|
if (prettyIndent)
|
|
s += " ";
|
|
s += values.at(i).write(prettyIndent, indentLevel + 1);
|
|
if (i != (values.size() - 1))
|
|
s += ",";
|
|
if (prettyIndent)
|
|
s += "\n";
|
|
}
|
|
|
|
if (prettyIndent)
|
|
indentStr(prettyIndent, indentLevel - 1, s);
|
|
s += "}";
|
|
}
|
|
|