OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 namespace blink { | 50 namespace blink { |
51 | 51 |
52 class JSONArray; | 52 class JSONArray; |
53 class JSONObject; | 53 class JSONObject; |
54 | 54 |
55 class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> { | 55 class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> { |
56 public: | 56 public: |
57 static const int maxDepth = 1000; | 57 static const int maxDepth = 1000; |
58 | 58 |
59 JSONValue() : m_type(TypeNull) { } | |
60 virtual ~JSONValue() { } | 59 virtual ~JSONValue() { } |
61 | 60 |
62 static PassRefPtr<JSONValue> null() | 61 static PassRefPtr<JSONValue> null() |
63 { | 62 { |
64 return adoptRef(new JSONValue()); | 63 return adoptRef(new JSONValue()); |
65 } | 64 } |
66 | 65 |
67 typedef enum { | 66 typedef enum { |
68 TypeNull = 0, | 67 TypeNull = 0, |
69 TypeBoolean, | 68 TypeBoolean, |
70 TypeNumber, | 69 TypeNumber, |
71 TypeString, | 70 TypeString, |
72 TypeObject, | 71 TypeObject, |
73 TypeArray | 72 TypeArray |
74 } Type; | 73 } Type; |
75 | 74 |
76 Type type() const { return m_type; } | 75 Type type() const { return m_type; } |
77 | 76 |
78 bool isNull() const { return m_type == TypeNull; } | 77 bool isNull() const { return m_type == TypeNull; } |
79 | 78 |
80 virtual bool asBoolean(bool* output) const; | 79 virtual bool asBoolean(bool* output) const; |
81 virtual bool asNumber(double* output) const; | 80 virtual bool asNumber(double* output) const; |
82 virtual bool asNumber(long* output) const; | 81 virtual bool asNumber(long* output) const; |
83 virtual bool asNumber(int* output) const; | 82 virtual bool asNumber(int* output) const; |
84 virtual bool asNumber(unsigned long* output) const; | 83 virtual bool asNumber(unsigned long* output) const; |
85 virtual bool asNumber(unsigned* output) const; | 84 virtual bool asNumber(unsigned* output) const; |
86 virtual bool asString(String* output) const; | 85 virtual bool asString(String* output) const; |
87 virtual bool asObject(RefPtr<JSONObject>* output); | |
88 virtual bool asArray(RefPtr<JSONArray>* output); | |
89 virtual PassRefPtr<JSONObject> asObject(); | |
90 virtual PassRefPtr<JSONArray> asArray(); | |
91 | 86 |
92 String toJSONString() const; | 87 String toJSONString() const; |
93 String toPrettyJSONString() const; | 88 String toPrettyJSONString() const; |
94 virtual void writeJSON(StringBuilder* output) const; | 89 virtual void writeJSON(StringBuilder* output) const; |
95 virtual void prettyWriteJSON(StringBuilder* output) const; | 90 virtual void prettyWriteJSON(StringBuilder* output) const; |
96 | 91 |
97 static String quoteString(const String&); | 92 static String quoteString(const String&); |
98 | 93 |
99 protected: | 94 protected: |
| 95 JSONValue() : m_type(TypeNull) { } |
100 explicit JSONValue(Type type) : m_type(type) { } | 96 explicit JSONValue(Type type) : m_type(type) { } |
101 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const
; | 97 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const
; |
102 | 98 |
103 private: | 99 private: |
104 friend class JSONObjectBase; | 100 friend class JSONObject; |
105 friend class JSONArrayBase; | 101 friend class JSONArray; |
106 | 102 |
107 Type m_type; | 103 Type m_type; |
108 }; | 104 }; |
109 | 105 |
110 class PLATFORM_EXPORT JSONBasicValue : public JSONValue { | 106 class PLATFORM_EXPORT JSONBasicValue : public JSONValue { |
111 public: | 107 public: |
112 | 108 |
113 static PassRefPtr<JSONBasicValue> create(bool value) | 109 static PassRefPtr<JSONBasicValue> create(bool value) |
114 { | 110 { |
115 return adoptRef(new JSONBasicValue(value)); | 111 return adoptRef(new JSONBasicValue(value)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 157 |
162 void writeJSON(StringBuilder* output) const override; | 158 void writeJSON(StringBuilder* output) const override; |
163 | 159 |
164 private: | 160 private: |
165 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } | 161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } |
166 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } | 162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } |
167 | 163 |
168 String m_stringValue; | 164 String m_stringValue; |
169 }; | 165 }; |
170 | 166 |
171 class PLATFORM_EXPORT JSONObjectBase : public JSONValue { | 167 class PLATFORM_EXPORT JSONObject : public JSONValue { |
172 private: | 168 private: |
173 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; | 169 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; |
174 | 170 |
175 public: | 171 public: |
176 typedef Dictionary::iterator iterator; | 172 typedef Dictionary::iterator iterator; |
177 typedef Dictionary::const_iterator const_iterator; | 173 typedef Dictionary::const_iterator const_iterator; |
178 | 174 |
179 PassRefPtr<JSONObject> asObject() override; | 175 static PassRefPtr<JSONObject> create() |
180 JSONObject* openAccessors(); | 176 { |
| 177 return adoptRef(new JSONObject()); |
| 178 } |
| 179 |
| 180 static PassRefPtr<JSONObject> cast(PassRefPtr<JSONValue> value) |
| 181 { |
| 182 if (!value || value->type() != TypeObject) |
| 183 return nullptr; |
| 184 return adoptRef(static_cast<JSONObject*>(value.leakRef())); |
| 185 } |
181 | 186 |
182 void writeJSON(StringBuilder* output) const override; | 187 void writeJSON(StringBuilder* output) const override; |
183 | 188 |
184 int size() const { return m_data.size(); } | 189 int size() const { return m_data.size(); } |
185 | 190 |
186 protected: | |
187 ~JSONObjectBase() override; | |
188 | |
189 bool asObject(RefPtr<JSONObject>* output) override; | |
190 | |
191 void setBoolean(const String& name, bool); | 191 void setBoolean(const String& name, bool); |
192 void setNumber(const String& name, double); | 192 void setNumber(const String& name, double); |
193 void setString(const String& name, const String&); | 193 void setString(const String& name, const String&); |
194 void setValue(const String& name, PassRefPtr<JSONValue>); | 194 void setValue(const String& name, PassRefPtr<JSONValue>); |
195 void setObject(const String& name, PassRefPtr<JSONObject>); | 195 void setObject(const String& name, PassRefPtr<JSONObject>); |
196 void setArray(const String& name, PassRefPtr<JSONArray>); | 196 void setArray(const String& name, PassRefPtr<JSONArray>); |
197 | 197 |
198 iterator find(const String& name); | 198 iterator find(const String& name); |
199 const_iterator find(const String& name) const; | 199 const_iterator find(const String& name) const; |
200 bool getBoolean(const String& name, bool* output) const; | 200 bool getBoolean(const String& name, bool* output) const; |
201 template<class T> bool getNumber(const String& name, T* output) const | 201 template<class T> bool getNumber(const String& name, T* output) const |
202 { | 202 { |
203 RefPtr<JSONValue> value = get(name); | 203 RefPtr<JSONValue> value = get(name); |
204 if (!value) | 204 if (!value) |
205 return false; | 205 return false; |
206 return value->asNumber(output); | 206 return value->asNumber(output); |
207 } | 207 } |
208 bool getString(const String& name, String* output) const; | 208 bool getString(const String& name, String* output) const; |
209 PassRefPtr<JSONObject> getObject(const String& name) const; | 209 PassRefPtr<JSONObject> getObject(const String& name) const; |
210 PassRefPtr<JSONArray> getArray(const String& name) const; | 210 PassRefPtr<JSONArray> getArray(const String& name) const; |
211 PassRefPtr<JSONValue> get(const String& name) const; | 211 PassRefPtr<JSONValue> get(const String& name) const; |
212 | 212 |
213 bool booleanProperty(const String& name, bool defaultValue) const; | 213 bool booleanProperty(const String& name, bool defaultValue) const; |
214 | 214 |
215 void remove(const String& name); | 215 void remove(const String& name); |
216 | 216 |
217 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; | |
218 | |
219 iterator begin() { return m_data.begin(); } | 217 iterator begin() { return m_data.begin(); } |
220 iterator end() { return m_data.end(); } | 218 iterator end() { return m_data.end(); } |
221 const_iterator begin() const { return m_data.begin(); } | 219 const_iterator begin() const { return m_data.begin(); } |
222 const_iterator end() const { return m_data.end(); } | 220 const_iterator end() const { return m_data.end(); } |
| 221 ~JSONObject() override; |
223 | 222 |
224 protected: | 223 protected: |
225 JSONObjectBase(); | 224 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; |
226 | 225 |
227 private: | 226 private: |
| 227 JSONObject(); |
| 228 |
228 Dictionary m_data; | 229 Dictionary m_data; |
229 Vector<String> m_order; | 230 Vector<String> m_order; |
230 }; | 231 }; |
231 | 232 |
232 class PLATFORM_EXPORT JSONObject : public JSONObjectBase { | 233 class PLATFORM_EXPORT JSONArray : public JSONValue { |
233 public: | |
234 static PassRefPtr<JSONObject> create() | |
235 { | |
236 return adoptRef(new JSONObject()); | |
237 } | |
238 | |
239 using JSONObjectBase::asObject; | |
240 | |
241 using JSONObjectBase::setBoolean; | |
242 using JSONObjectBase::setNumber; | |
243 using JSONObjectBase::setString; | |
244 using JSONObjectBase::setValue; | |
245 using JSONObjectBase::setObject; | |
246 using JSONObjectBase::setArray; | |
247 | |
248 using JSONObjectBase::find; | |
249 using JSONObjectBase::getBoolean; | |
250 using JSONObjectBase::getNumber; | |
251 using JSONObjectBase::getString; | |
252 using JSONObjectBase::getObject; | |
253 using JSONObjectBase::getArray; | |
254 using JSONObjectBase::get; | |
255 | |
256 using JSONObjectBase::booleanProperty; | |
257 | |
258 using JSONObjectBase::remove; | |
259 | |
260 using JSONObjectBase::begin; | |
261 using JSONObjectBase::end; | |
262 | |
263 using JSONObjectBase::size; | |
264 }; | |
265 | |
266 | |
267 class PLATFORM_EXPORT JSONArrayBase : public JSONValue { | |
268 public: | 234 public: |
269 typedef Vector<RefPtr<JSONValue>>::iterator iterator; | 235 typedef Vector<RefPtr<JSONValue>>::iterator iterator; |
270 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; | 236 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; |
271 | 237 |
272 PassRefPtr<JSONArray> asArray() override; | 238 static PassRefPtr<JSONArray> create() |
| 239 { |
| 240 return adoptRef(new JSONArray()); |
| 241 } |
273 | 242 |
274 unsigned length() const { return m_data.size(); } | 243 static PassRefPtr<JSONArray> cast(PassRefPtr<JSONValue> value) |
| 244 { |
| 245 if (!value || value->type() != TypeArray) |
| 246 return nullptr; |
| 247 return adoptRef(static_cast<JSONArray*>(value.leakRef())); |
| 248 } |
| 249 |
| 250 ~JSONArray() override; |
275 | 251 |
276 void writeJSON(StringBuilder* output) const override; | 252 void writeJSON(StringBuilder* output) const override; |
277 | 253 |
278 protected: | |
279 ~JSONArrayBase() override; | |
280 | |
281 bool asArray(RefPtr<JSONArray>* output) override; | |
282 | |
283 void pushBoolean(bool); | 254 void pushBoolean(bool); |
284 void pushInt(int); | 255 void pushInt(int); |
285 void pushNumber(double); | 256 void pushNumber(double); |
286 void pushString(const String&); | 257 void pushString(const String&); |
287 void pushValue(PassRefPtr<JSONValue>); | 258 void pushValue(PassRefPtr<JSONValue>); |
288 void pushObject(PassRefPtr<JSONObject>); | 259 void pushObject(PassRefPtr<JSONObject>); |
289 void pushArray(PassRefPtr<JSONArray>); | 260 void pushArray(PassRefPtr<JSONArray>); |
290 | 261 |
291 PassRefPtr<JSONValue> get(size_t index); | 262 PassRefPtr<JSONValue> get(size_t index); |
292 | 263 unsigned length() const { return m_data.size(); } |
293 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; | |
294 | 264 |
295 iterator begin() { return m_data.begin(); } | 265 iterator begin() { return m_data.begin(); } |
296 iterator end() { return m_data.end(); } | 266 iterator end() { return m_data.end(); } |
297 const_iterator begin() const { return m_data.begin(); } | 267 const_iterator begin() const { return m_data.begin(); } |
298 const_iterator end() const { return m_data.end(); } | 268 const_iterator end() const { return m_data.end(); } |
299 | 269 |
300 protected: | 270 protected: |
301 JSONArrayBase(); | 271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; |
302 | 272 |
303 private: | 273 private: |
| 274 JSONArray(); |
304 Vector<RefPtr<JSONValue>> m_data; | 275 Vector<RefPtr<JSONValue>> m_data; |
305 }; | 276 }; |
306 | 277 |
307 class PLATFORM_EXPORT JSONArray : public JSONArrayBase { | |
308 public: | |
309 static PassRefPtr<JSONArray> create() | |
310 { | |
311 return adoptRef(new JSONArray()); | |
312 } | |
313 | |
314 using JSONArrayBase::asArray; | |
315 | |
316 using JSONArrayBase::pushBoolean; | |
317 using JSONArrayBase::pushInt; | |
318 using JSONArrayBase::pushNumber; | |
319 using JSONArrayBase::pushString; | |
320 using JSONArrayBase::pushValue; | |
321 using JSONArrayBase::pushObject; | |
322 using JSONArrayBase::pushArray; | |
323 | |
324 using JSONArrayBase::get; | |
325 | |
326 using JSONArrayBase::begin; | |
327 using JSONArrayBase::end; | |
328 }; | |
329 | |
330 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); | 278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); |
331 void doubleQuoteStringForJSON(const String&, StringBuilder*); | 279 void doubleQuoteStringForJSON(const String&, StringBuilder*); |
332 | 280 |
333 } // namespace blink | 281 } // namespace blink |
334 | 282 |
335 #endif // JSONValues_h | 283 #endif // JSONValues_h |
OLD | NEW |