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