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

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

Issue 2218533002: Backporting JSONValues from protocol::Values (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch back to partition allocator Created 4 years, 4 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
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 17 matching lines...) Expand all
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef JSONValues_h 31 #ifndef JSONValues_h
32 #define JSONValues_h 32 #define JSONValues_h
33 33
34 #include "platform/PlatformExport.h" 34 #include "platform/PlatformExport.h"
35 #include "wtf/Allocator.h" 35 #include "wtf/Allocator.h"
36 #include "wtf/Forward.h" 36 #include "wtf/Forward.h"
37 #include "wtf/HashMap.h" 37 #include "wtf/HashMap.h"
38 #include "wtf/RefCounted.h" 38 #include "wtf/Noncopyable.h"
39 #include "wtf/TypeTraits.h" 39 #include "wtf/TypeTraits.h"
40 #include "wtf/Vector.h" 40 #include "wtf/Vector.h"
41 #include "wtf/text/StringHash.h" 41 #include "wtf/text/StringHash.h"
42 #include "wtf/text/WTFString.h" 42 #include "wtf/text/WTFString.h"
43 43
44 #include <memory>
45
44 namespace blink { 46 namespace blink {
45 47
46 class JSONValue; 48 class JSONValue;
47 49
48 } // namespace blink 50 } // namespace blink
49 51
50 namespace blink { 52 namespace blink {
51 53
52 class JSONArray; 54 class JSONArray;
53 class JSONObject; 55 class JSONObject;
54 56
55 class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> { 57 class PLATFORM_EXPORT JSONValue {
58 USING_FAST_MALLOC(JSONValue);
59 WTF_MAKE_NONCOPYABLE(JSONValue);
56 public: 60 public:
57 static const int maxDepth = 1000; 61 static const int maxDepth = 1000;
58 62
59 virtual ~JSONValue() { } 63 virtual ~JSONValue() { }
60 64
61 static PassRefPtr<JSONValue> null() 65 static std::unique_ptr<JSONValue> null()
62 { 66 {
63 return adoptRef(new JSONValue()); 67 return wrapUnique(new JSONValue());
64 } 68 }
65 69
66 typedef enum { 70 enum ValueType {
67 TypeNull = 0, 71 TypeNull = 0,
68 TypeBoolean, 72 TypeBoolean,
69 TypeNumber, 73 TypeInteger,
74 TypeDouble,
70 TypeString, 75 TypeString,
71 TypeObject, 76 TypeObject,
72 TypeArray 77 TypeArray
73 } Type; 78 };
74 79
75 Type getType() const { return m_type; } 80 ValueType getType() const { return m_type; }
76 81
77 bool isNull() const { return m_type == TypeNull; } 82 bool isNull() const { return m_type == TypeNull; }
78 83
79 virtual bool asBoolean(bool* output) const; 84 virtual bool asBoolean(bool* output) const;
80 virtual bool asNumber(double* output) const; 85 virtual bool asDouble(double* output) const;
81 virtual bool asNumber(long* output) const; 86 virtual bool asInteger(int* output) const;
82 virtual bool asNumber(int* output) const;
83 virtual bool asNumber(unsigned long* output) const;
84 virtual bool asNumber(unsigned* output) const;
85 virtual bool asString(String* output) const; 87 virtual bool asString(String* output) const;
86 88
87 String toJSONString() const; 89 String toJSONString() const;
88 String toPrettyJSONString() const; 90 String toPrettyJSONString() const;
89 virtual void writeJSON(StringBuilder* output) const; 91 virtual void writeJSON(StringBuilder* output) const;
90 virtual void prettyWriteJSON(StringBuilder* output) const; 92 virtual void prettyWriteJSON(StringBuilder* output) const;
93 virtual std::unique_ptr<JSONValue> clone() const;
91 94
92 static String quoteString(const String&); 95 static String quoteString(const String&);
93 96
94 protected: 97 protected:
95 JSONValue() : m_type(TypeNull) { } 98 JSONValue() : m_type(TypeNull) { }
96 explicit JSONValue(Type type) : m_type(type) { } 99 explicit JSONValue(ValueType type) : m_type(type) { }
97 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const ; 100 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const ;
98 101
99 private: 102 private:
100 friend class JSONObject; 103 friend class JSONObject;
101 friend class JSONArray; 104 friend class JSONArray;
102 105
103 Type m_type; 106 ValueType m_type;
104 }; 107 };
105 108
106 class PLATFORM_EXPORT JSONBasicValue : public JSONValue { 109 class PLATFORM_EXPORT JSONBasicValue : public JSONValue {
107 public: 110 public:
108 111 static std::unique_ptr<JSONBasicValue> create(bool value)
109 static PassRefPtr<JSONBasicValue> create(bool value)
110 { 112 {
111 return adoptRef(new JSONBasicValue(value)); 113 return wrapUnique(new JSONBasicValue(value));
112 } 114 }
113 115
114 static PassRefPtr<JSONBasicValue> create(int value) 116 static std::unique_ptr<JSONBasicValue> create(int value)
115 { 117 {
116 return adoptRef(new JSONBasicValue(value)); 118 return wrapUnique(new JSONBasicValue(value));
117 } 119 }
118 120
119 static PassRefPtr<JSONBasicValue> create(double value) 121 static std::unique_ptr<JSONBasicValue> create(double value)
120 { 122 {
121 return adoptRef(new JSONBasicValue(value)); 123 return wrapUnique(new JSONBasicValue(value));
122 } 124 }
123 125
124 bool asBoolean(bool* output) const override; 126 bool asBoolean(bool* output) const override;
125 bool asNumber(double* output) const override; 127 bool asDouble(double* output) const override;
126 bool asNumber(long* output) const override; 128 bool asInteger(int* output) const override;
127 bool asNumber(int* output) const override;
128 bool asNumber(unsigned long* output) const override;
129 bool asNumber(unsigned* output) const override;
130
131 void writeJSON(StringBuilder* output) const override; 129 void writeJSON(StringBuilder* output) const override;
130 std::unique_ptr<JSONValue> clone() const override;
132 131
133 private: 132 private:
134 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { } 133 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { }
135 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d ouble)value) { } 134 explicit JSONBasicValue(int value) : JSONValue(TypeInteger), m_integerValue( value) { }
136 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue (value) { } 135 explicit JSONBasicValue(double value) : JSONValue(TypeDouble), m_doubleValue (value) { }
137 136
138 union { 137 union {
139 bool m_boolValue; 138 bool m_boolValue;
140 double m_doubleValue; 139 double m_doubleValue;
140 int m_integerValue;
141 }; 141 };
142 }; 142 };
143 143
144 class PLATFORM_EXPORT JSONString : public JSONValue { 144 class PLATFORM_EXPORT JSONString : public JSONValue {
145 public: 145 public:
146 static PassRefPtr<JSONString> create(const String& value) 146 static std::unique_ptr<JSONString> create(const String& value)
147 { 147 {
148 return adoptRef(new JSONString(value)); 148 return wrapUnique(new JSONString(value));
149 } 149 }
150 150
151 static PassRefPtr<JSONString> create(const char* value) 151 static std::unique_ptr<JSONString> create(const char* value)
152 { 152 {
153 return adoptRef(new JSONString(value)); 153 return wrapUnique(new JSONString(value));
154 } 154 }
155 155
156 bool asString(String* output) const override; 156 bool asString(String* output) const override;
157
158 void writeJSON(StringBuilder* output) const override; 157 void writeJSON(StringBuilder* output) const override;
158 std::unique_ptr<JSONValue> clone() const override;
159 159
160 private: 160 private:
161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { } 161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { }
162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { } 162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { }
163 163
164 String m_stringValue; 164 String m_stringValue;
165 }; 165 };
166 166
167 class PLATFORM_EXPORT JSONObject : public JSONValue { 167 class PLATFORM_EXPORT JSONObject : public JSONValue {
168 private:
169 typedef HashMap<String, RefPtr<JSONValue>> Dictionary;
170
171 public: 168 public:
172 typedef Dictionary::iterator iterator; 169 using Entry = std::pair<String, JSONValue*>;
173 typedef Dictionary::const_iterator const_iterator; 170 static std::unique_ptr<JSONObject> create()
174
175 static PassRefPtr<JSONObject> create()
176 { 171 {
177 return adoptRef(new JSONObject()); 172 return wrapUnique(new JSONObject());
178 } 173 }
179 174
180 static PassRefPtr<JSONObject> cast(PassRefPtr<JSONValue> value) 175 static JSONObject* cast(JSONValue* value)
181 { 176 {
182 if (!value || value->getType() != TypeObject) 177 if (!value || value->getType() != TypeObject)
183 return nullptr; 178 return nullptr;
184 return adoptRef(static_cast<JSONObject*>(value.leakRef())); 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 } 185 }
186 186
187 void writeJSON(StringBuilder* output) const override; 187 void writeJSON(StringBuilder* output) const override;
188 std::unique_ptr<JSONValue> clone() const override;
188 189
189 int size() const { return m_data.size(); } 190 size_t size() const { return m_data.size(); }
190 191
191 void setBoolean(const String& name, bool); 192 void setBoolean(const String& name, bool);
192 void setNumber(const String& name, double); 193 void setInteger(const String& name, int);
194 void setDouble(const String& name, double);
193 void setString(const String& name, const String&); 195 void setString(const String& name, const String&);
194 void setValue(const String& name, PassRefPtr<JSONValue>); 196 void setValue(const String& name, std::unique_ptr<JSONValue>);
195 void setObject(const String& name, PassRefPtr<JSONObject>); 197 void setObject(const String& name, std::unique_ptr<JSONObject>);
196 void setArray(const String& name, PassRefPtr<JSONArray>); 198 void setArray(const String& name, std::unique_ptr<JSONArray>);
197 199
198 iterator find(const String& name);
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 bool getInteger(const String& name, int* output) const;
202 { 202 bool getDouble(const String& name, double* output) const;
203 RefPtr<JSONValue> value = get(name);
204 if (!value)
205 return false;
206 return value->asNumber(output);
207 }
208 bool getString(const String& name, String* output) const; 203 bool getString(const String& name, String* output) const;
209 PassRefPtr<JSONObject> getObject(const String& name) const; 204
210 PassRefPtr<JSONArray> getArray(const String& name) const; 205 JSONObject* getObject(const String& name) const;
211 PassRefPtr<JSONValue> get(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;
212 209
213 bool booleanProperty(const String& name, bool defaultValue) const; 210 bool booleanProperty(const String& name, bool defaultValue) const;
214 211 int integerProperty(const String& name, int defaultValue) const;
212 double doubleProperty(const String& name, double defaultValue) const;
215 void remove(const String& name); 213 void remove(const String& name);
216 214
217 iterator begin() { return m_data.begin(); }
218 iterator end() { return m_data.end(); }
219 const_iterator begin() const { return m_data.begin(); }
220 const_iterator end() const { return m_data.end(); }
221 ~JSONObject() override; 215 ~JSONObject() override;
222 216
223 protected: 217 protected:
224 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; 218 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e;
225 219
226 private: 220 private:
227 JSONObject(); 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 }
228 229
230 using Dictionary = HashMap<String, std::unique_ptr<JSONValue>>;
229 Dictionary m_data; 231 Dictionary m_data;
230 Vector<String> m_order; 232 Vector<String> m_order;
231 }; 233 };
232 234
233 class PLATFORM_EXPORT JSONArray : public JSONValue { 235 class PLATFORM_EXPORT JSONArray : public JSONValue {
234 public: 236 public:
235 typedef Vector<RefPtr<JSONValue>>::iterator iterator; 237 static std::unique_ptr<JSONArray> create()
236 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator;
237
238 static PassRefPtr<JSONArray> create()
239 { 238 {
240 return adoptRef(new JSONArray()); 239 return wrapUnique(new JSONArray());
241 } 240 }
242 241
243 static PassRefPtr<JSONArray> cast(PassRefPtr<JSONValue> value) 242 static JSONArray* cast(JSONValue* value)
244 { 243 {
245 if (!value || value->getType() != TypeArray) 244 if (!value || value->getType() != TypeArray)
246 return nullptr; 245 return nullptr;
247 return adoptRef(static_cast<JSONArray*>(value.leakRef())); 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()));
248 } 252 }
249 253
250 ~JSONArray() override; 254 ~JSONArray() override;
251 255
252 void writeJSON(StringBuilder* output) const override; 256 void writeJSON(StringBuilder* output) const override;
257 std::unique_ptr<JSONValue> clone() const override;
253 258
254 void pushBoolean(bool); 259 void pushBoolean(bool);
255 void pushInt(int); 260 void pushInteger(int);
256 void pushNumber(double); 261 void pushDouble(double);
257 void pushString(const String&); 262 void pushString(const String&);
258 void pushValue(PassRefPtr<JSONValue>); 263 void pushValue(std::unique_ptr<JSONValue>);
259 void pushObject(PassRefPtr<JSONObject>); 264 void pushObject(std::unique_ptr<JSONObject>);
260 void pushArray(PassRefPtr<JSONArray>); 265 void pushArray(std::unique_ptr<JSONArray>);
261 266
262 PassRefPtr<JSONValue> get(size_t index); 267 JSONValue* at(size_t index);
263 unsigned length() const { return m_data.size(); } 268 size_t size() const { return m_data.size(); }
264
265 iterator begin() { return m_data.begin(); }
266 iterator end() { return m_data.end(); }
267 const_iterator begin() const { return m_data.begin(); }
268 const_iterator end() const { return m_data.end(); }
269 269
270 protected: 270 protected:
271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; 271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e;
272 272
273 private: 273 private:
274 JSONArray(); 274 JSONArray();
275 Vector<RefPtr<JSONValue>> m_data; 275 Vector<std::unique_ptr<JSONValue>> m_data;
276 }; 276 };
277 277
278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); 278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*);
279 void doubleQuoteStringForJSON(const String&, StringBuilder*); 279 void doubleQuoteStringForJSON(const String&, StringBuilder*);
280 280
281 } // namespace blink 281 } // namespace blink
282 282
283 #endif // JSONValues_h 283 #endif // JSONValues_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/payments/PaymentRequest.cpp ('k') | third_party/WebKit/Source/platform/JSONValues.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698