Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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); | |
|
jbroman
2016/08/04 15:17:30
You may also want USING_FAST_MALLOC(JSONValue), as
| |
| 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 typedef enum { |
| 67 TypeNull = 0, | 70 TypeNull = 0, |
| 68 TypeBoolean, | 71 TypeBoolean, |
| 69 TypeNumber, | 72 TypeNumber, |
| 70 TypeString, | 73 TypeString, |
| 71 TypeObject, | 74 TypeObject, |
| 72 TypeArray | 75 TypeArray |
| 73 } Type; | 76 } Type; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 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 Type 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 |
| 109 static PassRefPtr<JSONBasicValue> create(bool value) | 112 static std::unique_ptr<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 asNumber(double* output) const override; |
| 126 bool asNumber(long* output) const override; | 129 bool asNumber(long* output) const override; |
| 127 bool asNumber(int* output) const override; | 130 bool asNumber(int* output) const override; |
| 128 bool asNumber(unsigned long* output) const override; | 131 bool asNumber(unsigned long* output) const override; |
| 129 bool asNumber(unsigned* output) const override; | 132 bool asNumber(unsigned* output) const override; |
| 130 | 133 |
| 131 void writeJSON(StringBuilder* output) const override; | 134 void writeJSON(StringBuilder* output) const override; |
| 132 | 135 |
| 133 private: | 136 private: |
| 134 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { } | 137 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va lue) { } |
| 135 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d ouble)value) { } | 138 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d ouble)value) { } |
| 136 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue (value) { } | 139 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue (value) { } |
| 137 | 140 |
| 138 union { | 141 union { |
| 139 bool m_boolValue; | 142 bool m_boolValue; |
| 140 double m_doubleValue; | 143 double m_doubleValue; |
| 141 }; | 144 }; |
| 142 }; | 145 }; |
| 143 | 146 |
| 144 class PLATFORM_EXPORT JSONString : public JSONValue { | 147 class PLATFORM_EXPORT JSONString : public JSONValue { |
| 145 public: | 148 public: |
| 146 static PassRefPtr<JSONString> create(const String& value) | 149 static std::unique_ptr<JSONString> create(const String& value) |
| 147 { | 150 { |
| 148 return adoptRef(new JSONString(value)); | 151 return wrapUnique(new JSONString(value)); |
| 149 } | 152 } |
| 150 | 153 |
| 151 static PassRefPtr<JSONString> create(const char* value) | 154 static std::unique_ptr<JSONString> create(const char* value) |
| 152 { | 155 { |
| 153 return adoptRef(new JSONString(value)); | 156 return wrapUnique(new JSONString(value)); |
| 154 } | 157 } |
| 155 | 158 |
| 156 bool asString(String* output) const override; | 159 bool asString(String* output) const override; |
| 157 | 160 |
| 158 void writeJSON(StringBuilder* output) const override; | 161 void writeJSON(StringBuilder* output) const override; |
| 159 | 162 |
| 160 private: | 163 private: |
| 161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { } | 164 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa lue(value) { } |
| 162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { } | 165 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu e(value) { } |
| 163 | 166 |
| 164 String m_stringValue; | 167 String m_stringValue; |
| 165 }; | 168 }; |
| 166 | 169 |
| 167 class PLATFORM_EXPORT JSONObject : public JSONValue { | 170 class PLATFORM_EXPORT JSONObject : public JSONValue { |
| 168 private: | 171 private: |
| 169 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; | 172 typedef HashMap<String, std::unique_ptr<JSONValue>> Dictionary; |
| 170 | 173 |
| 171 public: | 174 public: |
| 172 typedef Dictionary::iterator iterator; | 175 typedef Dictionary::iterator iterator; |
| 173 typedef Dictionary::const_iterator const_iterator; | 176 typedef Dictionary::const_iterator const_iterator; |
| 174 | 177 |
| 175 static PassRefPtr<JSONObject> create() | 178 static std::unique_ptr<JSONObject> create() |
| 176 { | 179 { |
| 177 return adoptRef(new JSONObject()); | 180 return wrapUnique(new JSONObject()); |
| 178 } | 181 } |
| 179 | 182 |
| 180 static PassRefPtr<JSONObject> cast(PassRefPtr<JSONValue> value) | 183 static JSONObject* cast(JSONValue* value) |
| 181 { | 184 { |
| 182 if (!value || value->getType() != TypeObject) | 185 if (!value || value->getType() != TypeObject) |
| 183 return nullptr; | 186 return nullptr; |
| 184 return adoptRef(static_cast<JSONObject*>(value.leakRef())); | 187 return static_cast<JSONObject*>(value); |
| 188 } | |
| 189 | |
| 190 static std::unique_ptr<JSONObject> cast(std::unique_ptr<JSONValue> value) | |
| 191 { | |
| 192 return wrapUnique(JSONObject::cast(value.release())); | |
| 185 } | 193 } |
| 186 | 194 |
| 187 void writeJSON(StringBuilder* output) const override; | 195 void writeJSON(StringBuilder* output) const override; |
| 188 | 196 |
| 189 int size() const { return m_data.size(); } | 197 int size() const { return m_data.size(); } |
| 190 | 198 |
| 191 void setBoolean(const String& name, bool); | 199 void setBoolean(const String& name, bool); |
| 192 void setNumber(const String& name, double); | 200 void setNumber(const String& name, double); |
| 193 void setString(const String& name, const String&); | 201 void setString(const String& name, const String&); |
| 194 void setValue(const String& name, PassRefPtr<JSONValue>); | 202 void setValue(const String& name, std::unique_ptr<JSONValue>); |
| 195 void setObject(const String& name, PassRefPtr<JSONObject>); | 203 void setObject(const String& name, std::unique_ptr<JSONObject>); |
| 196 void setArray(const String& name, PassRefPtr<JSONArray>); | 204 void setArray(const String& name, std::unique_ptr<JSONArray>); |
| 197 | 205 |
| 198 iterator find(const String& name); | 206 iterator find(const String& name); |
| 199 const_iterator find(const String& name) const; | 207 const_iterator find(const String& name) const; |
| 200 bool getBoolean(const String& name, bool* output) const; | 208 bool getBoolean(const String& name, bool* output) const; |
| 201 template<class T> bool getNumber(const String& name, T* output) const | 209 template<class T> bool getNumber(const String& name, T* output) const |
| 202 { | 210 { |
| 203 RefPtr<JSONValue> value = get(name); | 211 JSONValue* value = get(name); |
| 204 if (!value) | 212 if (!value) |
| 205 return false; | 213 return false; |
| 206 return value->asNumber(output); | 214 return value->asNumber(output); |
| 207 } | 215 } |
| 208 bool getString(const String& name, String* output) const; | 216 bool getString(const String& name, String* output) const; |
| 209 PassRefPtr<JSONObject> getObject(const String& name) const; | 217 JSONObject* getObject(const String& name) const; |
| 210 PassRefPtr<JSONArray> getArray(const String& name) const; | 218 JSONArray* getArray(const String& name) const; |
| 211 PassRefPtr<JSONValue> get(const String& name) const; | 219 JSONValue* get(const String& name) const; |
| 212 | 220 |
| 213 bool booleanProperty(const String& name, bool defaultValue) const; | 221 bool booleanProperty(const String& name, bool defaultValue) const; |
| 214 | 222 |
| 215 void remove(const String& name); | 223 void remove(const String& name); |
| 216 | 224 |
| 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; | 225 ~JSONObject() override; |
| 222 | 226 |
| 223 protected: | 227 protected: |
| 224 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; | 228 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; |
| 225 | 229 |
| 226 private: | 230 private: |
| 227 JSONObject(); | 231 JSONObject(); |
| 228 | 232 |
| 229 Dictionary m_data; | 233 Dictionary m_data; |
| 230 Vector<String> m_order; | 234 Vector<String> m_order; |
| 231 }; | 235 }; |
| 232 | 236 |
| 233 class PLATFORM_EXPORT JSONArray : public JSONValue { | 237 class PLATFORM_EXPORT JSONArray : public JSONValue { |
| 234 public: | 238 public: |
| 235 typedef Vector<RefPtr<JSONValue>>::iterator iterator; | 239 static std::unique_ptr<JSONArray> create() |
| 236 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; | |
| 237 | |
| 238 static PassRefPtr<JSONArray> create() | |
| 239 { | 240 { |
| 240 return adoptRef(new JSONArray()); | 241 return wrapUnique(new JSONArray()); |
| 241 } | 242 } |
| 242 | 243 |
| 243 static PassRefPtr<JSONArray> cast(PassRefPtr<JSONValue> value) | 244 static JSONArray* cast(JSONValue* value) |
| 244 { | 245 { |
| 245 if (!value || value->getType() != TypeArray) | 246 if (!value || value->getType() != TypeArray) |
| 246 return nullptr; | 247 return nullptr; |
| 247 return adoptRef(static_cast<JSONArray*>(value.leakRef())); | 248 return static_cast<JSONArray*>(value); |
| 249 } | |
| 250 | |
| 251 static std::unique_ptr<JSONArray> cast(std::unique_ptr<JSONValue> value) | |
| 252 { | |
| 253 return wrapUnique(JSONArray::cast(value.release())); | |
| 248 } | 254 } |
| 249 | 255 |
| 250 ~JSONArray() override; | 256 ~JSONArray() override; |
| 251 | 257 |
| 252 void writeJSON(StringBuilder* output) const override; | 258 void writeJSON(StringBuilder* output) const override; |
| 253 | 259 |
| 254 void pushBoolean(bool); | 260 void pushBoolean(bool); |
| 255 void pushInt(int); | 261 void pushInt(int); |
| 256 void pushNumber(double); | 262 void pushNumber(double); |
| 257 void pushString(const String&); | 263 void pushString(const String&); |
| 258 void pushValue(PassRefPtr<JSONValue>); | 264 void pushValue(std::unique_ptr<JSONValue>); |
| 259 void pushObject(PassRefPtr<JSONObject>); | 265 void pushObject(std::unique_ptr<JSONObject>); |
| 260 void pushArray(PassRefPtr<JSONArray>); | 266 void pushArray(std::unique_ptr<JSONArray>); |
| 261 | 267 |
| 262 PassRefPtr<JSONValue> get(size_t index); | 268 JSONValue* get(size_t index) const; |
| 263 unsigned length() const { return m_data.size(); } | 269 unsigned length() const { return m_data.size(); } |
| 264 | 270 |
| 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 | |
| 270 protected: | 271 protected: |
| 271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; | 272 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid e; |
| 272 | 273 |
| 273 private: | 274 private: |
| 274 JSONArray(); | 275 JSONArray(); |
| 275 Vector<RefPtr<JSONValue>> m_data; | 276 Vector<std::unique_ptr<JSONValue>> m_data; |
| 276 }; | 277 }; |
| 277 | 278 |
| 278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); | 279 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); |
| 279 void doubleQuoteStringForJSON(const String&, StringBuilder*); | 280 void doubleQuoteStringForJSON(const String&, StringBuilder*); |
| 280 | 281 |
| 281 } // namespace blink | 282 } // namespace blink |
| 282 | 283 |
| 283 #endif // JSONValues_h | 284 #endif // JSONValues_h |
| OLD | NEW |