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 <string>
9 : : #include <vector>
10 : :
11 : 21087 : static std::string json_escape(const std::string& inS)
12 : : {
13 [ - + ]: 21087 : std::string outS;
14 [ - + + - ]: 21087 : outS.reserve(inS.size() * 2);
15 : :
16 [ - + + + ]: 882539 : for (unsigned int i = 0; i < inS.size(); i++) {
17 [ + + ]: 861452 : unsigned char ch = static_cast<unsigned char>(inS[i]);
18 : 861452 : const char *escStr = escapes[ch];
19 : :
20 [ + + ]: 861452 : if (escStr)
21 [ + - ]: 73 : outS += escStr;
22 : : else
23 [ + - ]: 1722831 : outS += static_cast<char>(ch);
24 : : }
25 : :
26 : 21087 : return outS;
27 : 0 : }
28 : :
29 : : // NOLINTNEXTLINE(misc-no-recursion)
30 : 27904 : std::string UniValue::write(unsigned int prettyIndent,
31 : : unsigned int indentLevel) const
32 : : {
33 [ + - ]: 27904 : std::string s;
34 [ + - ]: 27904 : s.reserve(1024);
35 : :
36 : 27904 : unsigned int modIndent = indentLevel;
37 [ + + ]: 27904 : if (modIndent == 0)
38 : 8648 : modIndent = 1;
39 : :
40 [ + + + + : 27904 : switch (typ) {
+ + - ]
41 : 15 : case VNULL:
42 [ + - ]: 15 : s += "null";
43 : : break;
44 : 446 : case VOBJ:
45 [ + - ]: 446 : writeObject(prettyIndent, modIndent, s);
46 : : break;
47 : 4893 : case VARR:
48 [ + - ]: 4893 : writeArray(prettyIndent, modIndent, s);
49 : : break;
50 : 19934 : case VSTR:
51 [ + - + - : 59802 : s += "\"" + json_escape(val) + "\"";
- + ]
52 : 19934 : break;
53 : 1905 : case VNUM:
54 [ - + ]: 1905 : s += val;
55 : : break;
56 : 711 : case VBOOL:
57 [ + + ]: 28615 : s += (val == "1" ? "true" : "false");
58 : : break;
59 : : }
60 : :
61 : 27904 : return s;
62 : 0 : }
63 : :
64 : 9154 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
65 : : {
66 : 9154 : s.append(prettyIndent * indentLevel, ' ');
67 : 9154 : }
68 : :
69 : : // NOLINTNEXTLINE(misc-no-recursion)
70 : 4893 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
71 : : {
72 : 4893 : s += "[";
73 [ + + ]: 4893 : if (prettyIndent)
74 : 1771 : s += "\n";
75 : :
76 [ - + + + ]: 22790 : for (unsigned int i = 0; i < values.size(); i++) {
77 [ + + ]: 17897 : if (prettyIndent)
78 : 6726 : indentStr(prettyIndent, indentLevel, s);
79 [ - + ]: 35794 : s += values[i].write(prettyIndent, indentLevel + 1);
80 [ - + + + ]: 17897 : if (i != (values.size() - 1)) {
81 : 13201 : s += ",";
82 : : }
83 [ + + ]: 17897 : if (prettyIndent)
84 : 6726 : s += "\n";
85 : : }
86 : :
87 [ + + ]: 4893 : if (prettyIndent)
88 : 1771 : indentStr(prettyIndent, indentLevel - 1, s);
89 : 4893 : s += "]";
90 : 4893 : }
91 : :
92 : : // NOLINTNEXTLINE(misc-no-recursion)
93 : 446 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
94 : : {
95 : 446 : s += "{";
96 [ + + ]: 446 : if (prettyIndent)
97 : 215 : s += "\n";
98 : :
99 [ - + + + ]: 1599 : for (unsigned int i = 0; i < keys.size(); i++) {
100 [ + + ]: 1153 : if (prettyIndent)
101 : 442 : indentStr(prettyIndent, indentLevel, s);
102 [ + - - + ]: 3459 : s += "\"" + json_escape(keys[i]) + "\":";
103 [ + + ]: 1153 : if (prettyIndent)
104 : 442 : s += " ";
105 [ - + ]: 2306 : s += values.at(i).write(prettyIndent, indentLevel + 1);
106 [ - + + + ]: 1153 : if (i != (values.size() - 1))
107 : 708 : s += ",";
108 [ + + ]: 1153 : if (prettyIndent)
109 : 442 : s += "\n";
110 : : }
111 : :
112 [ + + ]: 446 : if (prettyIndent)
113 : 215 : indentStr(prettyIndent, indentLevel - 1, s);
114 : 446 : s += "}";
115 : 446 : }
116 : :
|