BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
univalue_write.cpp
Go to the documentation of this file.
1 // Copyright 2014 BitPay Inc.
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <iomanip>
6 #include <sstream>
7 #include <stdio.h>
8 #include "univalue.h"
9 #include "univalue_escapes.h"
10 
11 static std::string json_escape(const std::string& inS)
12 {
13  std::string outS;
14  outS.reserve(inS.size() * 2);
15 
16  for (unsigned int i = 0; i < inS.size(); i++) {
17  unsigned char ch = inS[i];
18  const char *escStr = escapes[ch];
19 
20  if (escStr)
21  outS += escStr;
22  else
23  outS += ch;
24  }
25 
26  return outS;
27 }
28 
29 std::string UniValue::write(unsigned int prettyIndent,
30  unsigned int indentLevel) const
31 {
32  std::string s;
33  s.reserve(1024);
34 
35  unsigned int modIndent = indentLevel;
36  if (modIndent == 0)
37  modIndent = 1;
38 
39  switch (typ) {
40  case VNULL:
41  s += "null";
42  break;
43  case VOBJ:
44  writeObject(prettyIndent, modIndent, s);
45  break;
46  case VARR:
47  writeArray(prettyIndent, modIndent, s);
48  break;
49  case VSTR:
50  s += "\"" + json_escape(val) + "\"";
51  break;
52  case VNUM:
53  s += val;
54  break;
55  case VBOOL:
56  s += (val == "1" ? "true" : "false");
57  break;
58  }
59 
60  return s;
61 }
62 
63 static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, std::string& s)
64 {
65  s.append(prettyIndent * indentLevel, ' ');
66 }
67 
68 void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
69 {
70  s += "[";
71  if (prettyIndent)
72  s += "\n";
73 
74  for (unsigned int i = 0; i < values.size(); i++) {
75  if (prettyIndent)
76  indentStr(prettyIndent, indentLevel, s);
77  s += values[i].write(prettyIndent, indentLevel + 1);
78  if (i != (values.size() - 1)) {
79  s += ",";
80  }
81  if (prettyIndent)
82  s += "\n";
83  }
84 
85  if (prettyIndent)
86  indentStr(prettyIndent, indentLevel - 1, s);
87  s += "]";
88 }
89 
90 void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
91 {
92  s += "{";
93  if (prettyIndent)
94  s += "\n";
95 
96  for (unsigned int i = 0; i < keys.size(); i++) {
97  if (prettyIndent)
98  indentStr(prettyIndent, indentLevel, s);
99  s += "\"" + json_escape(keys[i]) + "\":";
100  if (prettyIndent)
101  s += " ";
102  s += values.at(i).write(prettyIndent, indentLevel + 1);
103  if (i != (values.size() - 1))
104  s += ",";
105  if (prettyIndent)
106  s += "\n";
107  }
108 
109  if (prettyIndent)
110  indentStr(prettyIndent, indentLevel - 1, s);
111  s += "}";
112 }
113 
std::vector< UniValue > values
Definition: univalue.h:159
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string val
Definition: univalue.h:157
UniValue::VType typ
Definition: univalue.h:156
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const
std::vector< std::string > keys
Definition: univalue.h:158
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string &s) const