13struct UniValueStreamWriter {
16 UniValueStreamWriter() { str.reserve(1024); }
18 std::string getString() {
19#if __cplusplus >= 201103L
20 return std::move(str);
28 void put(
char c) { str.push_back(c); }
29 void put(
char c,
size_t nFill) { str.append(nFill, c); }
30 void write(
const char *s) { str.append(s); }
31 void write(
const std::string &s) { str.append(s); }
33 void indentStr(
unsigned int prettyIndent,
unsigned int indentLevel) {
34 put(
' ', prettyIndent * indentLevel);
37 void escapeJson(
const std::string &inS);
38 void writeAny(
unsigned int prettyIndent,
unsigned int indentLevel,
40 void writeArray(
unsigned int prettyIndent,
unsigned int indentLevel,
42 void writeObject(
unsigned int prettyIndent,
unsigned int indentLevel,
46void UniValueStreamWriter::escapeJson(
const std::string &inS) {
47 size_t len = inS.length();
48 for (
size_t i = 0; i < len; i++) {
49 const char ch = inS[i];
50 const char *
const escStr =
escapes[uint8_t(ch)];
60void UniValueStreamWriter::writeAny(
unsigned int prettyIndent,
61 unsigned int indentLevel,
63 unsigned int modIndent = indentLevel;
73 writeObject(prettyIndent, modIndent, obj);
76 writeArray(prettyIndent, modIndent, obj);
87 write(obj.
val ==
"1" ?
"true" :
"false");
92void UniValueStreamWriter::writeArray(
unsigned int prettyIndent,
93 unsigned int indentLevel,
100 const size_t nValues = obj.
values.size();
101 for (
size_t i = 0; i < nValues; ++i) {
103 indentStr(prettyIndent, indentLevel);
105 writeAny(prettyIndent, indentLevel + 1, obj.
values[i]);
106 if (i != nValues - 1) {
115 indentStr(prettyIndent, indentLevel - 1);
120void UniValueStreamWriter::writeObject(
unsigned int prettyIndent,
121 unsigned int indentLevel,
131 const size_t nItems = obj.
keys.size();
132 for (
size_t i = 0; i < nItems; ++i) {
134 indentStr(prettyIndent, indentLevel);
137 escapeJson(obj.
keys[i]);
143 writeAny(prettyIndent, indentLevel + 1, obj.
values[i]);
144 if (i != nItems - 1) {
153 indentStr(prettyIndent, indentLevel - 1);
160 unsigned int indentLevel)
const {
161 UniValueStreamWriter ss;
162 ss.writeAny(prettyIndent, indentLevel, *
this);
163 return ss.getString();
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::vector< UniValue > values
std::vector< std::string > keys
static const char * escapes[256]