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 17892674 : static std::string json_escape(const std::string& inS) 13 : { 14 17892674 : std::string outS; 15 17892674 : outS.reserve(inS.size() * 2); 16 : 17 413431060 : for (unsigned int i = 0; i < inS.size(); i++) { 18 395538386 : unsigned char ch = static_cast<unsigned char>(inS[i]); 19 395538386 : const char *escStr = escapes[ch]; 20 : 21 395538386 : if (escStr) 22 344231 : outS += escStr; 23 : else 24 395194155 : outS += static_cast<char>(ch); 25 395538386 : } 26 : 27 17892674 : return outS; 28 17892674 : } 29 : 30 15943782 : std::string UniValue::write(unsigned int prettyIndent, 31 : unsigned int indentLevel) const 32 : { 33 15943782 : std::string s; 34 15943782 : s.reserve(1024); 35 : 36 15943782 : unsigned int modIndent = indentLevel; 37 15943782 : if (modIndent == 0) 38 646335 : modIndent = 1; 39 : 40 15943782 : switch (typ) { 41 : case VNULL: 42 611964 : s += "null"; 43 611964 : break; 44 : case VOBJ: 45 1972948 : writeObject(prettyIndent, modIndent, s); 46 1972948 : break; 47 : case VARR: 48 528601 : writeArray(prettyIndent, modIndent, s); 49 528601 : break; 50 : case VSTR: 51 4103915 : s += "\"" + json_escape(val) + "\""; 52 4103915 : break; 53 : case VNUM: 54 6906056 : s += val; 55 6906056 : break; 56 : case VBOOL: 57 1820298 : s += (val == "1" ? "true" : "false"); 58 1820298 : break; 59 : } 60 : 61 15943782 : return s; 62 15943782 : } 63 : 64 31940 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) 65 : { 66 31940 : s.append(prettyIndent * indentLevel, ' '); 67 31940 : } 68 : 69 528601 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const 70 : { 71 528601 : s += "["; 72 528601 : if (prettyIndent) 73 5081 : s += "\n"; 74 : 75 2031751 : for (unsigned int i = 0; i < values.size(); i++) { 76 1503150 : if (prettyIndent) 77 8912 : indentStr(prettyIndent, indentLevel, s); 78 1503150 : s += values[i].write(prettyIndent, indentLevel + 1); 79 1503150 : if (i != (values.size() - 1)) { 80 1222229 : s += ","; 81 1222229 : } 82 1503150 : if (prettyIndent) 83 8912 : s += "\n"; 84 1503150 : } 85 : 86 528601 : if (prettyIndent) 87 5081 : indentStr(prettyIndent, indentLevel - 1, s); 88 528601 : s += "]"; 89 528601 : } 90 : 91 1972948 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const 92 : { 93 1972948 : s += "{"; 94 1972948 : if (prettyIndent) 95 7200 : s += "\n"; 96 : 97 15761707 : for (unsigned int i = 0; i < keys.size(); i++) { 98 13788759 : if (prettyIndent) 99 10747 : indentStr(prettyIndent, indentLevel, s); 100 13788759 : s += "\"" + json_escape(keys[i]) + "\":"; 101 13788759 : if (prettyIndent) 102 10747 : s += " "; 103 13788759 : s += values.at(i).write(prettyIndent, indentLevel + 1); 104 13788759 : if (i != (values.size() - 1)) 105 11821160 : s += ","; 106 13788759 : if (prettyIndent) 107 10747 : s += "\n"; 108 13788759 : } 109 : 110 1972948 : if (prettyIndent) 111 7200 : indentStr(prettyIndent, indentLevel - 1, s); 112 1972948 : s += "}"; 113 1972948 : } 114 :