1 // Copyright 2014 BitPay Inc.
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "univalue_escapes.h"
13 static string
json_escape(const string
& inS
)
16 outS
.reserve(inS
.size() * 2);
18 for (unsigned int i
= 0; i
< inS
.size(); i
++) {
19 unsigned char ch
= inS
[i
];
20 const char *escStr
= escapes
[ch
];
31 string
UniValue::write(unsigned int prettyIndent
,
32 unsigned int indentLevel
) const
37 unsigned int modIndent
= indentLevel
;
46 writeObject(prettyIndent
, modIndent
, s
);
49 writeArray(prettyIndent
, modIndent
, s
);
52 s
+= "\"" + json_escape(val
) + "\"";
58 s
+= (val
== "1" ? "true" : "false");
65 static void indentStr(unsigned int prettyIndent
, unsigned int indentLevel
, string
& s
)
67 s
.append(prettyIndent
* indentLevel
, ' ');
70 void UniValue::writeArray(unsigned int prettyIndent
, unsigned int indentLevel
, string
& s
) const
76 for (unsigned int i
= 0; i
< values
.size(); i
++) {
78 indentStr(prettyIndent
, indentLevel
, s
);
79 s
+= values
[i
].write(prettyIndent
, indentLevel
+ 1);
80 if (i
!= (values
.size() - 1)) {
90 indentStr(prettyIndent
, indentLevel
- 1, s
);
94 void UniValue::writeObject(unsigned int prettyIndent
, unsigned int indentLevel
, string
& s
) const
100 for (unsigned int i
= 0; i
< keys
.size(); i
++) {
102 indentStr(prettyIndent
, indentLevel
, s
);
103 s
+= "\"" + json_escape(keys
[i
]) + "\":";
106 s
+= values
.at(i
).write(prettyIndent
, indentLevel
+ 1);
107 if (i
!= (values
.size() - 1))
114 indentStr(prettyIndent
, indentLevel
- 1, s
);