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