1 // Author: Hong Jiang <hong@hjiang.net>
2 /*
3 source: http://github.com/hjiang/jsonxx/
4 
5 Copyright (c) 2010 Hong Jiang
6 
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation
9 files (the "Software"), to deal in the Software without
10 restriction, including without limitation the rights to use,
11 copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following
14 conditions:
15 
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
27 
28 */
29 
30 #include <cassert>
31 #include <iostream>
32 #include <map>
33 #include <vector>
34 
35 namespace jsonxx {
36   bool match(const std::string& pattern, std::istream& input,
37 	     bool ignore_ws = true);
38   bool parse_string(std::istream& input, std::string* value);
39   bool parse_bool(std::istream& input, bool* value);
40   bool parse_null(std::istream& input);
41   bool parse_float(std::istream& input, double* value);
42   bool parse_number(std::istream& input, long* value);
43   bool parse_number(std::istream& input, int* value);
44 
45 // TODO: *::parse() should be static functions.
46 
47 class Value;
48 
49 // A JSON Object
50 class Object {
51   public:
52     Object();
53     ~Object();
54     bool parse(std::istream& input);
55 
56     template <typename T>
57     bool has(const std::string& key);
58 
59     // Always call has<>() first. If the key doesn't exist, consider
60     // the behavior undefined.
61     template <typename T>
62     T& get(const std::string& key);
63   private:
64     Object(const Object&);
65     Object& operator=(const Object&);
66 
67     std::map<std::string, Value*> value_map_;
68 };
69 
70 class Value;
71 
72 class Array {
73   public:
74     Array();
75     ~Array();
76     bool parse(std::istream& input);
77 
size()78     unsigned int size() { return values_.size(); }
79 
80     template <typename T>
81     bool has(unsigned int i);
82 
83     template <typename T>
84     T& get(unsigned int i);
85 
86   private:
87     Array(const Array&);
88     Array& operator=(const Array&);
89     std::vector<Value*> values_;
90 };
91 
92 // A value could be a number, an array, a string, an object, a
93 // boolean, or null
94 class Value {
95   public:
96     class Null {};
97 
98     Value();
99     ~Value();
100     bool parse(std::istream& input);
101     template<typename T>
102     bool is();
103     template<typename T>
104     T& get();
105   private:
106     Value(const Value&);
107     Value& operator=(const Value&);
108     enum {
109         INTEGER_,
110         STRING_,
111         BOOL_,
112         NULL_,
113         ARRAY_,
114         OBJECT_,
115         INVALID_
116     } type_;
117     union {
118         long integer_value_;
119         std::string* string_value_;
120         bool bool_value_;
121         Array* array_value_;
122         Object* object_value_;
123     };
124 };
125 
126 template <typename T>
has(unsigned int i)127 bool Array::has(unsigned int i) {
128     if (i >= size()) {
129         return false;
130     } else {
131         return values_[i]->is<T>();
132     }
133 }
134 
135 template <typename T>
get(unsigned int i)136 T& Array::get(unsigned int i) {
137     assert(i < size());
138     return values_[i]->get<T>();
139 }
140 
141 template <typename T>
has(const std::string & key)142 bool Object::has(const std::string& key) {
143     return value_map_.count(key) > 0 && value_map_[key]->is<T>();
144 }
145 
146 template <typename T>
get(const std::string & key)147 T& Object::get(const std::string& key) {
148     assert(has<T>(key));
149     return value_map_[key]->get<T>();
150 }
151 
152 template<>
153 inline bool Value::is<Value::Null>() {
154     return type_ == NULL_;
155 }
156 
157 template<>
158 inline bool Value::is<bool>() {
159     return type_ == BOOL_;
160 }
161 
162 template<>
163 inline bool Value::is<std::string>() {
164     return type_ == STRING_;
165 }
166 
167 template<>
168 inline bool Value::is<long>() {
169     return type_ == INTEGER_;
170 }
171 
172 template<>
173 inline bool Value::is<Array>() {
174     return type_ == ARRAY_;
175 }
176 
177 template<>
178 inline bool Value::is<Object>() {
179     return type_ == OBJECT_;
180 }
181 
182 template<>
183 inline bool& Value::get<bool>() {
184     assert(is<bool>());
185     return bool_value_;
186 }
187 
188 template<>
189 inline std::string& Value::get<std::string>() {
190     assert(is<std::string>());
191     return *string_value_;
192 }
193 
194 template<>
195 inline long& Value::get<long>() {
196     assert(is<long>());
197     return integer_value_;
198 }
199 
200 template<>
201 inline Array& Value::get<Array>() {
202     assert(is<Array>());
203     return *array_value_;
204 }
205 
206 template<>
207 inline Object& Value::get<Object>() {
208     assert(is<Object>());
209     return *object_value_;
210 }
211 
212 }  // namespace jsonxx
213