CMSC23700 Common Code Library
Support code for CS23700 programming projects
json.hxx
Go to the documentation of this file.
1 
6 /*
7  * COPYRIGHT (c) 2015 John Reppy (http://cs.uchicago.edu/~jhr)
8  * All rights reserved.
9  */
10 
11 #ifndef _JSON_HXX_
12 #define _JSON_HXX_
13 
14 #include <vector>
15 #include <string>
16 #include <map>
17 
18 namespace JSON {
19 
21  enum Type {
28  };
29 
30  class Value;
31  class Object;
32  class Array;
33  class Number;
34  class String;
35  class Bool;
36  class Null;
37 
38  // parse a JSON file; this returns nullptr if there is a parsing error
39  Value *ParseFile (std::string filename);
40 
41  // virtual base class of JSON values
42  class Value {
43  public:
44 
46  Type type() const { return this->_ty; }
47 
49  bool isObject() const { return (this->_ty == T_OBJECT); }
50 
52  bool isArray() const { return (this->_ty == T_ARRAY); }
53 
55  bool isNumber() const { return (this->_ty == T_NUMBER); }
56 
58  bool isString() const { return (this->_ty == T_STRING); }
59 
61  bool isBool() const { return (this->_ty == T_BOOL); }
62 
64  bool isNull() const { return (this->_ty == T_NULL); }
65 
67  const Object *asObject () const;
68 
70  const Array *asArray () const;
71 
73  const Number *asNumber () const;
74 
76  const String *asString () const;
77 
79  const Bool *asBool () const;
80 
81  virtual ~Value() = 0;
82  virtual std::string toString() = 0;
83 
84  protected:
85 
86  explicit Value (Type ty) : _ty(ty) { };
87 
88  Type _ty;
89  };
90 
91  inline std::ostream& operator<< (std::ostream& s, Value *v)
92  {
93  return s << v->toString();
94  }
95 
97  class Object : public Value {
98  public:
99  Object ();
100  ~Object ();
101 
103  int size () const { return this->_value.size(); }
104 
106  void insert (std::string key, Value *val);
107 
110  Value *operator[] (std::string key) const;
111 
112  std::string toString();
113 
114  private:
115  std::map<std::string, Value *> _value;
116  };
117 
119  class Array : public Value {
120  public:
121  Array ();
122  ~Array ();
123 
124  int length () const { return static_cast<int>(this->_value.size()); }
125  void add (Value *v) { this->_value.push_back(v); }
126 
127  Value *operator[] (int idx) const { return this->_value[idx]; }
128 
129  std::string toString();
130 
131  private:
132  std::vector<Value *> _value;
133  };
134 
135  class Number : public Value {
136  public:
137  Number (double v);
138  ~Number ();
139 
140  double value () const { return this->_value; }
141 
142  std::string toString();
143 
144  private:
145  double _value;
146  };
147 
148  class String : public Value {
149  public:
150  String (std::string v);
151  ~String ();
152 
153  std::string value () const { return this->_value; }
154 
155  std::string toString();
156 
157  private:
158  std::string _value;
159  };
160 
161  class Bool : public Value {
162  public:
163  Bool (bool v);
164  ~Bool ();
165 
166  bool value () const { return this->_value; }
167 
168  std::string toString();
169 
170  private:
171  bool _value;
172  };
173 
174  class Null : public Value {
175  public:
176  Null ();
177  ~Null ();
178 
179  std::string toString();
180 
181  };
182 
183 } // namespace JSON
184 
185 #endif // !_JSON_HXX_
object consisting of name-value pairs
Definition: json.hxx:22
std::string toString()
virtual ~Value()=0
Type _ty
Definition: json.hxx:86
std::ostream & operator<<(std::ostream &s, Value *v)
Definition: json.hxx:91
virtual std::string toString()=0
Definition: json.hxx:148
int length() const
Definition: json.hxx:124
bool isNumber() const
return true if this value is a number
Definition: json.hxx:55
Value * operator[](std::string key) const
bool isBool() const
return true if this value is a boolean
Definition: json.hxx:61
double value() const
Definition: json.hxx:140
JSON arrays.
Definition: json.hxx:119
booleans
Definition: json.hxx:26
void add(Value *v)
Definition: json.hxx:125
bool isString() const
return true if this value is a string
Definition: json.hxx:58
int size() const
return the number of fields in the object
Definition: json.hxx:103
bool isNull() const
return true if this value is the null value
Definition: json.hxx:64
Value * operator[](int idx) const
Definition: json.hxx:127
Definition: json.hxx:174
Number(double v)
std::string toString()
strings
Definition: json.hxx:25
void insert(std::string key, Value *val)
insert a key-value pair into the object
bool value() const
Definition: json.hxx:166
const String * asString() const
dynamic cast to JSON object
std::string value() const
Definition: json.hxx:153
arrays of JSON values
Definition: json.hxx:23
const Object * asObject() const
dynamic cast to JSON object
bool isArray() const
return true if this value is an array
Definition: json.hxx:52
Definition: json.hxx:42
const Array * asArray() const
dynamic cast to JSON object
bool isObject() const
return true if this value is an object
Definition: json.hxx:49
Definition: json.hxx:18
String(std::string v)
Bool(bool v)
Type
the types of JSON values
Definition: json.hxx:21
Value * ParseFile(std::string filename)
Definition: json.hxx:135
Definition: json.hxx:161
std::string toString()
const Number * asNumber() const
dynamic cast to JSON object
the null value
Definition: json.hxx:27
numbers (represented as doubles)
Definition: json.hxx:24
std::string toString()
std::string toString()
std::string toString()
const Bool * asBool() const
dynamic cast to JSON object
Type type() const
return the type of this JSON value
Definition: json.hxx:46
Value(Type ty)
Definition: json.hxx:86
JSON objects.
Definition: json.hxx:97