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 : 5718513 : static std::string json_escape(const std::string& inS)
12 : : {
13 [ - + ]: 5718513 : std::string outS;
14 [ - + + - ]: 5718513 : outS.reserve(inS.size() * 2);
15 : :
16 [ - + + + ]: 105862058 : for (unsigned int i = 0; i < inS.size(); i++) {
17 [ + + ]: 100143545 : unsigned char ch = static_cast<unsigned char>(inS[i]);
18 : 100143545 : const char *escStr = escapes[ch];
19 : :
20 [ + + ]: 100143545 : if (escStr)
21 [ + - ]: 665581 : outS += escStr;
22 : : else
23 [ + - ]: 199621509 : outS += static_cast<char>(ch);
24 : : }
25 : :
26 : 5718513 : return outS;
27 : 0 : }
28 : :
29 : : // NOLINTNEXTLINE(misc-no-recursion)
30 : 6043629 : std::string UniValue::write(unsigned int prettyIndent,
31 : : unsigned int indentLevel) const
32 : : {
33 [ + - ]: 6043629 : std::string s;
34 [ + - ]: 6043629 : s.reserve(1024);
35 : :
36 : 6043629 : unsigned int modIndent = indentLevel;
37 [ + + ]: 6043629 : if (modIndent == 0)
38 : 21852 : modIndent = 1;
39 : :
40 [ + + + + : 6043629 : switch (typ) {
+ + - ]
41 : 13192 : case VNULL:
42 [ + - ]: 13192 : s += "null";
43 : : break;
44 : 1134076 : case VOBJ:
45 [ + - ]: 1134076 : writeObject(prettyIndent, modIndent, s);
46 : : break;
47 : 154700 : case VARR:
48 [ + - ]: 154700 : writeArray(prettyIndent, modIndent, s);
49 : : break;
50 : 1157319 : case VSTR:
51 [ + - + - : 3471957 : s += "\"" + json_escape(val) + "\"";
- + ]
52 : 1157319 : break;
53 : 3581520 : case VNUM:
54 [ - + ]: 3581520 : s += val;
55 : : break;
56 : 2822 : case VBOOL:
57 [ + + ]: 6046451 : s += (val == "1" ? "true" : "false");
58 : : break;
59 : : }
60 : :
61 : 6043629 : return s;
62 : 0 : }
63 : :
64 : 6681994 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
65 : : {
66 : 6681994 : s.append(prettyIndent * indentLevel, ' ');
67 : 6681994 : }
68 : :
69 : : // NOLINTNEXTLINE(misc-no-recursion)
70 : 154700 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
71 : : {
72 : 154700 : s += '[';
73 [ + + ]: 154700 : if (prettyIndent)
74 : 22499 : s += '\n';
75 : :
76 [ - + + + ]: 1592784 : for (unsigned int i = 0; i < values.size(); i++) {
77 [ + + ]: 1438084 : if (prettyIndent)
78 : 1096873 : indentStr(prettyIndent, indentLevel, s);
79 [ - + ]: 2876168 : s += values[i].write(prettyIndent, indentLevel + 1);
80 [ - + + + ]: 1438084 : if (i != (values.size() - 1)) {
81 : 1385941 : s += ',';
82 : : }
83 [ + + ]: 1438084 : if (prettyIndent)
84 : 1096873 : s += '\n';
85 : : }
86 : :
87 [ + + ]: 154700 : if (prettyIndent)
88 : 22499 : indentStr(prettyIndent, indentLevel - 1, s);
89 : 154700 : s += ']';
90 : 154700 : }
91 : :
92 : : // NOLINTNEXTLINE(misc-no-recursion)
93 : 1134076 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
94 : : {
95 : 1134076 : s += '{';
96 [ + + ]: 1134076 : if (prettyIndent)
97 : 1124542 : s += '\n';
98 : :
99 [ - + + + ]: 5695270 : for (unsigned int i = 0; i < keys.size(); i++) {
100 [ + + ]: 4561194 : if (prettyIndent)
101 : 4438080 : indentStr(prettyIndent, indentLevel, s);
102 [ + - - + ]: 13683582 : s += "\"" + json_escape(keys[i]) + "\":";
103 [ + + ]: 4561194 : if (prettyIndent)
104 : 4438080 : s += ' ';
105 [ - + ]: 9122388 : s += values.at(i).write(prettyIndent, indentLevel + 1);
106 [ - + + + ]: 4561194 : if (i != (values.size() - 1))
107 : 3433731 : s += ',';
108 [ + + ]: 4561194 : if (prettyIndent)
109 : 4438080 : s += '\n';
110 : : }
111 : :
112 [ + + ]: 1134076 : if (prettyIndent)
113 : 1124542 : indentStr(prettyIndent, indentLevel - 1, s);
114 : 1134076 : s += '}';
115 : 1134076 : }
116 : :
|