| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 namespace blink { | 50 namespace blink { |
| 51 | 51 |
| 52 class JSONArray; | 52 class JSONArray; |
| 53 class JSONObject; | 53 class JSONObject; |
| 54 | 54 |
| 55 class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> { | 55 class PLATFORM_EXPORT JSONValue : public RefCounted<JSONValue> { |
| 56 public: | 56 public: |
| 57 static const int maxDepth = 1000; | 57 static const int maxDepth = 1000; |
| 58 | 58 |
| 59 JSONValue() : m_type(TypeNull) { } |
| 59 virtual ~JSONValue() { } | 60 virtual ~JSONValue() { } |
| 60 | 61 |
| 61 static PassRefPtr<JSONValue> null() | 62 static PassRefPtr<JSONValue> null() |
| 62 { | 63 { |
| 63 return adoptRef(new JSONValue()); | 64 return adoptRef(new JSONValue()); |
| 64 } | 65 } |
| 65 | 66 |
| 66 typedef enum { | 67 typedef enum { |
| 67 TypeNull = 0, | 68 TypeNull = 0, |
| 68 TypeBoolean, | 69 TypeBoolean, |
| 69 TypeNumber, | 70 TypeNumber, |
| 70 TypeString, | 71 TypeString, |
| 71 TypeObject, | 72 TypeObject, |
| 72 TypeArray | 73 TypeArray |
| 73 } Type; | 74 } Type; |
| 74 | 75 |
| 75 Type type() const { return m_type; } | 76 Type type() const { return m_type; } |
| 76 | 77 |
| 77 bool isNull() const { return m_type == TypeNull; } | 78 bool isNull() const { return m_type == TypeNull; } |
| 78 | 79 |
| 79 virtual bool asBoolean(bool* output) const; | 80 virtual bool asBoolean(bool* output) const; |
| 80 virtual bool asNumber(double* output) const; | 81 virtual bool asNumber(double* output) const; |
| 81 virtual bool asNumber(long* output) const; | 82 virtual bool asNumber(long* output) const; |
| 82 virtual bool asNumber(int* output) const; | 83 virtual bool asNumber(int* output) const; |
| 83 virtual bool asNumber(unsigned long* output) const; | 84 virtual bool asNumber(unsigned long* output) const; |
| 84 virtual bool asNumber(unsigned* output) const; | 85 virtual bool asNumber(unsigned* output) const; |
| 85 virtual bool asString(String* output) const; | 86 virtual bool asString(String* output) const; |
| 87 virtual bool asObject(RefPtr<JSONObject>* output); |
| 88 virtual bool asArray(RefPtr<JSONArray>* output); |
| 89 virtual PassRefPtr<JSONObject> asObject(); |
| 90 virtual PassRefPtr<JSONArray> asArray(); |
| 86 | 91 |
| 87 String toJSONString() const; | 92 String toJSONString() const; |
| 88 String toPrettyJSONString() const; | 93 String toPrettyJSONString() const; |
| 89 virtual void writeJSON(StringBuilder* output) const; | 94 virtual void writeJSON(StringBuilder* output) const; |
| 90 virtual void prettyWriteJSON(StringBuilder* output) const; | 95 virtual void prettyWriteJSON(StringBuilder* output) const; |
| 91 | 96 |
| 92 static String quoteString(const String&); | 97 static String quoteString(const String&); |
| 93 | 98 |
| 94 protected: | 99 protected: |
| 95 JSONValue() : m_type(TypeNull) { } | |
| 96 explicit JSONValue(Type type) : m_type(type) { } | 100 explicit JSONValue(Type 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 JSONObjectBase; |
| 101 friend class JSONArray; | 105 friend class JSONArrayBase; |
| 102 | 106 |
| 103 Type m_type; | 107 Type 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 |
| 109 static PassRefPtr<JSONBasicValue> create(bool value) | 113 static PassRefPtr<JSONBasicValue> create(bool value) |
| 110 { | 114 { |
| 111 return adoptRef(new JSONBasicValue(value)); | 115 return adoptRef(new JSONBasicValue(value)); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 161 |
| 158 void writeJSON(StringBuilder* output) const override; | 162 void writeJSON(StringBuilder* output) const override; |
| 159 | 163 |
| 160 private: | 164 private: |
| 161 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } | 165 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } |
| 162 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } | 166 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } |
| 163 | 167 |
| 164 String m_stringValue; | 168 String m_stringValue; |
| 165 }; | 169 }; |
| 166 | 170 |
| 167 class PLATFORM_EXPORT JSONObject : public JSONValue { | 171 class PLATFORM_EXPORT JSONObjectBase : public JSONValue { |
| 168 private: | 172 private: |
| 169 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; | 173 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; |
| 170 | 174 |
| 171 public: | 175 public: |
| 172 typedef Dictionary::iterator iterator; | 176 typedef Dictionary::iterator iterator; |
| 173 typedef Dictionary::const_iterator const_iterator; | 177 typedef Dictionary::const_iterator const_iterator; |
| 174 | 178 |
| 175 static PassRefPtr<JSONObject> create() | 179 PassRefPtr<JSONObject> asObject() override; |
| 176 { | 180 JSONObject* openAccessors(); |
| 177 return adoptRef(new JSONObject()); | |
| 178 } | |
| 179 | |
| 180 static PassRefPtr<JSONObject> cast(PassRefPtr<JSONValue> value) | |
| 181 { | |
| 182 if (!value || value->type() != TypeObject) | |
| 183 return nullptr; | |
| 184 return adoptRef(static_cast<JSONObject*>(value.leakRef())); | |
| 185 } | |
| 186 | 181 |
| 187 void writeJSON(StringBuilder* output) const override; | 182 void writeJSON(StringBuilder* output) const override; |
| 188 | 183 |
| 189 int size() const { return m_data.size(); } | 184 int size() const { return m_data.size(); } |
| 190 | 185 |
| 186 protected: |
| 187 ~JSONObjectBase() override; |
| 188 |
| 189 bool asObject(RefPtr<JSONObject>* output) override; |
| 190 |
| 191 void setBoolean(const String& name, bool); | 191 void setBoolean(const String& name, bool); |
| 192 void setNumber(const String& name, double); | 192 void setNumber(const String& name, double); |
| 193 void setString(const String& name, const String&); | 193 void setString(const String& name, const String&); |
| 194 void setValue(const String& name, PassRefPtr<JSONValue>); | 194 void setValue(const String& name, PassRefPtr<JSONValue>); |
| 195 void setObject(const String& name, PassRefPtr<JSONObject>); | 195 void setObject(const String& name, PassRefPtr<JSONObject>); |
| 196 void setArray(const String& name, PassRefPtr<JSONArray>); | 196 void setArray(const String& name, PassRefPtr<JSONArray>); |
| 197 | 197 |
| 198 iterator find(const String& name); | 198 iterator find(const String& name); |
| 199 const_iterator find(const String& name) const; | 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 template<class T> bool getNumber(const String& name, T* output) const |
| 202 { | 202 { |
| 203 RefPtr<JSONValue> value = get(name); | 203 RefPtr<JSONValue> value = get(name); |
| 204 if (!value) | 204 if (!value) |
| 205 return false; | 205 return false; |
| 206 return value->asNumber(output); | 206 return value->asNumber(output); |
| 207 } | 207 } |
| 208 bool getString(const String& name, String* output) const; | 208 bool getString(const String& name, String* output) const; |
| 209 PassRefPtr<JSONObject> getObject(const String& name) const; | 209 PassRefPtr<JSONObject> getObject(const String& name) const; |
| 210 PassRefPtr<JSONArray> getArray(const String& name) const; | 210 PassRefPtr<JSONArray> getArray(const String& name) const; |
| 211 PassRefPtr<JSONValue> get(const String& name) const; | 211 PassRefPtr<JSONValue> get(const String& name) const; |
| 212 | 212 |
| 213 bool booleanProperty(const String& name, bool defaultValue) const; | 213 bool booleanProperty(const String& name, bool defaultValue) const; |
| 214 | 214 |
| 215 void remove(const String& name); | 215 void remove(const String& name); |
| 216 | 216 |
| 217 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; |
| 218 |
| 217 iterator begin() { return m_data.begin(); } | 219 iterator begin() { return m_data.begin(); } |
| 218 iterator end() { return m_data.end(); } | 220 iterator end() { return m_data.end(); } |
| 219 const_iterator begin() const { return m_data.begin(); } | 221 const_iterator begin() const { return m_data.begin(); } |
| 220 const_iterator end() const { return m_data.end(); } | 222 const_iterator end() const { return m_data.end(); } |
| 221 ~JSONObject() override; | |
| 222 | 223 |
| 223 protected: | 224 protected: |
| 224 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; | 225 JSONObjectBase(); |
| 225 | 226 |
| 226 private: | 227 private: |
| 227 JSONObject(); | |
| 228 | |
| 229 Dictionary m_data; | 228 Dictionary m_data; |
| 230 Vector<String> m_order; | 229 Vector<String> m_order; |
| 231 }; | 230 }; |
| 232 | 231 |
| 233 class PLATFORM_EXPORT JSONArray : public JSONValue { | 232 class PLATFORM_EXPORT JSONObject : public JSONObjectBase { |
| 233 public: |
| 234 static PassRefPtr<JSONObject> create() |
| 235 { |
| 236 return adoptRef(new JSONObject()); |
| 237 } |
| 238 |
| 239 using JSONObjectBase::asObject; |
| 240 |
| 241 using JSONObjectBase::setBoolean; |
| 242 using JSONObjectBase::setNumber; |
| 243 using JSONObjectBase::setString; |
| 244 using JSONObjectBase::setValue; |
| 245 using JSONObjectBase::setObject; |
| 246 using JSONObjectBase::setArray; |
| 247 |
| 248 using JSONObjectBase::find; |
| 249 using JSONObjectBase::getBoolean; |
| 250 using JSONObjectBase::getNumber; |
| 251 using JSONObjectBase::getString; |
| 252 using JSONObjectBase::getObject; |
| 253 using JSONObjectBase::getArray; |
| 254 using JSONObjectBase::get; |
| 255 |
| 256 using JSONObjectBase::booleanProperty; |
| 257 |
| 258 using JSONObjectBase::remove; |
| 259 |
| 260 using JSONObjectBase::begin; |
| 261 using JSONObjectBase::end; |
| 262 |
| 263 using JSONObjectBase::size; |
| 264 }; |
| 265 |
| 266 |
| 267 class PLATFORM_EXPORT JSONArrayBase : public JSONValue { |
| 234 public: | 268 public: |
| 235 typedef Vector<RefPtr<JSONValue>>::iterator iterator; | 269 typedef Vector<RefPtr<JSONValue>>::iterator iterator; |
| 236 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; | 270 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; |
| 237 | 271 |
| 238 static PassRefPtr<JSONArray> create() | 272 PassRefPtr<JSONArray> asArray() override; |
| 239 { | |
| 240 return adoptRef(new JSONArray()); | |
| 241 } | |
| 242 | 273 |
| 243 static PassRefPtr<JSONArray> cast(PassRefPtr<JSONValue> value) | 274 unsigned length() const { return m_data.size(); } |
| 244 { | |
| 245 if (!value || value->type() != TypeArray) | |
| 246 return nullptr; | |
| 247 return adoptRef(static_cast<JSONArray*>(value.leakRef())); | |
| 248 } | |
| 249 | |
| 250 ~JSONArray() override; | |
| 251 | 275 |
| 252 void writeJSON(StringBuilder* output) const override; | 276 void writeJSON(StringBuilder* output) const override; |
| 253 | 277 |
| 278 protected: |
| 279 ~JSONArrayBase() override; |
| 280 |
| 281 bool asArray(RefPtr<JSONArray>* output) override; |
| 282 |
| 254 void pushBoolean(bool); | 283 void pushBoolean(bool); |
| 255 void pushInt(int); | 284 void pushInt(int); |
| 256 void pushNumber(double); | 285 void pushNumber(double); |
| 257 void pushString(const String&); | 286 void pushString(const String&); |
| 258 void pushValue(PassRefPtr<JSONValue>); | 287 void pushValue(PassRefPtr<JSONValue>); |
| 259 void pushObject(PassRefPtr<JSONObject>); | 288 void pushObject(PassRefPtr<JSONObject>); |
| 260 void pushArray(PassRefPtr<JSONArray>); | 289 void pushArray(PassRefPtr<JSONArray>); |
| 261 | 290 |
| 262 PassRefPtr<JSONValue> get(size_t index); | 291 PassRefPtr<JSONValue> get(size_t index); |
| 263 unsigned length() const { return m_data.size(); } | 292 |
| 293 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; |
| 264 | 294 |
| 265 iterator begin() { return m_data.begin(); } | 295 iterator begin() { return m_data.begin(); } |
| 266 iterator end() { return m_data.end(); } | 296 iterator end() { return m_data.end(); } |
| 267 const_iterator begin() const { return m_data.begin(); } | 297 const_iterator begin() const { return m_data.begin(); } |
| 268 const_iterator end() const { return m_data.end(); } | 298 const_iterator end() const { return m_data.end(); } |
| 269 | 299 |
| 270 protected: | 300 protected: |
| 271 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; | 301 JSONArrayBase(); |
| 272 | 302 |
| 273 private: | 303 private: |
| 274 JSONArray(); | |
| 275 Vector<RefPtr<JSONValue>> m_data; | 304 Vector<RefPtr<JSONValue>> m_data; |
| 276 }; | 305 }; |
| 277 | 306 |
| 307 class PLATFORM_EXPORT JSONArray : public JSONArrayBase { |
| 308 public: |
| 309 static PassRefPtr<JSONArray> create() |
| 310 { |
| 311 return adoptRef(new JSONArray()); |
| 312 } |
| 313 |
| 314 using JSONArrayBase::asArray; |
| 315 |
| 316 using JSONArrayBase::pushBoolean; |
| 317 using JSONArrayBase::pushInt; |
| 318 using JSONArrayBase::pushNumber; |
| 319 using JSONArrayBase::pushString; |
| 320 using JSONArrayBase::pushValue; |
| 321 using JSONArrayBase::pushObject; |
| 322 using JSONArrayBase::pushArray; |
| 323 |
| 324 using JSONArrayBase::get; |
| 325 |
| 326 using JSONArrayBase::begin; |
| 327 using JSONArrayBase::end; |
| 328 }; |
| 329 |
| 278 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); | 330 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); |
| 279 void doubleQuoteStringForJSON(const String&, StringBuilder*); | 331 void doubleQuoteStringForJSON(const String&, StringBuilder*); |
| 280 | 332 |
| 281 } // namespace blink | 333 } // namespace blink |
| 282 | 334 |
| 283 #endif // JSONValues_h | 335 #endif // JSONValues_h |
| OLD | NEW |