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 21294 : static std::string json_escape(const std::string& inS) 13 : { 14 21294 : std::string outS; 15 21294 : outS.reserve(inS.size() * 2); 16 : 17 947373 : for (unsigned int i = 0; i < inS.size(); i++) { 18 926079 : unsigned char ch = static_cast<unsigned char>(inS[i]); 19 926079 : const char *escStr = escapes[ch]; 20 : 21 926079 : if (escStr) 22 61 : outS += escStr; 23 : else 24 926018 : outS += static_cast<char>(ch); 25 926079 : } 26 : 27 21294 : return outS; 28 21294 : } 29 : 30 27027 : std::string UniValue::write(unsigned int prettyIndent, 31 : unsigned int indentLevel) const 32 : { 33 27027 : std::string s; 34 27027 : s.reserve(1024); 35 : 36 27027 : unsigned int modIndent = indentLevel; 37 27027 : if (modIndent == 0) 38 8563 : modIndent = 1; 39 : 40 27027 : switch (typ) { 41 : case VNULL: 42 15 : s += "null"; 43 15 : break; 44 : case VOBJ: 45 599 : writeObject(prettyIndent, modIndent, s); 46 599 : break; 47 : case VARR: 48 4436 : writeArray(prettyIndent, modIndent, s); 49 4436 : break; 50 : case VSTR: 51 19571 : s += "\"" + json_escape(val) + "\""; 52 19571 : break; 53 : case VNUM: 54 1788 : s += val; 55 1788 : break; 56 : case VBOOL: 57 618 : s += (val == "1" ? "true" : "false"); 58 618 : break; 59 : } 60 : 61 27027 : return s; 62 27027 : } 63 : 64 9386 : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) 65 : { 66 9386 : s.append(prettyIndent * indentLevel, ' '); 67 9386 : } 68 : 69 4436 : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const 70 : { 71 4436 : s += "["; 72 4436 : if (prettyIndent) 73 1673 : s += "\n"; 74 : 75 20980 : for (unsigned int i = 0; i < values.size(); i++) { 76 16544 : if (prettyIndent) 77 6321 : indentStr(prettyIndent, indentLevel, s); 78 16544 : s += values[i].write(prettyIndent, indentLevel + 1); 79 16544 : if (i != (values.size() - 1)) { 80 12314 : s += ","; 81 12314 : } 82 16544 : if (prettyIndent) 83 6321 : s += "\n"; 84 16544 : } 85 : 86 4436 : if (prettyIndent) 87 1673 : indentStr(prettyIndent, indentLevel - 1, s); 88 4436 : s += "]"; 89 4436 : } 90 : 91 599 : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const 92 : { 93 599 : s += "{"; 94 599 : if (prettyIndent) 95 407 : s += "\n"; 96 : 97 2322 : for (unsigned int i = 0; i < keys.size(); i++) { 98 1723 : if (prettyIndent) 99 985 : indentStr(prettyIndent, indentLevel, s); 100 1723 : s += "\"" + json_escape(keys[i]) + "\":"; 101 1723 : if (prettyIndent) 102 985 : s += " "; 103 1723 : s += values.at(i).write(prettyIndent, indentLevel + 1); 104 1723 : if (i != (values.size() - 1)) 105 1125 : s += ","; 106 1723 : if (prettyIndent) 107 985 : s += "\n"; 108 1723 : } 109 : 110 599 : if (prettyIndent) 111 407 : indentStr(prettyIndent, indentLevel - 1, s); 112 599 : s += "}"; 113 599 : } 114 :