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

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: Clone LoggingCanvas log 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 WTF_MAKE_NONCOPYABLE(JSONValue);
56 public: 59 public:
57 static const int maxDepth = 1000; 60 static const int maxDepth = 1000;
58 61
59 virtual ~JSONValue() { } 62 virtual ~JSONValue() { }
60 63
61 static PassRefPtr<JSONValue> null() 64 static std::unique_ptr<JSONValue> null()
62 { 65 {
63 return adoptRef(new JSONValue()); 66 return wrapUnique(new JSONValue());
64 } 67 }
65 68
66 typedef enum { 69 enum ValueType {
67 TypeNull = 0, 70 TypeNull = 0,
68 TypeBoolean, 71 TypeBoolean,
69 TypeNumber, 72 TypeInteger,
73 TypeDouble,
70 TypeString, 74 TypeString,
71 TypeObject, 75 TypeObject,
72 TypeArray 76 TypeArray,
73 } Type; 77 TypeSerialized
78 };
74 79
75 Type getType() const { return m_type; } 80 ValueType type() const { return m_type; }
pfeldman 2016/08/05 04:05:26 We might have to leave this as getType(). See http
iclelland 2016/08/05 05:02:10 Ahh, thanks. I didn't see the history behind that
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;
88 virtual bool asSerialized(String* output) const;
pfeldman 2016/08/05 04:05:26 SerializedValue is inspector-specific, should be r
iclelland 2016/08/05 05:02:10 Thanks. I was hoping that was the case. Done.
86 89
87 String toJSONString() const; 90 String toJSONString() const;
88 String toPrettyJSONString() const; 91 String toPrettyJSONString() const;
89 virtual void writeJSON(StringBuilder* output) const; 92 virtual void writeJSON(StringBuilder* output) const;
90 virtual void prettyWriteJSON(StringBuilder* output) const; 93 virtual void prettyWriteJSON(StringBuilder* output) const;
94 virtual std::unique_ptr<JSONValue> clone() const;
91 95
92 static String quoteString(const String&); 96 static String quoteString(const String&);
93 97
94 protected: 98 protected:
95 JSONValue() : m_type(TypeNull) { } 99 JSONValue() : m_type(TypeNull) { }
96 explicit JSONValue(Type type) : m_type(type) { } 100 explicit JSONValue(ValueType type) : m_type(type) { }
97 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const ; 101 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const ;
98 102
99 private: 103 private:
100 friend class JSONObject; 104 friend class JSONObject;
101 friend class JSONArray; 105 friend class JSONArray;
102 106
103 Type m_type; 107 ValueType m_type;
104 }; 108 };
105 109
106 class PLATFORM_EXPORT JSONBasicValue : public JSONValue { 110 class PLATFORM_EXPORT JSONBasicValue : public JSONValue {
107 public: 111 public:
108 112 static std::unique_ptr<JSONBasicValue> create(bool value)
109 static PassRefPtr<JSONBasicValue> create(bool value)
110 { 113 {
111 return adoptRef(new JSONBasicValue(value)); 114 return wrapUnique(new JSONBasicValue(value));
112 } 115 }
113 116
114 static PassRefPtr<JSONBasicValue> create(int value) 117 static std::unique_ptr<JSONBasicValue> create(int value)
115 { 118 {
116 return adoptRef(new JSONBasicValue(value)); 119 return wrapUnique(new JSONBasicValue(value));
117 } 120 }
118 121
119 static PassRefPtr<JSONBasicValue> create(double value) 122 static std::unique_ptr<JSONBasicValue> create(double value)
120 { 123 {
121 return adoptRef(new JSONBasicValue(value)); 124 return wrapUnique(new JSONBasicValue(value));
122 } 125 }
123 126
124 bool asBoolean(bool* output) const override; 127 bool asBoolean(bool* output) const override;
125 bool asNumber(double* output) const override; 128 bool asDouble(double* output) const override;
126 bool asNumber(long* output) const override; 129 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; 130 void writeJSON(StringBuilder* output) const override;
131 std::unique_ptr<JSONValue> clone() const override;
132 132
133 private: 133 private:
134 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { } 134 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { }
135 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d ouble)value) { } 135 explicit JSONBasicValue(int value) : JSONValue(TypeInteger), m_integerValue( value) { }
136 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue (value) { } 136 explicit JSONBasicValue(double value) : JSONValue(TypeDouble), m_doubleValue (value) { }
137 137
138 union { 138 union {
139 bool m_boolValue; 139 bool m_boolValue;
140 double m_doubleValue; 140 double m_doubleValue;
141 int m_integerValue;
141 }; 142 };
142 }; 143 };
143 144
144 class PLATFORM_EXPORT JSONString : public JSONValue { 145 class PLATFORM_EXPORT JSONString : public JSONValue {
145 public: 146 public:
146 static PassRefPtr<JSONString> create(const String& value) 147 static std::unique_ptr<JSONString> create(const String& value)
147 { 148 {
148 return adoptRef(new JSONString(value)); 149 return wrapUnique(new JSONString(value));
149 } 150 }
150 151
151 static PassRefPtr<JSONString> create(const char* value) 152 static std::unique_ptr<JSONString> create(const char* value)
152 { 153 {
153 return adoptRef(new JSONString(value)); 154 return wrapUnique(new JSONString(value));
154 } 155 }
155 156
156 bool asString(String* output) const override; 157 bool asString(String* output) const override;
157
158 void writeJSON(StringBuilder* output) const override; 158 void writeJSON(StringBuilder* output) const override;
159 std::unique_ptr<JSONValue> clone() const override;
159 160
160 private: 161 private:
161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { } 162 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { }
162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { } 163 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { }
163 164
164 String m_stringValue; 165 String m_stringValue;
165 }; 166 };
166 167
167 class PLATFORM_EXPORT JSONObject : public JSONValue { 168 class PLATFORM_EXPORT SerializedValue : public JSONValue {
pfeldman 2016/08/05 04:05:26 SerializedValue is inspector-specific and should b
iclelland 2016/08/05 05:02:10 Done.
168 private:
169 typedef HashMap<String, RefPtr<JSONValue>> Dictionary;
170
171 public: 169 public:
172 typedef Dictionary::iterator iterator; 170 static std::unique_ptr<SerializedValue> create(const String& value)
173 typedef Dictionary::const_iterator const_iterator;
174
175 static PassRefPtr<JSONObject> create()
176 { 171 {
177 return adoptRef(new JSONObject()); 172 return wrapUnique(new SerializedValue(value));
178 } 173 }
179 174
180 static PassRefPtr<JSONObject> cast(PassRefPtr<JSONValue> value) 175 bool asSerialized(String* output) const override;
176 void writeJSON(StringBuilder* output) const override;
177 std::unique_ptr<JSONValue> clone() const override;
178
179 private:
180 explicit SerializedValue(const String& value) : JSONValue(TypeSerialized), m _serializedValue(value) { }
181 explicit SerializedValue(const char* value) : JSONValue(TypeSerialized), m_s erializedValue(value) { }
182
183 String m_serializedValue;
184 };
185
186 class PLATFORM_EXPORT JSONObject : public JSONValue {
187 public:
188 using Entry = std::pair<String, JSONValue*>;
189 static std::unique_ptr<JSONObject> create()
181 { 190 {
182 if (!value || value->getType() != TypeObject) 191 return wrapUnique(new JSONObject());
192 }
193
194 static JSONObject* cast(JSONValue* value)
195 {
196 if (!value || value->type() != TypeObject)
183 return nullptr; 197 return nullptr;
184 return adoptRef(static_cast<JSONObject*>(value.leakRef())); 198 return static_cast<JSONObject*>(value);
199 }
200
201 static std::unique_ptr<JSONObject> cast(std::unique_ptr<JSONValue> value)
202 {
203 return wrapUnique(JSONObject::cast(value.release()));
185 } 204 }
186 205
187 void writeJSON(StringBuilder* output) const override; 206 void writeJSON(StringBuilder* output) const override;
207 std::unique_ptr<JSONValue> clone() const override;
188 208
189 int size() const { return m_data.size(); } 209 size_t size() const { return m_data.size(); }
190 210
191 void setBoolean(const String& name, bool); 211 void setBoolean(const String& name, bool);
192 void setNumber(const String& name, double); 212 void setInteger(const String& name, int);
213 void setDouble(const String& name, double);
193 void setString(const String& name, const String&); 214 void setString(const String& name, const String&);
194 void setValue(const String& name, PassRefPtr<JSONValue>); 215 void setValue(const String& name, std::unique_ptr<JSONValue>);
195 void setObject(const String& name, PassRefPtr<JSONObject>); 216 void setObject(const String& name, std::unique_ptr<JSONObject>);
196 void setArray(const String& name, PassRefPtr<JSONArray>); 217 void setArray(const String& name, std::unique_ptr<JSONArray>);
197 218
198 iterator find(const String& name);
199 const_iterator find(const String& name) const;
200 bool getBoolean(const String& name, bool* output) const; 219 bool getBoolean(const String& name, bool* output) const;
201 template<class T> bool getNumber(const String& name, T* output) const 220 bool getInteger(const String& name, int* output) const;
202 { 221 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; 222 bool getString(const String& name, String* output) const;
209 PassRefPtr<JSONObject> getObject(const String& name) const; 223
210 PassRefPtr<JSONArray> getArray(const String& name) const; 224 JSONObject* getObject(const String& name) const;
211 PassRefPtr<JSONValue> get(const String& name) const; 225 JSONArray* getArray(const String& name) const;
226 JSONValue* get(const String& name) const;
227 Entry at(size_t index) const;
212 228
213 bool booleanProperty(const String& name, bool defaultValue) const; 229 bool booleanProperty(const String& name, bool defaultValue) const;
214 230 int integerProperty(const String& name, int defaultValue) const;
231 double doubleProperty(const String& name, double defaultValue) const;
215 void remove(const String& name); 232 void remove(const String& name);
216 233
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; 234 ~JSONObject() override;
222 235
223 protected: 236 protected:
224 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; 237 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e;
225 238
226 private: 239 private:
227 JSONObject(); 240 JSONObject();
241 template<typename T>
242 void set(const String& key, std::unique_ptr<T>& value)
243 {
244 DCHECK(value);
245 if (m_data.set(key, std::move(value)).isNewEntry)
246 m_order.append(key);
247 }
228 248
249 using Dictionary = HashMap<String, std::unique_ptr<JSONValue>>;
229 Dictionary m_data; 250 Dictionary m_data;
230 Vector<String> m_order; 251 Vector<String> m_order;
231 }; 252 };
232 253
233 class PLATFORM_EXPORT JSONArray : public JSONValue { 254 class PLATFORM_EXPORT JSONArray : public JSONValue {
234 public: 255 public:
235 typedef Vector<RefPtr<JSONValue>>::iterator iterator; 256 static std::unique_ptr<JSONArray> create()
236 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator;
237
238 static PassRefPtr<JSONArray> create()
239 { 257 {
240 return adoptRef(new JSONArray()); 258 return wrapUnique(new JSONArray());
241 } 259 }
242 260
243 static PassRefPtr<JSONArray> cast(PassRefPtr<JSONValue> value) 261 static JSONArray* cast(JSONValue* value)
244 { 262 {
245 if (!value || value->getType() != TypeArray) 263 if (!value || value->type() != TypeArray)
246 return nullptr; 264 return nullptr;
247 return adoptRef(static_cast<JSONArray*>(value.leakRef())); 265 return static_cast<JSONArray*>(value);
266 }
267
268 static std::unique_ptr<JSONArray> cast(std::unique_ptr<JSONValue> value)
269 {
270 return wrapUnique(JSONArray::cast(value.release()));
248 } 271 }
249 272
250 ~JSONArray() override; 273 ~JSONArray() override;
251 274
252 void writeJSON(StringBuilder* output) const override; 275 void writeJSON(StringBuilder* output) const override;
276 std::unique_ptr<JSONValue> clone() const override;
253 277
254 void pushBoolean(bool); 278 void pushBoolean(bool);
255 void pushInt(int); 279 void pushInteger(int);
256 void pushNumber(double); 280 void pushDouble(double);
257 void pushString(const String&); 281 void pushString(const String&);
258 void pushValue(PassRefPtr<JSONValue>); 282 void pushValue(std::unique_ptr<JSONValue>);
259 void pushObject(PassRefPtr<JSONObject>); 283 void pushObject(std::unique_ptr<JSONObject>);
260 void pushArray(PassRefPtr<JSONArray>); 284 void pushArray(std::unique_ptr<JSONArray>);
261 285
262 PassRefPtr<JSONValue> get(size_t index); 286 JSONValue* at(size_t index);
263 unsigned length() const { return m_data.size(); } 287 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 288
270 protected: 289 protected:
271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; 290 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e;
272 291
273 private: 292 private:
274 JSONArray(); 293 JSONArray();
275 Vector<RefPtr<JSONValue>> m_data; 294 Vector<std::unique_ptr<JSONValue>> m_data;
276 }; 295 };
277 296
278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); 297 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*);
279 void doubleQuoteStringForJSON(const String&, StringBuilder*); 298 void doubleQuoteStringForJSON(const String&, StringBuilder*);
280 299
281 } // namespace blink 300 } // namespace blink
282 301
283 #endif // JSONValues_h 302 #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