Branch data Line data Source code
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 <memory>
9 : : #include <string>
10 : : #include <vector>
11 : :
12 : 21018 : static std::string json_escape(const std::string& inS)
13 : : {
14 [ - + ]: 21018 : std::string outS;
15 [ - + + - ]: 21018 : outS.reserve(inS.size() * 2);
16 : :
17 [ - + + + ]: 880143 : for (unsigned int i = 0; i < inS.size(); i++) {
18 [ + + ]: 859125 : unsigned char ch = static_cast<unsigned char>(inS[i]);
19 : 859125 : const char *escStr = escapes[ch];
20 : :
21 [ + + ]: 859125 : if (escStr)
22 [ + - ]: 73 : outS += escStr;
23 : : else
24 [ + - ]: 1718177 : outS += static_cast<char>(ch);
25 : : }
26 : :
27 : 21018 : return outS;
28 : 0 : }
29 : :
30 : : // NOLINTNEXTLINE(misc-no-recursion)
31 : 27811 : std::string UniValue::write(unsigned int prettyIndent,
32 : : unsigned int indentLevel) const
33 : : {
34 [ + - ]: 27811 : std::string s;
35 [ + - ]: 27811 : s.reserve(1024);
36 : :
37 : 27811 : unsigned int modIndent = indentLevel;
38 [ + + ]: 27811 : if (modIndent == 0)
39 : 8635 : modIndent = 1;
40 : :
41 [ + + + + : 27811 : switch (typ) {
+ + - ]
42 : 15 : case VNULL:
43 [ + - ]: 15 : s += "null";
44 : : break;
45 : 442 : case VOBJ:
46 [ + - ]: 442 : writeObject(prettyIndent, modIndent, s);
47 : : break;
48 : 4870 : case VARR:
49 [ + - ]: 4870 : writeArray(prettyIndent, modIndent, s);
50 : : break;
51 : 19873 : case VSTR:
52 [ + - + - : 59619 : s += "\"" + json_escape(val) + "\"";
- + ]
53 : 19873 : break;
54 : 1900 : case VNUM:
55 [ - + ]: 1900 : s += val;
56 : : break;
57 : 711 : case VBOOL:
58 [ + + ]: 28522 : s += (val == "1" ? "true" : "false");
59 : : break;
60 : : }
61 : :
62 : 27811 : return s;
63 : 0 : }
64 : :
65 : 9105 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
66 : : {
67 : 9105 : s.append(prettyIndent * indentLevel, ' ');
68 : 9105 : }
69 : :
70 : : // NOLINTNEXTLINE(misc-no-recursion)
71 : 4870 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
72 : : {
73 : 4870 : s += "[";
74 [ + + ]: 4870 : if (prettyIndent)
75 : 1760 : s += "\n";
76 : :
77 [ - + + + ]: 22699 : for (unsigned int i = 0; i < values.size(); i++) {
78 [ + + ]: 17829 : if (prettyIndent)
79 : 6700 : indentStr(prettyIndent, indentLevel, s);
80 [ - + ]: 35658 : s += values[i].write(prettyIndent, indentLevel + 1);
81 [ - + + + ]: 17829 : if (i != (values.size() - 1)) {
82 : 13150 : s += ",";
83 : : }
84 [ + + ]: 17829 : if (prettyIndent)
85 : 6700 : s += "\n";
86 : : }
87 : :
88 [ + + ]: 4870 : if (prettyIndent)
89 : 1760 : indentStr(prettyIndent, indentLevel - 1, s);
90 : 4870 : s += "]";
91 : 4870 : }
92 : :
93 : : // NOLINTNEXTLINE(misc-no-recursion)
94 : 442 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
95 : : {
96 : 442 : s += "{";
97 [ + + ]: 442 : if (prettyIndent)
98 : 211 : s += "\n";
99 : :
100 [ - + + + ]: 1587 : for (unsigned int i = 0; i < keys.size(); i++) {
101 [ + + ]: 1145 : if (prettyIndent)
102 : 434 : indentStr(prettyIndent, indentLevel, s);
103 [ + - - + ]: 3435 : s += "\"" + json_escape(keys[i]) + "\":";
104 [ + + ]: 1145 : if (prettyIndent)
105 : 434 : s += " ";
106 [ - + ]: 2290 : s += values.at(i).write(prettyIndent, indentLevel + 1);
107 [ - + + + ]: 1145 : if (i != (values.size() - 1))
108 : 704 : s += ",";
109 [ + + ]: 1145 : if (prettyIndent)
110 : 434 : s += "\n";
111 : : }
112 : :
113 [ + + ]: 442 : if (prettyIndent)
114 : 211 : indentStr(prettyIndent, indentLevel - 1, s);
115 : 442 : s += "}";
116 : 442 : }
117 : :
|