2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "platform/PlatformExport.h"
35 #include "wtf/Forward.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/RefCounted.h"
38 #include "wtf/Vector.h"
39 #include "wtf/text/StringHash.h"
40 #include "wtf/text/WTFString.h"
47 class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> {
49 static const int maxDepth = 1000;
51 JSONValue() : m_type(TypeNull) { }
52 virtual ~JSONValue() { }
54 static PassRefPtr<JSONValue> null()
56 return adoptRef(new JSONValue());
68 Type type() const { return m_type; }
70 bool isNull() const { return m_type == TypeNull; }
72 virtual bool asBoolean(bool* output) const;
73 virtual bool asNumber(double* output) const;
74 virtual bool asNumber(long* output) const;
75 virtual bool asNumber(int* output) const;
76 virtual bool asNumber(unsigned long* output) const;
77 virtual bool asNumber(unsigned* output) const;
78 virtual bool asString(String* output) const;
79 virtual bool asValue(RefPtr<JSONValue>* output);
80 virtual bool asObject(RefPtr<JSONObject>* output);
81 virtual bool asArray(RefPtr<JSONArray>* output);
82 virtual PassRefPtr<JSONObject> asObject();
83 virtual PassRefPtr<JSONArray> asArray();
85 String toJSONString() const;
86 String toPrettyJSONString() const;
87 virtual void writeJSON(StringBuilder* output) const;
88 virtual void prettyWriteJSON(StringBuilder* output) const;
91 explicit JSONValue(Type type) : m_type(type) { }
92 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const;
95 friend class JSONObjectBase;
96 friend class JSONArrayBase;
101 class PLATFORM_EXPORT JSONBasicValue : public JSONValue {
104 static PassRefPtr<JSONBasicValue> create(bool value)
106 return adoptRef(new JSONBasicValue(value));
109 static PassRefPtr<JSONBasicValue> create(int value)
111 return adoptRef(new JSONBasicValue(value));
114 static PassRefPtr<JSONBasicValue> create(double value)
116 return adoptRef(new JSONBasicValue(value));
119 virtual bool asBoolean(bool* output) const override;
120 virtual bool asNumber(double* output) const override;
121 virtual bool asNumber(long* output) const override;
122 virtual bool asNumber(int* output) const override;
123 virtual bool asNumber(unsigned long* output) const override;
124 virtual bool asNumber(unsigned* output) const override;
126 virtual void writeJSON(StringBuilder* output) const override;
129 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(value) { }
130 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((double)value) { }
131 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue(value) { }
135 double m_doubleValue;
139 class PLATFORM_EXPORT JSONString : public JSONValue {
141 static PassRefPtr<JSONString> create(const String& value)
143 return adoptRef(new JSONString(value));
146 static PassRefPtr<JSONString> create(const char* value)
148 return adoptRef(new JSONString(value));
151 virtual bool asString(String* output) const override;
153 virtual void writeJSON(StringBuilder* output) const override;
156 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringValue(value) { }
157 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValue(value) { }
159 String m_stringValue;
162 class PLATFORM_EXPORT JSONObjectBase : public JSONValue {
164 typedef HashMap<String, RefPtr<JSONValue> > Dictionary;
167 typedef Dictionary::iterator iterator;
168 typedef Dictionary::const_iterator const_iterator;
170 virtual PassRefPtr<JSONObject> asObject() override;
171 JSONObject* openAccessors();
173 virtual void writeJSON(StringBuilder* output) const override;
176 virtual ~JSONObjectBase();
178 virtual bool asObject(RefPtr<JSONObject>* output) override;
180 void setBoolean(const String& name, bool);
181 void setNumber(const String& name, double);
182 void setString(const String& name, const String&);
183 void setValue(const String& name, PassRefPtr<JSONValue>);
184 void setObject(const String& name, PassRefPtr<JSONObject>);
185 void setArray(const String& name, PassRefPtr<JSONArray>);
187 iterator find(const String& name);
188 const_iterator find(const String& name) const;
189 bool getBoolean(const String& name, bool* output) const;
190 template<class T> bool getNumber(const String& name, T* output) const
192 RefPtr<JSONValue> value = get(name);
195 return value->asNumber(output);
197 bool getString(const String& name, String* output) const;
198 PassRefPtr<JSONObject> getObject(const String& name) const;
199 PassRefPtr<JSONArray> getArray(const String& name) const;
200 PassRefPtr<JSONValue> get(const String& name) const;
202 void remove(const String& name);
204 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const override;
206 iterator begin() { return m_data.begin(); }
207 iterator end() { return m_data.end(); }
208 const_iterator begin() const { return m_data.begin(); }
209 const_iterator end() const { return m_data.end(); }
211 int size() const { return m_data.size(); }
218 Vector<String> m_order;
221 class PLATFORM_EXPORT JSONObject : public JSONObjectBase {
223 static PassRefPtr<JSONObject> create()
225 return adoptRef(new JSONObject());
228 using JSONObjectBase::asObject;
230 using JSONObjectBase::setBoolean;
231 using JSONObjectBase::setNumber;
232 using JSONObjectBase::setString;
233 using JSONObjectBase::setValue;
234 using JSONObjectBase::setObject;
235 using JSONObjectBase::setArray;
237 using JSONObjectBase::find;
238 using JSONObjectBase::getBoolean;
239 using JSONObjectBase::getNumber;
240 using JSONObjectBase::getString;
241 using JSONObjectBase::getObject;
242 using JSONObjectBase::getArray;
243 using JSONObjectBase::get;
245 using JSONObjectBase::remove;
247 using JSONObjectBase::begin;
248 using JSONObjectBase::end;
250 using JSONObjectBase::size;
254 class PLATFORM_EXPORT JSONArrayBase : public JSONValue {
256 typedef Vector<RefPtr<JSONValue> >::iterator iterator;
257 typedef Vector<RefPtr<JSONValue> >::const_iterator const_iterator;
259 virtual PassRefPtr<JSONArray> asArray() override;
261 unsigned length() const { return m_data.size(); }
263 virtual void writeJSON(StringBuilder* output) const override;
266 virtual ~JSONArrayBase();
268 virtual bool asArray(RefPtr<JSONArray>* output) override;
270 void pushBoolean(bool);
272 void pushNumber(double);
273 void pushString(const String&);
274 void pushValue(PassRefPtr<JSONValue>);
275 void pushObject(PassRefPtr<JSONObject>);
276 void pushArray(PassRefPtr<JSONArray>);
278 PassRefPtr<JSONValue> get(size_t index);
280 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const override;
282 iterator begin() { return m_data.begin(); }
283 iterator end() { return m_data.end(); }
284 const_iterator begin() const { return m_data.begin(); }
285 const_iterator end() const { return m_data.end(); }
291 Vector<RefPtr<JSONValue> > m_data;
294 class PLATFORM_EXPORT JSONArray : public JSONArrayBase {
296 static PassRefPtr<JSONArray> create()
298 return adoptRef(new JSONArray());
301 using JSONArrayBase::asArray;
303 using JSONArrayBase::pushBoolean;
304 using JSONArrayBase::pushInt;
305 using JSONArrayBase::pushNumber;
306 using JSONArrayBase::pushString;
307 using JSONArrayBase::pushValue;
308 using JSONArrayBase::pushObject;
309 using JSONArrayBase::pushArray;
311 using JSONArrayBase::get;
313 using JSONArrayBase::begin;
314 using JSONArrayBase::end;
319 #endif // !defined(JSONValues_h)