cprover
json.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_JSON_H
11 #define CPROVER_UTIL_JSON_H
12 
13 #include <vector>
14 #include <map>
15 #include <iosfwd>
16 #include <string>
17 
18 class json_objectt;
19 class json_arrayt;
20 
21 class jsont
22 {
23 public:
24  enum class kindt
25  {
26  J_STRING,
27  J_NUMBER,
28  J_OBJECT,
29  J_ARRAY,
30  J_TRUE,
31  J_FALSE,
32  J_NULL
33  };
34 
36 
37  bool is_string() const
38  {
39  return kind==kindt::J_STRING;
40  }
41 
42  bool is_number() const
43  {
44  return kind==kindt::J_NUMBER;
45  }
46 
47  bool is_object() const
48  {
49  return kind==kindt::J_OBJECT;
50  }
51 
52  bool is_array() const
53  {
54  return kind==kindt::J_ARRAY;
55  }
56 
57  bool is_true() const
58  {
59  return kind==kindt::J_TRUE;
60  }
61 
62  bool is_false() const
63  {
64  return kind==kindt::J_FALSE;
65  }
66 
67  bool is_null() const
68  {
69  return kind==kindt::J_NULL;
70  }
71 
72  jsont():kind(kindt::J_NULL)
73  {
74  }
75 
76  void output(std::ostream &out) const
77  {
78  output_rec(out, 0);
79  }
80 
81  void swap(jsont &other);
82 
83  static jsont json_boolean(bool value)
84  {
86  }
87 
88  void clear()
89  {
90  value.clear();
92  object.clear();
93  array.clear();
94  }
95 
98 
99  // this presumes that this is an object
100  const jsont &operator[](const std::string &key) const
101  {
102  objectt::const_iterator it=object.find(key);
103  if(it==object.end())
104  return null_json_object;
105  else
106  return it->second;
107  }
108 
109 protected:
110  void output_rec(std::ostream &, unsigned indent) const;
111  static void escape_string(const std::string &, std::ostream &);
112 
113  static const jsont null_json_object;
114 
115  explicit jsont(kindt _kind):kind(_kind)
116  {
117  }
118 
119  jsont(kindt _kind, const std::string &_value):kind(_kind), value(_value)
120  {
121  }
122 
123 public:
124  // should become protected
125  typedef std::vector<jsont> arrayt;
127 
128  typedef std::map<std::string, jsont> objectt;
130 
131  std::string value;
132 };
133 
134 inline std::ostream &operator<<(std::ostream &out, const jsont &src)
135 {
136  src.output(out);
137  return out;
138 }
139 
140 class json_arrayt:public jsont
141 {
142 public:
143  json_arrayt():jsont(kindt::J_ARRAY)
144  {
145  }
146 
147  void resize(std::size_t size)
148  {
149  array.resize(size);
150  }
151 
152  std::size_t size() const
153  {
154  return array.size();
155  }
156 
158  {
159  array.push_back(json);
160  return array.back();
161  }
162 
164  {
165  array.push_back(jsont());
166  return array.back();
167  }
168 };
169 
170 class json_stringt:public jsont
171 {
172 public:
173  explicit json_stringt(const std::string &_value):
174  jsont(kindt::J_STRING, _value)
175  {
176  }
177 };
178 
179 class json_numbert:public jsont
180 {
181 public:
182  explicit json_numbert(const std::string &_value):
183  jsont(kindt::J_NUMBER, _value)
184  {
185  }
186 };
187 
188 class json_objectt:public jsont
189 {
190 public:
191  json_objectt():jsont(kindt::J_OBJECT)
192  {
193  }
194 
195  jsont &operator[](const std::string &key)
196  {
197  return object[key];
198  }
199 
200  const jsont &operator[](const std::string &key) const
201  {
202  objectt::const_iterator it=object.find(key);
203  if(it==object.end())
204  return null_json_object;
205  else
206  return it->second;
207  }
208 };
209 
210 class json_truet:public jsont
211 {
212 public:
213  json_truet():jsont(kindt::J_TRUE) { }
214 };
215 
216 class json_falset:public jsont
217 {
218 public:
219  json_falset():jsont(kindt::J_FALSE) { }
220 };
221 
222 class json_nullt:public jsont
223 {
224 public:
225  json_nullt():jsont(kindt::J_NULL) { }
226 };
227 
229 {
231  return static_cast<json_arrayt &>(*this);
232 }
233 
235 {
237  return static_cast<json_objectt &>(*this);
238 }
239 
240 #endif // CPROVER_UTIL_JSON_H
std::ostream & operator<<(std::ostream &out, const jsont &src)
Definition: json.h:134
bool is_object() const
Definition: json.h:47
json_truet()
Definition: json.h:213
Definition: json.h:21
static jsont json_boolean(bool value)
Definition: json.h:83
bool is_number() const
Definition: json.h:42
static void escape_string(const std::string &, std::ostream &)
Definition: json.cpp:15
json_arrayt & make_array()
Definition: json.h:228
jsont & push_back(const jsont &json)
Definition: json.h:157
std::map< std::string, jsont > objectt
Definition: json.h:128
void output_rec(std::ostream &, unsigned indent) const
Definition: json.cpp:53
void output(std::ostream &out) const
Definition: json.h:76
const jsont & operator[](const std::string &key) const
Definition: json.h:200
bool is_false() const
Definition: json.h:62
kindt
Definition: json.h:24
jsont(kindt _kind, const std::string &_value)
Definition: json.h:119
const jsont & operator[](const std::string &key) const
Definition: json.h:100
bool is_null() const
Definition: json.h:67
objectt object
Definition: json.h:129
jsont(kindt _kind)
Definition: json.h:115
json_stringt(const std::string &_value)
Definition: json.h:173
json_numbert(const std::string &_value)
Definition: json.h:182
bool is_true() const
Definition: json.h:57
jsont & operator[](const std::string &key)
Definition: json.h:195
bool is_string() const
Definition: json.h:37
bool is_array() const
Definition: json.h:52
std::string value
Definition: json.h:131
json_objectt()
Definition: json.h:191
static const jsont null_json_object
Definition: json.h:113
std::vector< jsont > arrayt
Definition: json.h:125
void resize(std::size_t size)
Definition: json.h:147
void swap(jsont &other)
Definition: json.cpp:136
json_arrayt()
Definition: json.h:143
json_objectt & make_object()
Definition: json.h:234
jsont()
Definition: json.h:72
json_nullt()
Definition: json.h:225
kindt kind
Definition: json.h:35
arrayt array
Definition: json.h:126
jsont & push_back()
Definition: json.h:163
json_falset()
Definition: json.h:219
std::size_t size() const
Definition: json.h:152
json_objectt json(const source_locationt &location)
Definition: json_expr.cpp:23
void clear()
Definition: json.h:88