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/TypeTraits.h"
39 #include "wtf/Vector.h"
40 #include "wtf/text/StringHash.h"
41 #include "wtf/text/WTFString.h"
53 // FIXME: Avoid the need for this global upcasting to JSONValue (for PassRefPtr<T>.)
54 // The current CodeGeneratorInspector.py generates code which order sorts its input
55 // types and generates forward declarations where needed. But with inline uses
56 // of setValue(PassRefPtr<JSONValue>) this is not quite sufficient for the
57 // implicit conversion of PassRefPtr<T> to PassRefPtr<JSONValue> for a T that
58 // has only been forward declared -- IsPointerConvertible<> doesn't have
59 // complete types to work with.
61 // Work around that problem here by hackily declaring this global & unsafe
64 // (InspectorTypeBuilder.h is the only piece of code that relies on this specialization.)
65 template<typename From
> class IsPointerConvertible
<From
, blink::JSONValue
> {
79 class PLATFORM_EXPORT JSONValue
: public RefCounted
<JSONValue
> {
81 static const int maxDepth
= 1000;
83 JSONValue() : m_type(TypeNull
) { }
84 virtual ~JSONValue() { }
86 static PassRefPtr
<JSONValue
> null()
88 return adoptRef(new JSONValue());
100 Type
type() const { return m_type
; }
102 bool isNull() const { return m_type
== TypeNull
; }
104 virtual bool asBoolean(bool* output
) const;
105 virtual bool asNumber(double* output
) const;
106 virtual bool asNumber(long* output
) const;
107 virtual bool asNumber(int* output
) const;
108 virtual bool asNumber(unsigned long* output
) const;
109 virtual bool asNumber(unsigned* output
) const;
110 virtual bool asString(String
* output
) const;
111 virtual bool asObject(RefPtr
<JSONObject
>* output
);
112 virtual bool asArray(RefPtr
<JSONArray
>* output
);
113 virtual PassRefPtr
<JSONObject
> asObject();
114 virtual PassRefPtr
<JSONArray
> asArray();
116 String
toJSONString() const;
117 String
toPrettyJSONString() const;
118 virtual void writeJSON(StringBuilder
* output
) const;
119 virtual void prettyWriteJSON(StringBuilder
* output
) const;
121 static String
quoteString(const String
&);
124 explicit JSONValue(Type type
) : m_type(type
) { }
125 virtual void prettyWriteJSONInternal(StringBuilder
* output
, int depth
) const;
128 friend class JSONObjectBase
;
129 friend class JSONArrayBase
;
134 class PLATFORM_EXPORT JSONBasicValue
: public JSONValue
{
137 static PassRefPtr
<JSONBasicValue
> create(bool value
)
139 return adoptRef(new JSONBasicValue(value
));
142 static PassRefPtr
<JSONBasicValue
> create(int value
)
144 return adoptRef(new JSONBasicValue(value
));
147 static PassRefPtr
<JSONBasicValue
> create(double value
)
149 return adoptRef(new JSONBasicValue(value
));
152 bool asBoolean(bool* output
) const override
;
153 bool asNumber(double* output
) const override
;
154 bool asNumber(long* output
) const override
;
155 bool asNumber(int* output
) const override
;
156 bool asNumber(unsigned long* output
) const override
;
157 bool asNumber(unsigned* output
) const override
;
159 void writeJSON(StringBuilder
* output
) const override
;
162 explicit JSONBasicValue(bool value
) : JSONValue(TypeBoolean
), m_boolValue(value
) { }
163 explicit JSONBasicValue(int value
) : JSONValue(TypeNumber
), m_doubleValue((double)value
) { }
164 explicit JSONBasicValue(double value
) : JSONValue(TypeNumber
), m_doubleValue(value
) { }
168 double m_doubleValue
;
172 class PLATFORM_EXPORT JSONString
: public JSONValue
{
174 static PassRefPtr
<JSONString
> create(const String
& value
)
176 return adoptRef(new JSONString(value
));
179 static PassRefPtr
<JSONString
> create(const char* value
)
181 return adoptRef(new JSONString(value
));
184 bool asString(String
* output
) const override
;
186 void writeJSON(StringBuilder
* output
) const override
;
189 explicit JSONString(const String
& value
) : JSONValue(TypeString
), m_stringValue(value
) { }
190 explicit JSONString(const char* value
) : JSONValue(TypeString
), m_stringValue(value
) { }
192 String m_stringValue
;
195 class PLATFORM_EXPORT JSONObjectBase
: public JSONValue
{
197 typedef HashMap
<String
, RefPtr
<JSONValue
>> Dictionary
;
200 typedef Dictionary::iterator iterator
;
201 typedef Dictionary::const_iterator const_iterator
;
203 PassRefPtr
<JSONObject
> asObject() override
;
204 JSONObject
* openAccessors();
206 void writeJSON(StringBuilder
* output
) const override
;
208 int size() const { return m_data
.size(); }
211 ~JSONObjectBase() override
;
213 bool asObject(RefPtr
<JSONObject
>* output
) override
;
215 void setBoolean(const String
& name
, bool);
216 void setNumber(const String
& name
, double);
217 void setString(const String
& name
, const String
&);
218 void setValue(const String
& name
, PassRefPtr
<JSONValue
>);
219 void setObject(const String
& name
, PassRefPtr
<JSONObject
>);
220 void setArray(const String
& name
, PassRefPtr
<JSONArray
>);
222 iterator
find(const String
& name
);
223 const_iterator
find(const String
& name
) const;
224 bool getBoolean(const String
& name
, bool* output
) const;
225 template<class T
> bool getNumber(const String
& name
, T
* output
) const
227 RefPtr
<JSONValue
> value
= get(name
);
230 return value
->asNumber(output
);
232 bool getString(const String
& name
, String
* output
) const;
233 PassRefPtr
<JSONObject
> getObject(const String
& name
) const;
234 PassRefPtr
<JSONArray
> getArray(const String
& name
) const;
235 PassRefPtr
<JSONValue
> get(const String
& name
) const;
237 void remove(const String
& name
);
239 void prettyWriteJSONInternal(StringBuilder
* output
, int depth
) const override
;
241 iterator
begin() { return m_data
.begin(); }
242 iterator
end() { return m_data
.end(); }
243 const_iterator
begin() const { return m_data
.begin(); }
244 const_iterator
end() const { return m_data
.end(); }
251 Vector
<String
> m_order
;
254 class PLATFORM_EXPORT JSONObject
: public JSONObjectBase
{
256 static PassRefPtr
<JSONObject
> create()
258 return adoptRef(new JSONObject());
261 using JSONObjectBase::asObject
;
263 using JSONObjectBase::setBoolean
;
264 using JSONObjectBase::setNumber
;
265 using JSONObjectBase::setString
;
266 using JSONObjectBase::setValue
;
267 using JSONObjectBase::setObject
;
268 using JSONObjectBase::setArray
;
270 using JSONObjectBase::find
;
271 using JSONObjectBase::getBoolean
;
272 using JSONObjectBase::getNumber
;
273 using JSONObjectBase::getString
;
274 using JSONObjectBase::getObject
;
275 using JSONObjectBase::getArray
;
276 using JSONObjectBase::get
;
278 using JSONObjectBase::remove
;
280 using JSONObjectBase::begin
;
281 using JSONObjectBase::end
;
283 using JSONObjectBase::size
;
287 class PLATFORM_EXPORT JSONArrayBase
: public JSONValue
{
289 typedef Vector
<RefPtr
<JSONValue
>>::iterator iterator
;
290 typedef Vector
<RefPtr
<JSONValue
>>::const_iterator const_iterator
;
292 PassRefPtr
<JSONArray
> asArray() override
;
294 unsigned length() const { return m_data
.size(); }
296 void writeJSON(StringBuilder
* output
) const override
;
299 ~JSONArrayBase() override
;
301 bool asArray(RefPtr
<JSONArray
>* output
) override
;
303 void pushBoolean(bool);
305 void pushNumber(double);
306 void pushString(const String
&);
307 void pushValue(PassRefPtr
<JSONValue
>);
308 void pushObject(PassRefPtr
<JSONObject
>);
309 void pushArray(PassRefPtr
<JSONArray
>);
311 PassRefPtr
<JSONValue
> get(size_t index
);
313 void prettyWriteJSONInternal(StringBuilder
* output
, int depth
) const override
;
315 iterator
begin() { return m_data
.begin(); }
316 iterator
end() { return m_data
.end(); }
317 const_iterator
begin() const { return m_data
.begin(); }
318 const_iterator
end() const { return m_data
.end(); }
324 Vector
<RefPtr
<JSONValue
>> m_data
;
327 class PLATFORM_EXPORT JSONArray
: public JSONArrayBase
{
329 static PassRefPtr
<JSONArray
> create()
331 return adoptRef(new JSONArray());
334 using JSONArrayBase::asArray
;
336 using JSONArrayBase::pushBoolean
;
337 using JSONArrayBase::pushInt
;
338 using JSONArrayBase::pushNumber
;
339 using JSONArrayBase::pushString
;
340 using JSONArrayBase::pushValue
;
341 using JSONArrayBase::pushObject
;
342 using JSONArrayBase::pushArray
;
344 using JSONArrayBase::get
;
346 using JSONArrayBase::begin
;
347 using JSONArrayBase::end
;
350 void doubleQuoteStringForJSON(const String
&, StringBuilder
*);
354 #endif // JSONValues_h