CMSC23700 Common Code Library
Support code for CS23700 programming projects
Loading...
Searching...
No Matches
json.hpp
Go to the documentation of this file.
1
11/*
12 * This code is loosely based on that of the SimpleJSON Library (http://mjpa.in/json)
13 * by Mike Anchor.
14 *
15 * COPYRIGHT (c) 2015-2023 John Reppy (http://cs.uchicago.edu/~jhr)
16 * All rights reserved.
17 */
18
19#ifndef _JSON_HPP_
20#define _JSON_HPP_
21
22#include <vector>
23#include <string>
24#include <map>
25#include <cstdint>
26
27namespace json {
28
39
40 class Value;
41 class Object;
42 class Array;
43 class Number;
44 class Integer;
45 class Real;
46 class String;
47 class Bool;
48 class Null;
49
50 // parse a JSON file; this returns nullptr if there is a parsing error
51 Value *parseFile (std::string filename);
52
53 // virtual base class of JSON values
54 class Value {
55 public:
56
58 Type type() const { return this->_ty; }
59
61 bool isObject() const { return (this->type() == T_OBJECT); }
62
64 bool isArray() const { return (this->type() == T_ARRAY); }
65
67 bool isNumber() const
68 {
69 return (this->type() == T_REAL) || (this->type() == T_INTEGER);
70 }
71
73 bool isInteger() const { return (this->type() == T_INTEGER); }
74
76 bool isReal() const { return (this->type() == T_REAL); }
77
79 bool isString() const { return (this->type() == T_STRING); }
80
82 bool isBool() const { return (this->type() == T_BOOL); }
83
85 bool isNull() const { return (this->type() == T_NULL); }
86
88 const Object *asObject () const;
89
91 const Array *asArray () const;
92
94 const Number *asNumber () const;
95
97 const Integer *asInteger () const;
98
100 const Real *asReal () const;
101
103 const String *asString () const;
104
106 const Bool *asBool () const;
107
108 virtual ~Value();
109 virtual std::string toString() = 0;
110
111 protected:
112
113 explicit Value (Type ty) : _ty(ty) { };
114
116 };
117
118 inline std::ostream& operator<< (std::ostream& s, Value *v)
119 {
120 return s << v->toString();
121 }
122
124 class Object : public Value {
125 public:
126 Object () : Value(T_OBJECT), _value() { };
128
130 int size () const { return this->_value.size(); }
131
133 void insert (std::string key, Value *val);
134
137 Value *operator[] (std::string key) const;
138
141 const Object *fieldAsObject (std::string key) const
142 {
143 const Value *v = (*this)[key];
144 return (v != nullptr) ? v->asObject() : nullptr;
145 }
146
149 const Array *fieldAsArray (std::string key) const
150 {
151 const Value *v = (*this)[key];
152 return (v != nullptr) ? v->asArray() : nullptr;
153 }
154
157 const Number *fieldAsNumber (std::string key) const
158 {
159 const Value *v = (*this)[key];
160 return (v != nullptr) ? v->asNumber() : nullptr;
161 }
162
165 const Integer *fieldAsInteger (std::string key) const
166 {
167 const Value *v = (*this)[key];
168 return (v != nullptr) ? v->asInteger() : nullptr;
169 }
170
173 const Real *fieldAsReal (std::string key) const
174 {
175 const Value *v = (*this)[key];
176 return (v != nullptr) ? v->asReal() : nullptr;
177 }
178
181 const String *fieldAsString (std::string key) const
182 {
183 const Value *v = (*this)[key];
184 return (v != nullptr) ? v->asString() : nullptr;
185 }
186
189 const Bool *fieldAsBool (std::string key) const
190 {
191 const Value *v = (*this)[key];
192 return (v != nullptr) ? v->asBool() : nullptr;
193 }
194
195 std::string toString();
196
197 private:
198 std::map<std::string, Value *> _value;
199 };
200
202 class Array : public Value {
203 public:
204 Array () : Value(T_ARRAY), _value() { };
206
207 int length () const { return static_cast<int>(this->_value.size()); }
208 void add (Value *v) { this->_value.push_back(v); }
209
210 Value *operator[] (int idx) const { return this->_value[idx]; }
211
212 std::string toString();
213
214 private:
215 std::vector<Value *> _value;
216 };
217
219 class Number : public Value {
220 public:
221 virtual ~Number ();
222
223 virtual std::string toString() = 0;
224 virtual double realVal () const = 0;
225
226 protected:
227 explicit Number (Type ty) : Value(ty) { };
228
229 };
230
231 class Integer : public Number {
232 public:
233 Integer (int64_t v) : Number(T_INTEGER), _value(v) { }
235
236 int64_t intVal () const { return this->_value; }
237 double realVal () const { return static_cast<double>(this->_value); }
238
239 std::string toString();
240
241 private:
242 int64_t _value;
243 };
244
245 class Real : public Number {
246 public:
247 Real (double v) : Number(T_REAL), _value(v) { }
249
250 double realVal () const { return this->_value; }
251
252 std::string toString();
253
254 private:
255 double _value;
256 };
257
258 class String : public Value {
259 public:
260 String (std::string v) : Value(T_STRING), _value(v) { };
262
263 std::string value () const { return this->_value; }
264
265 std::string toString();
266
267 private:
268 std::string _value;
269 };
270
271 class Bool : public Value {
272 public:
273 Bool (bool v) : Value(T_BOOL), _value(v) { };
275
276 bool value () const { return this->_value; }
277
278 std::string toString();
279
280 private:
281 bool _value;
282 };
283
284 class Null : public Value {
285 public:
286 Null () : Value(T_NULL) { };
288
289 std::string toString();
290
291 };
292
293} // namespace json
294
295#endif // !_JSON_HPP_
JSON arrays.
Definition json.hpp:202
void add(Value *v)
Definition json.hpp:208
int length() const
Definition json.hpp:207
Array()
Definition json.hpp:204
Value * operator[](int idx) const
Definition json.hpp:210
std::string toString()
Definition json.hpp:271
std::string toString()
Bool(bool v)
Definition json.hpp:273
bool value() const
Definition json.hpp:276
Definition json.hpp:231
double realVal() const
Definition json.hpp:237
std::string toString()
Integer(int64_t v)
Definition json.hpp:233
int64_t intVal() const
Definition json.hpp:236
Definition json.hpp:284
std::string toString()
Null()
Definition json.hpp:286
base class for JSON numbers
Definition json.hpp:219
virtual ~Number()
Number(Type ty)
Definition json.hpp:227
virtual double realVal() const =0
virtual std::string toString()=0
JSON objects.
Definition json.hpp:124
void insert(std::string key, Value *val)
insert a key-value pair into the object
const String * fieldAsString(std::string key) const
Definition json.hpp:181
const Real * fieldAsReal(std::string key) const
Definition json.hpp:173
const Object * fieldAsObject(std::string key) const
Definition json.hpp:141
Object()
Definition json.hpp:126
int size() const
return the number of fields in the object
Definition json.hpp:130
const Integer * fieldAsInteger(std::string key) const
Definition json.hpp:165
const Array * fieldAsArray(std::string key) const
Definition json.hpp:149
std::string toString()
Value * operator[](std::string key) const
const Number * fieldAsNumber(std::string key) const
Definition json.hpp:157
const Bool * fieldAsBool(std::string key) const
Definition json.hpp:189
Definition json.hpp:245
Real(double v)
Definition json.hpp:247
std::string toString()
double realVal() const
Definition json.hpp:250
Definition json.hpp:258
std::string toString()
String(std::string v)
Definition json.hpp:260
std::string value() const
Definition json.hpp:263
Definition json.hpp:54
const Number * asNumber() const
dynamic cast to JSON number
virtual ~Value()
bool isNull() const
return true if this value is the null value
Definition json.hpp:85
bool isObject() const
return true if this value is an object
Definition json.hpp:61
Type type() const
return the type of this JSON value
Definition json.hpp:58
const Real * asReal() const
dynamic cast to JSON real number
Type _ty
Definition json.hpp:115
const Object * asObject() const
dynamic cast to JSON object
const Array * asArray() const
dynamic cast to JSON array
bool isBool() const
return true if this value is a boolean
Definition json.hpp:82
virtual std::string toString()=0
const Bool * asBool() const
dynamic cast to JSON boolean
const Integer * asInteger() const
dynamic cast to JSON integer
bool isNumber() const
return true if this value is a number
Definition json.hpp:67
bool isArray() const
return true if this value is an array
Definition json.hpp:64
bool isString() const
return true if this value is a string
Definition json.hpp:79
Value(Type ty)
Definition json.hpp:113
bool isInteger() const
return true if this value is an integer
Definition json.hpp:73
bool isReal() const
return true if this value is a real number
Definition json.hpp:76
const String * asString() const
dynamic cast to JSON string
Definition json.hpp:27
Value * parseFile(std::string filename)
std::ostream & operator<<(std::ostream &s, Value *v)
Definition json.hpp:118
Type
the types of JSON values
Definition json.hpp:30
@ T_NULL
the null value
Definition json.hpp:37
@ T_REAL
real numbers (represented as doubles)
Definition json.hpp:34
@ T_ARRAY
arrays of JSON values
Definition json.hpp:32
@ T_BOOL
booleans
Definition json.hpp:36
@ T_STRING
strings
Definition json.hpp:35
@ T_INTEGER
integer numbers (represented as int64_t)
Definition json.hpp:33
@ T_OBJECT
object consisting of name-value pairs
Definition json.hpp:31