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 <iomanip>
# 6 : : #include <stdio.h>
# 7 : : #include "univalue.h"
# 8 : : #include "univalue_escapes.h"
# 9 : :
# 10 : : static std::string json_escape(const std::string& inS)
# 11 : 3408377 : {
# 12 : 3408377 : std::string outS;
# 13 : 3408377 : outS.reserve(inS.size() * 2);
# 14 : :
# 15 [ + + ]: 134470068 : for (unsigned int i = 0; i < inS.size(); i++) {
# 16 : 131061691 : unsigned char ch = static_cast<unsigned char>(inS[i]);
# 17 : 131061691 : const char *escStr = escapes[ch];
# 18 : :
# 19 [ + + ]: 131061691 : if (escStr)
# 20 : 22599 : outS += escStr;
# 21 : 131039092 : else
# 22 : 131039092 : outS += static_cast<char>(ch);
# 23 : 131061691 : }
# 24 : :
# 25 : 3408377 : return outS;
# 26 : 3408377 : }
# 27 : :
# 28 : : std::string UniValue::write(unsigned int prettyIndent,
# 29 : : unsigned int indentLevel) const
# 30 : 3122300 : {
# 31 : 3122300 : std::string s;
# 32 : 3122300 : s.reserve(1024);
# 33 : :
# 34 : 3122300 : unsigned int modIndent = indentLevel;
# 35 [ + + ]: 3122300 : if (modIndent == 0)
# 36 : 190774 : modIndent = 1;
# 37 : :
# 38 [ - + ]: 3122300 : switch (typ) {
# 39 [ + + ]: 147105 : case VNULL:
# 40 : 147105 : s += "null";
# 41 : 147105 : break;
# 42 [ + + ]: 370848 : case VOBJ:
# 43 : 370848 : writeObject(prettyIndent, modIndent, s);
# 44 : 370848 : break;
# 45 [ + + ]: 109747 : case VARR:
# 46 : 109747 : writeArray(prettyIndent, modIndent, s);
# 47 : 109747 : break;
# 48 [ + + ]: 1110158 : case VSTR:
# 49 : 1110158 : s += "\"" + json_escape(val) + "\"";
# 50 : 1110158 : break;
# 51 [ + + ]: 1152754 : case VNUM:
# 52 : 1152754 : s += val;
# 53 : 1152754 : break;
# 54 [ + + ]: 231688 : case VBOOL:
# 55 [ + + ]: 231688 : s += (val == "1" ? "true" : "false");
# 56 : 231688 : break;
# 57 : 3122300 : }
# 58 : :
# 59 : 3122300 : return s;
# 60 : 3122300 : }
# 61 : :
# 62 : : static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
# 63 : 30599 : {
# 64 : 30599 : s.append(prettyIndent * indentLevel, ' ');
# 65 : 30599 : }
# 66 : :
# 67 : : void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
# 68 : 109747 : {
# 69 : 109747 : s += "[";
# 70 [ + + ]: 109747 : if (prettyIndent)
# 71 : 5169 : s += "\n";
# 72 : :
# 73 [ + + ]: 741334 : for (unsigned int i = 0; i < values.size(); i++) {
# 74 [ + + ]: 631587 : if (prettyIndent)
# 75 : 14999 : indentStr(prettyIndent, indentLevel, s);
# 76 : 631587 : s += values[i].write(prettyIndent, indentLevel + 1);
# 77 [ + + ]: 631587 : if (i != (values.size() - 1)) {
# 78 : 562658 : s += ",";
# 79 : 562658 : }
# 80 [ + + ]: 631587 : if (prettyIndent)
# 81 : 14999 : s += "\n";
# 82 : 631587 : }
# 83 : :
# 84 [ + + ]: 109747 : if (prettyIndent)
# 85 : 5169 : indentStr(prettyIndent, indentLevel - 1, s);
# 86 : 109747 : s += "]";
# 87 : 109747 : }
# 88 : :
# 89 : : void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
# 90 : 370848 : {
# 91 : 370848 : s += "{";
# 92 [ + + ]: 370848 : if (prettyIndent)
# 93 : 3749 : s += "\n";
# 94 : :
# 95 [ + + ]: 2669067 : for (unsigned int i = 0; i < keys.size(); i++) {
# 96 [ + + ]: 2298219 : if (prettyIndent)
# 97 : 6682 : indentStr(prettyIndent, indentLevel, s);
# 98 : 2298219 : s += "\"" + json_escape(keys[i]) + "\":";
# 99 [ + + ]: 2298219 : if (prettyIndent)
# 100 : 6682 : s += " ";
# 101 : 2298219 : s += values.at(i).write(prettyIndent, indentLevel + 1);
# 102 [ + + ]: 2298219 : if (i != (values.size() - 1))
# 103 : 1928813 : s += ",";
# 104 [ + + ]: 2298219 : if (prettyIndent)
# 105 : 6682 : s += "\n";
# 106 : 2298219 : }
# 107 : :
# 108 [ + + ]: 370848 : if (prettyIndent)
# 109 : 3749 : indentStr(prettyIndent, indentLevel - 1, s);
# 110 : 370848 : s += "}";
# 111 : 370848 : }
# 112 : :
|