cprover
json.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "json.h"
10 
11 #include <ostream>
12 
14 
15 void jsont::escape_string(const std::string &src, std::ostream &out)
16 {
17  for(const auto &ch : src)
18  {
19  switch(ch)
20  {
21  case '\\':
22  case '"':
23  out << '\\';
24  out << ch;
25  break;
26 
27  case '\b':
28  out << "\\b";
29  break;
30 
31  case '\f':
32  out << "\\f";
33  break;
34 
35  case '\n':
36  out << "\\n";
37  break;
38 
39  case '\r':
40  out << "\\r";
41  break;
42 
43  case '\t':
44  out << "\\t";
45  break;
46 
47  default:
48  out << ch;
49  }
50  }
51 }
52 
56 void jsont::output_rec(std::ostream &out, unsigned indent) const
57 {
58  switch(kind)
59  {
60  case kindt::J_STRING:
61  out << '"';
62  escape_string(value, out);
63  out << '"';
64  break;
65 
66  case kindt::J_NUMBER:
67  out << value;
68  break;
69 
70  case kindt::J_OBJECT:
71  out << '{';
72  output_object(out, object, indent);
73  if(!object.empty())
74  {
75  out << '\n';
76  out << std::string(indent*2, ' ');
77  }
78  out << '}';
79  break;
80 
81  case kindt::J_ARRAY:
82  out << '[';
83 
84  if(array.empty())
85  out << ' ';
86  else
87  {
88  for(arrayt::const_iterator a_it=array.begin();
89  a_it!=array.end();
90  a_it++)
91  {
92  if(a_it!=array.begin())
93  out << ',';
94 
95  if(a_it->is_object())
96  {
97  out << '\n';
98  out << std::string((indent+1)*2, ' ');
99  }
100  else
101  out << ' ';
102 
103  a_it->output_rec(out, indent+1);
104  }
105 
106  if(array.back().is_object())
107  out << '\n' << std::string(indent*2, ' ');
108  else
109  out << ' ';
110  }
111 
112  out << ']';
113  break;
114 
115  case kindt::J_TRUE: out << "true"; break;
116 
117  case kindt::J_FALSE: out << "false"; break;
118 
119  case kindt::J_NULL: out << "null"; break;
120  }
121 }
122 
130  std::ostream &out,
131  const objectt &object,
132  unsigned indent)
133 {
134  for(objectt::const_iterator o_it = object.begin(); o_it != object.end();
135  o_it++)
136  {
137  if(o_it != object.begin())
138  out << ',';
139 
140  // A JSON object always starts with an opening brace,
141  // after which we put a newline.
142  out << '\n';
143 
144  out << std::string((indent + 1) * 2, ' ');
145 
146  jsont::output_key(out, o_it->first);
147  o_it->second.output_rec(out, indent + 1);
148  }
149 }
150 
151 void jsont::output_key(std::ostream &out, const std::string &key)
152 {
153  out << '"';
154  jsont::escape_string(key, out);
155  out << "\": ";
156 }
157 
158 void jsont::swap(jsont &other)
159 {
160  std::swap(other.kind, kind);
161  other.array.swap(array);
162  other.object.swap(object);
163  other.value.swap(value);
164 }
static void output_object(std::ostream &out, const objectt &object, unsigned indent)
Basic handling of the printing of a JSON object.
Definition: json.cpp:129
static void output_key(std::ostream &out, const std::string &key)
Definition: json.cpp:151
Definition: json.h:23
static void escape_string(const std::string &, std::ostream &)
Definition: json.cpp:15
std::map< std::string, jsont > objectt
Definition: json.h:131
void output_rec(std::ostream &, unsigned indent) const
Recursive printing of the json object.
Definition: json.cpp:56
objectt object
Definition: json.h:132
std::string value
Definition: json.h:137
static const jsont null_json_object
Definition: json.h:113
void swap(jsont &other)
Definition: json.cpp:158
kindt kind
Definition: json.h:37
arrayt array
Definition: json.h:129