Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: third_party/WebKit/Source/platform/JSONValues.h

Issue 2293493003: Move blink platform's JSON code into a directory. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #ifndef JSONValues_h
32 #define JSONValues_h
33
34 #include "platform/PlatformExport.h"
35 #include "wtf/Allocator.h"
36 #include "wtf/Forward.h"
37 #include "wtf/HashMap.h"
38 #include "wtf/Noncopyable.h"
39 #include "wtf/TypeTraits.h"
40 #include "wtf/Vector.h"
41 #include "wtf/text/StringHash.h"
42 #include "wtf/text/WTFString.h"
43
44 #include <memory>
45
46 namespace blink {
47
48 class JSONValue;
49
50 } // namespace blink
51
52 namespace blink {
53
54 class JSONArray;
55 class JSONObject;
56
57 class PLATFORM_EXPORT JSONValue {
58 USING_FAST_MALLOC(JSONValue);
59 WTF_MAKE_NONCOPYABLE(JSONValue);
60 public:
61 static const int maxDepth = 1000;
62
63 virtual ~JSONValue() { }
64
65 static std::unique_ptr<JSONValue> null()
66 {
67 return wrapUnique(new JSONValue());
68 }
69
70 enum ValueType {
71 TypeNull = 0,
72 TypeBoolean,
73 TypeInteger,
74 TypeDouble,
75 TypeString,
76 TypeObject,
77 TypeArray
78 };
79
80 ValueType getType() const { return m_type; }
81
82 bool isNull() const { return m_type == TypeNull; }
83
84 virtual bool asBoolean(bool* output) const;
85 virtual bool asDouble(double* output) const;
86 virtual bool asInteger(int* output) const;
87 virtual bool asString(String* output) const;
88
89 String toJSONString() const;
90 String toPrettyJSONString() const;
91 virtual void writeJSON(StringBuilder* output) const;
92 virtual void prettyWriteJSON(StringBuilder* output) const;
93 virtual std::unique_ptr<JSONValue> clone() const;
94
95 static String quoteString(const String&);
96
97 protected:
98 JSONValue() : m_type(TypeNull) { }
99 explicit JSONValue(ValueType type) : m_type(type) { }
100 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const ;
101
102 private:
103 friend class JSONObject;
104 friend class JSONArray;
105
106 ValueType m_type;
107 };
108
109 class PLATFORM_EXPORT JSONBasicValue : public JSONValue {
110 public:
111 static std::unique_ptr<JSONBasicValue> create(bool value)
112 {
113 return wrapUnique(new JSONBasicValue(value));
114 }
115
116 static std::unique_ptr<JSONBasicValue> create(int value)
117 {
118 return wrapUnique(new JSONBasicValue(value));
119 }
120
121 static std::unique_ptr<JSONBasicValue> create(double value)
122 {
123 return wrapUnique(new JSONBasicValue(value));
124 }
125
126 bool asBoolean(bool* output) const override;
127 bool asDouble(double* output) const override;
128 bool asInteger(int* output) const override;
129 void writeJSON(StringBuilder* output) const override;
130 std::unique_ptr<JSONValue> clone() const override;
131
132 private:
133 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { }
134 explicit JSONBasicValue(int value) : JSONValue(TypeInteger), m_integerValue( value) { }
135 explicit JSONBasicValue(double value) : JSONValue(TypeDouble), m_doubleValue (value) { }
136
137 union {
138 bool m_boolValue;
139 double m_doubleValue;
140 int m_integerValue;
141 };
142 };
143
144 class PLATFORM_EXPORT JSONString : public JSONValue {
145 public:
146 static std::unique_ptr<JSONString> create(const String& value)
147 {
148 return wrapUnique(new JSONString(value));
149 }
150
151 static std::unique_ptr<JSONString> create(const char* value)
152 {
153 return wrapUnique(new JSONString(value));
154 }
155
156 bool asString(String* output) const override;
157 void writeJSON(StringBuilder* output) const override;
158 std::unique_ptr<JSONValue> clone() const override;
159
160 private:
161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { }
162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { }
163
164 String m_stringValue;
165 };
166
167 class PLATFORM_EXPORT JSONObject : public JSONValue {
168 public:
169 using Entry = std::pair<String, JSONValue*>;
170 static std::unique_ptr<JSONObject> create()
171 {
172 return wrapUnique(new JSONObject());
173 }
174
175 static JSONObject* cast(JSONValue* value)
176 {
177 if (!value || value->getType() != TypeObject)
178 return nullptr;
179 return static_cast<JSONObject*>(value);
180 }
181
182 static std::unique_ptr<JSONObject> cast(std::unique_ptr<JSONValue> value)
183 {
184 return wrapUnique(JSONObject::cast(value.release()));
185 }
186
187 void writeJSON(StringBuilder* output) const override;
188 std::unique_ptr<JSONValue> clone() const override;
189
190 size_t size() const { return m_data.size(); }
191
192 void setBoolean(const String& name, bool);
193 void setInteger(const String& name, int);
194 void setDouble(const String& name, double);
195 void setString(const String& name, const String&);
196 void setValue(const String& name, std::unique_ptr<JSONValue>);
197 void setObject(const String& name, std::unique_ptr<JSONObject>);
198 void setArray(const String& name, std::unique_ptr<JSONArray>);
199
200 bool getBoolean(const String& name, bool* output) const;
201 bool getInteger(const String& name, int* output) const;
202 bool getDouble(const String& name, double* output) const;
203 bool getString(const String& name, String* output) const;
204
205 JSONObject* getObject(const String& name) const;
206 JSONArray* getArray(const String& name) const;
207 JSONValue* get(const String& name) const;
208 Entry at(size_t index) const;
209
210 bool booleanProperty(const String& name, bool defaultValue) const;
211 int integerProperty(const String& name, int defaultValue) const;
212 double doubleProperty(const String& name, double defaultValue) const;
213 void remove(const String& name);
214
215 ~JSONObject() override;
216
217 protected:
218 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e;
219
220 private:
221 JSONObject();
222 template<typename T>
223 void set(const String& key, std::unique_ptr<T>& value)
224 {
225 DCHECK(value);
226 if (m_data.set(key, std::move(value)).isNewEntry)
227 m_order.append(key);
228 }
229
230 using Dictionary = HashMap<String, std::unique_ptr<JSONValue>>;
231 Dictionary m_data;
232 Vector<String> m_order;
233 };
234
235 class PLATFORM_EXPORT JSONArray : public JSONValue {
236 public:
237 static std::unique_ptr<JSONArray> create()
238 {
239 return wrapUnique(new JSONArray());
240 }
241
242 static JSONArray* cast(JSONValue* value)
243 {
244 if (!value || value->getType() != TypeArray)
245 return nullptr;
246 return static_cast<JSONArray*>(value);
247 }
248
249 static std::unique_ptr<JSONArray> cast(std::unique_ptr<JSONValue> value)
250 {
251 return wrapUnique(JSONArray::cast(value.release()));
252 }
253
254 ~JSONArray() override;
255
256 void writeJSON(StringBuilder* output) const override;
257 std::unique_ptr<JSONValue> clone() const override;
258
259 void pushBoolean(bool);
260 void pushInteger(int);
261 void pushDouble(double);
262 void pushString(const String&);
263 void pushValue(std::unique_ptr<JSONValue>);
264 void pushObject(std::unique_ptr<JSONObject>);
265 void pushArray(std::unique_ptr<JSONArray>);
266
267 JSONValue* at(size_t index);
268 size_t size() const { return m_data.size(); }
269
270 protected:
271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e;
272
273 private:
274 JSONArray();
275 Vector<std::unique_ptr<JSONValue>> m_data;
276 };
277
278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*);
279 void doubleQuoteStringForJSON(const String&, StringBuilder*);
280
281 } // namespace blink
282
283 #endif // JSONValues_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/JSONParserTest.cpp ('k') | third_party/WebKit/Source/platform/JSONValues.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698