13 struct UniValueStreamWriter {
16 UniValueStreamWriter() {
20 std::string getString() {
21 #if __cplusplus >= 201103L
22 return std::move(str);
33 void put(
char c,
size_t nFill) {
36 void write(
const char *s) {
39 void write(
const std::string &s) {
43 void indentStr(
unsigned int prettyIndent,
unsigned int indentLevel) {
44 put(
' ', prettyIndent * indentLevel);
47 void escapeJson(
const std::string &inS);
48 void writeAny(
unsigned int prettyIndent,
unsigned int indentLevel,
const UniValue &obj);
49 void writeArray(
unsigned int prettyIndent,
unsigned int indentLevel,
const UniValue &obj);
50 void writeObject(
unsigned int prettyIndent,
unsigned int indentLevel,
const UniValue &obj);
53 void UniValueStreamWriter::escapeJson(
const std::string &inS) {
54 size_t len = inS.length();
55 for (
size_t i = 0; i < len; i++) {
56 const char ch = inS[i];
57 const char *
const escStr =
escapes[uint8_t(ch)];
67 void UniValueStreamWriter::writeAny(
unsigned int prettyIndent,
unsigned int indentLevel,
const UniValue &obj) {
68 unsigned int modIndent = indentLevel;
78 writeObject(prettyIndent, modIndent, obj);
81 writeArray(prettyIndent, modIndent, obj);
92 write(obj.
val ==
"1" ?
"true" :
"false");
97 void UniValueStreamWriter::writeArray(
unsigned int prettyIndent,
unsigned int indentLevel,
const UniValue &obj) {
103 const size_t nValues = obj.
values.size();
104 for (
size_t i = 0; i < nValues; ++i) {
106 indentStr(prettyIndent, indentLevel);
108 writeAny(prettyIndent, indentLevel + 1, obj.
values[i]);
109 if (i != nValues - 1) {
118 indentStr(prettyIndent, indentLevel - 1);
123 void UniValueStreamWriter::writeObject(
unsigned int prettyIndent,
unsigned int indentLevel,
const UniValue &obj) {
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);
159 std::string
UniValue::write(
unsigned int prettyIndent,
unsigned int indentLevel)
const {
160 UniValueStreamWriter ss;
161 ss.writeAny(prettyIndent, indentLevel, *
this);
162 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 std::string escapes[256]