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