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 virtual void writeJSON(StringBuilder* output) const;
89 explicit JSONValue(Type type) : m_type(type) { }
95 class PLATFORM_EXPORT JSONBasicValue : public JSONValue {
98 static PassRefPtr<JSONBasicValue> create(bool value)
100 return adoptRef(new JSONBasicValue(value));
103 static PassRefPtr<JSONBasicValue> create(int value)
105 return adoptRef(new JSONBasicValue(value));
108 static PassRefPtr<JSONBasicValue> create(double value)
110 return adoptRef(new JSONBasicValue(value));
113 virtual bool asBoolean(bool* output) const OVERRIDE;
114 virtual bool asNumber(double* output) const OVERRIDE;
115 virtual bool asNumber(long* output) const OVERRIDE;
116 virtual bool asNumber(int* output) const OVERRIDE;
117 virtual bool asNumber(unsigned long* output) const OVERRIDE;
118 virtual bool asNumber(unsigned* output) const OVERRIDE;
120 virtual void writeJSON(StringBuilder* output) const OVERRIDE;
123 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(value) { }
124 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((double)value) { }
125 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue(value) { }
129 double m_doubleValue;
133 class PLATFORM_EXPORT JSONString : public JSONValue {
135 static PassRefPtr<JSONString> create(const String& value)
137 return adoptRef(new JSONString(value));
140 static PassRefPtr<JSONString> create(const char* value)
142 return adoptRef(new JSONString(value));
145 virtual bool asString(String* output) const OVERRIDE;
147 virtual void writeJSON(StringBuilder* output) const OVERRIDE;
150 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringValue(value) { }
151 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValue(value) { }
153 String m_stringValue;
156 class PLATFORM_EXPORT JSONObjectBase : public JSONValue {
158 typedef HashMap<String, RefPtr<JSONValue> > Dictionary;
161 typedef Dictionary::iterator iterator;
162 typedef Dictionary::const_iterator const_iterator;
164 virtual PassRefPtr<JSONObject> asObject() OVERRIDE;
165 JSONObject* openAccessors();
168 virtual ~JSONObjectBase();
170 virtual bool asObject(RefPtr<JSONObject>* output) OVERRIDE;
172 void setBoolean(const String& name, bool);
173 void setNumber(const String& name, double);
174 void setString(const String& name, const String&);
175 void setValue(const String& name, PassRefPtr<JSONValue>);
176 void setObject(const String& name, PassRefPtr<JSONObject>);
177 void setArray(const String& name, PassRefPtr<JSONArray>);
179 iterator find(const String& name);
180 const_iterator find(const String& name) const;
181 bool getBoolean(const String& name, bool* output) const;
182 template<class T> bool getNumber(const String& name, T* output) const
184 RefPtr<JSONValue> value = get(name);
187 return value->asNumber(output);
189 bool getString(const String& name, String* output) const;
190 PassRefPtr<JSONObject> getObject(const String& name) const;
191 PassRefPtr<JSONArray> getArray(const String& name) const;
192 PassRefPtr<JSONValue> get(const String& name) const;
194 void remove(const String& name);
196 virtual void writeJSON(StringBuilder* output) const OVERRIDE;
198 iterator begin() { return m_data.begin(); }
199 iterator end() { return m_data.end(); }
200 const_iterator begin() const { return m_data.begin(); }
201 const_iterator end() const { return m_data.end(); }
203 int size() const { return m_data.size(); }
210 Vector<String> m_order;
213 class PLATFORM_EXPORT JSONObject : public JSONObjectBase {
215 static PassRefPtr<JSONObject> create()
217 return adoptRef(new JSONObject());
220 using JSONObjectBase::asObject;
222 using JSONObjectBase::setBoolean;
223 using JSONObjectBase::setNumber;
224 using JSONObjectBase::setString;
225 using JSONObjectBase::setValue;
226 using JSONObjectBase::setObject;
227 using JSONObjectBase::setArray;
229 using JSONObjectBase::find;
230 using JSONObjectBase::getBoolean;
231 using JSONObjectBase::getNumber;
232 using JSONObjectBase::getString;
233 using JSONObjectBase::getObject;
234 using JSONObjectBase::getArray;
235 using JSONObjectBase::get;
237 using JSONObjectBase::remove;
239 using JSONObjectBase::begin;
240 using JSONObjectBase::end;
242 using JSONObjectBase::size;
246 class PLATFORM_EXPORT JSONArrayBase : public JSONValue {
248 typedef Vector<RefPtr<JSONValue> >::iterator iterator;
249 typedef Vector<RefPtr<JSONValue> >::const_iterator const_iterator;
251 virtual PassRefPtr<JSONArray> asArray() OVERRIDE;
253 unsigned length() const { return m_data.size(); }
256 virtual ~JSONArrayBase();
258 virtual bool asArray(RefPtr<JSONArray>* output) OVERRIDE;
260 void pushBoolean(bool);
262 void pushNumber(double);
263 void pushString(const String&);
264 void pushValue(PassRefPtr<JSONValue>);
265 void pushObject(PassRefPtr<JSONObject>);
266 void pushArray(PassRefPtr<JSONArray>);
268 PassRefPtr<JSONValue> get(size_t index);
270 virtual void writeJSON(StringBuilder* output) const OVERRIDE;
272 iterator begin() { return m_data.begin(); }
273 iterator end() { return m_data.end(); }
274 const_iterator begin() const { return m_data.begin(); }
275 const_iterator end() const { return m_data.end(); }
281 Vector<RefPtr<JSONValue> > m_data;
284 class PLATFORM_EXPORT JSONArray : public JSONArrayBase {
286 static PassRefPtr<JSONArray> create()
288 return adoptRef(new JSONArray());
291 using JSONArrayBase::asArray;
293 using JSONArrayBase::pushBoolean;
294 using JSONArrayBase::pushInt;
295 using JSONArrayBase::pushNumber;
296 using JSONArrayBase::pushString;
297 using JSONArrayBase::pushValue;
298 using JSONArrayBase::pushObject;
299 using JSONArrayBase::pushArray;
301 using JSONArrayBase::get;
303 using JSONArrayBase::begin;
304 using JSONArrayBase::end;
307 } // namespace WebCore
309 #endif // !defined(JSONValues_h)