| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 static PassRefPtr<JSONBasicValue> create(int value) | 103 static PassRefPtr<JSONBasicValue> create(int value) |
| 104 { | 104 { |
| 105 return adoptRef(new JSONBasicValue(value)); | 105 return adoptRef(new JSONBasicValue(value)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 static PassRefPtr<JSONBasicValue> create(double value) | 108 static PassRefPtr<JSONBasicValue> create(double value) |
| 109 { | 109 { |
| 110 return adoptRef(new JSONBasicValue(value)); | 110 return adoptRef(new JSONBasicValue(value)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 virtual bool asBoolean(bool* output) const; | 113 virtual bool asBoolean(bool* output) const OVERRIDE; |
| 114 virtual bool asNumber(double* output) const; | 114 virtual bool asNumber(double* output) const OVERRIDE; |
| 115 virtual bool asNumber(long* output) const; | 115 virtual bool asNumber(long* output) const OVERRIDE; |
| 116 virtual bool asNumber(int* output) const; | 116 virtual bool asNumber(int* output) const OVERRIDE; |
| 117 virtual bool asNumber(unsigned long* output) const; | 117 virtual bool asNumber(unsigned long* output) const OVERRIDE; |
| 118 virtual bool asNumber(unsigned* output) const; | 118 virtual bool asNumber(unsigned* output) const OVERRIDE; |
| 119 | 119 |
| 120 virtual void writeJSON(StringBuilder* output) const; | 120 virtual void writeJSON(StringBuilder* output) const OVERRIDE; |
| 121 | 121 |
| 122 private: | 122 private: |
| 123 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va
lue) { } | 123 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va
lue) { } |
| 124 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d
ouble)value) { } | 124 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d
ouble)value) { } |
| 125 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue
(value) { } | 125 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue
(value) { } |
| 126 | 126 |
| 127 union { | 127 union { |
| 128 bool m_boolValue; | 128 bool m_boolValue; |
| 129 double m_doubleValue; | 129 double m_doubleValue; |
| 130 }; | 130 }; |
| 131 }; | 131 }; |
| 132 | 132 |
| 133 class PLATFORM_EXPORT JSONString : public JSONValue { | 133 class PLATFORM_EXPORT JSONString : public JSONValue { |
| 134 public: | 134 public: |
| 135 static PassRefPtr<JSONString> create(const String& value) | 135 static PassRefPtr<JSONString> create(const String& value) |
| 136 { | 136 { |
| 137 return adoptRef(new JSONString(value)); | 137 return adoptRef(new JSONString(value)); |
| 138 } | 138 } |
| 139 | 139 |
| 140 static PassRefPtr<JSONString> create(const char* value) | 140 static PassRefPtr<JSONString> create(const char* value) |
| 141 { | 141 { |
| 142 return adoptRef(new JSONString(value)); | 142 return adoptRef(new JSONString(value)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 virtual bool asString(String* output) const; | 145 virtual bool asString(String* output) const OVERRIDE; |
| 146 | 146 |
| 147 virtual void writeJSON(StringBuilder* output) const; | 147 virtual void writeJSON(StringBuilder* output) const OVERRIDE; |
| 148 | 148 |
| 149 private: | 149 private: |
| 150 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } | 150 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } |
| 151 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } | 151 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } |
| 152 | 152 |
| 153 String m_stringValue; | 153 String m_stringValue; |
| 154 }; | 154 }; |
| 155 | 155 |
| 156 class PLATFORM_EXPORT JSONObjectBase : public JSONValue { | 156 class PLATFORM_EXPORT JSONObjectBase : public JSONValue { |
| 157 private: | 157 private: |
| 158 typedef HashMap<String, RefPtr<JSONValue> > Dictionary; | 158 typedef HashMap<String, RefPtr<JSONValue> > Dictionary; |
| 159 | 159 |
| 160 public: | 160 public: |
| 161 typedef Dictionary::iterator iterator; | 161 typedef Dictionary::iterator iterator; |
| 162 typedef Dictionary::const_iterator const_iterator; | 162 typedef Dictionary::const_iterator const_iterator; |
| 163 | 163 |
| 164 virtual PassRefPtr<JSONObject> asObject(); | 164 virtual PassRefPtr<JSONObject> asObject() OVERRIDE; |
| 165 JSONObject* openAccessors(); | 165 JSONObject* openAccessors(); |
| 166 | 166 |
| 167 protected: | 167 protected: |
| 168 ~JSONObjectBase(); | 168 virtual ~JSONObjectBase(); |
| 169 | 169 |
| 170 virtual bool asObject(RefPtr<JSONObject>* output); | 170 virtual bool asObject(RefPtr<JSONObject>* output) OVERRIDE; |
| 171 | 171 |
| 172 void setBoolean(const String& name, bool); | 172 void setBoolean(const String& name, bool); |
| 173 void setNumber(const String& name, double); | 173 void setNumber(const String& name, double); |
| 174 void setString(const String& name, const String&); | 174 void setString(const String& name, const String&); |
| 175 void setValue(const String& name, PassRefPtr<JSONValue>); | 175 void setValue(const String& name, PassRefPtr<JSONValue>); |
| 176 void setObject(const String& name, PassRefPtr<JSONObject>); | 176 void setObject(const String& name, PassRefPtr<JSONObject>); |
| 177 void setArray(const String& name, PassRefPtr<JSONArray>); | 177 void setArray(const String& name, PassRefPtr<JSONArray>); |
| 178 | 178 |
| 179 iterator find(const String& name); | 179 iterator find(const String& name); |
| 180 const_iterator find(const String& name) const; | 180 const_iterator find(const String& name) const; |
| 181 bool getBoolean(const String& name, bool* output) const; | 181 bool getBoolean(const String& name, bool* output) const; |
| 182 template<class T> bool getNumber(const String& name, T* output) const | 182 template<class T> bool getNumber(const String& name, T* output) const |
| 183 { | 183 { |
| 184 RefPtr<JSONValue> value = get(name); | 184 RefPtr<JSONValue> value = get(name); |
| 185 if (!value) | 185 if (!value) |
| 186 return false; | 186 return false; |
| 187 return value->asNumber(output); | 187 return value->asNumber(output); |
| 188 } | 188 } |
| 189 bool getString(const String& name, String* output) const; | 189 bool getString(const String& name, String* output) const; |
| 190 PassRefPtr<JSONObject> getObject(const String& name) const; | 190 PassRefPtr<JSONObject> getObject(const String& name) const; |
| 191 PassRefPtr<JSONArray> getArray(const String& name) const; | 191 PassRefPtr<JSONArray> getArray(const String& name) const; |
| 192 PassRefPtr<JSONValue> get(const String& name) const; | 192 PassRefPtr<JSONValue> get(const String& name) const; |
| 193 | 193 |
| 194 void remove(const String& name); | 194 void remove(const String& name); |
| 195 | 195 |
| 196 virtual void writeJSON(StringBuilder* output) const; | 196 virtual void writeJSON(StringBuilder* output) const OVERRIDE; |
| 197 | 197 |
| 198 iterator begin() { return m_data.begin(); } | 198 iterator begin() { return m_data.begin(); } |
| 199 iterator end() { return m_data.end(); } | 199 iterator end() { return m_data.end(); } |
| 200 const_iterator begin() const { return m_data.begin(); } | 200 const_iterator begin() const { return m_data.begin(); } |
| 201 const_iterator end() const { return m_data.end(); } | 201 const_iterator end() const { return m_data.end(); } |
| 202 | 202 |
| 203 int size() const { return m_data.size(); } | 203 int size() const { return m_data.size(); } |
| 204 | 204 |
| 205 protected: | 205 protected: |
| 206 JSONObjectBase(); | 206 JSONObjectBase(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 241 |
| 242 using JSONObjectBase::size; | 242 using JSONObjectBase::size; |
| 243 }; | 243 }; |
| 244 | 244 |
| 245 | 245 |
| 246 class PLATFORM_EXPORT JSONArrayBase : public JSONValue { | 246 class PLATFORM_EXPORT JSONArrayBase : public JSONValue { |
| 247 public: | 247 public: |
| 248 typedef Vector<RefPtr<JSONValue> >::iterator iterator; | 248 typedef Vector<RefPtr<JSONValue> >::iterator iterator; |
| 249 typedef Vector<RefPtr<JSONValue> >::const_iterator const_iterator; | 249 typedef Vector<RefPtr<JSONValue> >::const_iterator const_iterator; |
| 250 | 250 |
| 251 virtual PassRefPtr<JSONArray> asArray(); | 251 virtual PassRefPtr<JSONArray> asArray() OVERRIDE; |
| 252 | 252 |
| 253 unsigned length() const { return m_data.size(); } | 253 unsigned length() const { return m_data.size(); } |
| 254 | 254 |
| 255 protected: | 255 protected: |
| 256 ~JSONArrayBase(); | 256 virtual ~JSONArrayBase(); |
| 257 | 257 |
| 258 virtual bool asArray(RefPtr<JSONArray>* output); | 258 virtual bool asArray(RefPtr<JSONArray>* output) OVERRIDE; |
| 259 | 259 |
| 260 void pushBoolean(bool); | 260 void pushBoolean(bool); |
| 261 void pushInt(int); | 261 void pushInt(int); |
| 262 void pushNumber(double); | 262 void pushNumber(double); |
| 263 void pushString(const String&); | 263 void pushString(const String&); |
| 264 void pushValue(PassRefPtr<JSONValue>); | 264 void pushValue(PassRefPtr<JSONValue>); |
| 265 void pushObject(PassRefPtr<JSONObject>); | 265 void pushObject(PassRefPtr<JSONObject>); |
| 266 void pushArray(PassRefPtr<JSONArray>); | 266 void pushArray(PassRefPtr<JSONArray>); |
| 267 | 267 |
| 268 PassRefPtr<JSONValue> get(size_t index); | 268 PassRefPtr<JSONValue> get(size_t index); |
| 269 | 269 |
| 270 virtual void writeJSON(StringBuilder* output) const; | 270 virtual void writeJSON(StringBuilder* output) const OVERRIDE; |
| 271 | 271 |
| 272 iterator begin() { return m_data.begin(); } | 272 iterator begin() { return m_data.begin(); } |
| 273 iterator end() { return m_data.end(); } | 273 iterator end() { return m_data.end(); } |
| 274 const_iterator begin() const { return m_data.begin(); } | 274 const_iterator begin() const { return m_data.begin(); } |
| 275 const_iterator end() const { return m_data.end(); } | 275 const_iterator end() const { return m_data.end(); } |
| 276 | 276 |
| 277 protected: | 277 protected: |
| 278 JSONArrayBase(); | 278 JSONArrayBase(); |
| 279 | 279 |
| 280 private: | 280 private: |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 385 |
| 386 inline void JSONArrayBase::pushArray(PassRefPtr<JSONArray> value) | 386 inline void JSONArrayBase::pushArray(PassRefPtr<JSONArray> value) |
| 387 { | 387 { |
| 388 ASSERT(value); | 388 ASSERT(value); |
| 389 m_data.append(value); | 389 m_data.append(value); |
| 390 } | 390 } |
| 391 | 391 |
| 392 } // namespace WebCore | 392 } // namespace WebCore |
| 393 | 393 |
| 394 #endif // !defined(JSONValues_h) | 394 #endif // !defined(JSONValues_h) |
| OLD | NEW |