| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 static PassRefPtr<JSONBasicValue> create(int value) | 140 static PassRefPtr<JSONBasicValue> create(int value) |
| 141 { | 141 { |
| 142 return adoptRef(new JSONBasicValue(value)); | 142 return adoptRef(new JSONBasicValue(value)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 static PassRefPtr<JSONBasicValue> create(double value) | 145 static PassRefPtr<JSONBasicValue> create(double value) |
| 146 { | 146 { |
| 147 return adoptRef(new JSONBasicValue(value)); | 147 return adoptRef(new JSONBasicValue(value)); |
| 148 } | 148 } |
| 149 | 149 |
| 150 virtual bool asBoolean(bool* output) const override; | 150 bool asBoolean(bool* output) const override; |
| 151 virtual bool asNumber(double* output) const override; | 151 bool asNumber(double* output) const override; |
| 152 virtual bool asNumber(long* output) const override; | 152 bool asNumber(long* output) const override; |
| 153 virtual bool asNumber(int* output) const override; | 153 bool asNumber(int* output) const override; |
| 154 virtual bool asNumber(unsigned long* output) const override; | 154 bool asNumber(unsigned long* output) const override; |
| 155 virtual bool asNumber(unsigned* output) const override; | 155 bool asNumber(unsigned* output) const override; |
| 156 | 156 |
| 157 virtual void writeJSON(StringBuilder* output) const override; | 157 void writeJSON(StringBuilder* output) const override; |
| 158 | 158 |
| 159 private: | 159 private: |
| 160 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va
lue) { } | 160 explicit JSONBasicValue(bool value) : JSONValue(TypeBoolean), m_boolValue(va
lue) { } |
| 161 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d
ouble)value) { } | 161 explicit JSONBasicValue(int value) : JSONValue(TypeNumber), m_doubleValue((d
ouble)value) { } |
| 162 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue
(value) { } | 162 explicit JSONBasicValue(double value) : JSONValue(TypeNumber), m_doubleValue
(value) { } |
| 163 | 163 |
| 164 union { | 164 union { |
| 165 bool m_boolValue; | 165 bool m_boolValue; |
| 166 double m_doubleValue; | 166 double m_doubleValue; |
| 167 }; | 167 }; |
| 168 }; | 168 }; |
| 169 | 169 |
| 170 class PLATFORM_EXPORT JSONString : public JSONValue { | 170 class PLATFORM_EXPORT JSONString : public JSONValue { |
| 171 public: | 171 public: |
| 172 static PassRefPtr<JSONString> create(const String& value) | 172 static PassRefPtr<JSONString> create(const String& value) |
| 173 { | 173 { |
| 174 return adoptRef(new JSONString(value)); | 174 return adoptRef(new JSONString(value)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 static PassRefPtr<JSONString> create(const char* value) | 177 static PassRefPtr<JSONString> create(const char* value) |
| 178 { | 178 { |
| 179 return adoptRef(new JSONString(value)); | 179 return adoptRef(new JSONString(value)); |
| 180 } | 180 } |
| 181 | 181 |
| 182 virtual bool asString(String* output) const override; | 182 bool asString(String* output) const override; |
| 183 | 183 |
| 184 virtual void writeJSON(StringBuilder* output) const override; | 184 void writeJSON(StringBuilder* output) const override; |
| 185 | 185 |
| 186 private: | 186 private: |
| 187 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } | 187 explicit JSONString(const String& value) : JSONValue(TypeString), m_stringVa
lue(value) { } |
| 188 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } | 188 explicit JSONString(const char* value) : JSONValue(TypeString), m_stringValu
e(value) { } |
| 189 | 189 |
| 190 String m_stringValue; | 190 String m_stringValue; |
| 191 }; | 191 }; |
| 192 | 192 |
| 193 class PLATFORM_EXPORT JSONObjectBase : public JSONValue { | 193 class PLATFORM_EXPORT JSONObjectBase : public JSONValue { |
| 194 private: | 194 private: |
| 195 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; | 195 typedef HashMap<String, RefPtr<JSONValue>> Dictionary; |
| 196 | 196 |
| 197 public: | 197 public: |
| 198 typedef Dictionary::iterator iterator; | 198 typedef Dictionary::iterator iterator; |
| 199 typedef Dictionary::const_iterator const_iterator; | 199 typedef Dictionary::const_iterator const_iterator; |
| 200 | 200 |
| 201 virtual PassRefPtr<JSONObject> asObject() override; | 201 PassRefPtr<JSONObject> asObject() override; |
| 202 JSONObject* openAccessors(); | 202 JSONObject* openAccessors(); |
| 203 | 203 |
| 204 virtual void writeJSON(StringBuilder* output) const override; | 204 void writeJSON(StringBuilder* output) const override; |
| 205 | 205 |
| 206 int size() const { return m_data.size(); } | 206 int size() const { return m_data.size(); } |
| 207 | 207 |
| 208 protected: | 208 protected: |
| 209 virtual ~JSONObjectBase(); | 209 ~JSONObjectBase() override; |
| 210 | 210 |
| 211 virtual bool asObject(RefPtr<JSONObject>* output) override; | 211 bool asObject(RefPtr<JSONObject>* output) override; |
| 212 | 212 |
| 213 void setBoolean(const String& name, bool); | 213 void setBoolean(const String& name, bool); |
| 214 void setNumber(const String& name, double); | 214 void setNumber(const String& name, double); |
| 215 void setString(const String& name, const String&); | 215 void setString(const String& name, const String&); |
| 216 void setValue(const String& name, PassRefPtr<JSONValue>); | 216 void setValue(const String& name, PassRefPtr<JSONValue>); |
| 217 void setObject(const String& name, PassRefPtr<JSONObject>); | 217 void setObject(const String& name, PassRefPtr<JSONObject>); |
| 218 void setArray(const String& name, PassRefPtr<JSONArray>); | 218 void setArray(const String& name, PassRefPtr<JSONArray>); |
| 219 | 219 |
| 220 iterator find(const String& name); | 220 iterator find(const String& name); |
| 221 const_iterator find(const String& name) const; | 221 const_iterator find(const String& name) const; |
| 222 bool getBoolean(const String& name, bool* output) const; | 222 bool getBoolean(const String& name, bool* output) const; |
| 223 template<class T> bool getNumber(const String& name, T* output) const | 223 template<class T> bool getNumber(const String& name, T* output) const |
| 224 { | 224 { |
| 225 RefPtr<JSONValue> value = get(name); | 225 RefPtr<JSONValue> value = get(name); |
| 226 if (!value) | 226 if (!value) |
| 227 return false; | 227 return false; |
| 228 return value->asNumber(output); | 228 return value->asNumber(output); |
| 229 } | 229 } |
| 230 bool getString(const String& name, String* output) const; | 230 bool getString(const String& name, String* output) const; |
| 231 PassRefPtr<JSONObject> getObject(const String& name) const; | 231 PassRefPtr<JSONObject> getObject(const String& name) const; |
| 232 PassRefPtr<JSONArray> getArray(const String& name) const; | 232 PassRefPtr<JSONArray> getArray(const String& name) const; |
| 233 PassRefPtr<JSONValue> get(const String& name) const; | 233 PassRefPtr<JSONValue> get(const String& name) const; |
| 234 | 234 |
| 235 void remove(const String& name); | 235 void remove(const String& name); |
| 236 | 236 |
| 237 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const
override; | 237 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; |
| 238 | 238 |
| 239 iterator begin() { return m_data.begin(); } | 239 iterator begin() { return m_data.begin(); } |
| 240 iterator end() { return m_data.end(); } | 240 iterator end() { return m_data.end(); } |
| 241 const_iterator begin() const { return m_data.begin(); } | 241 const_iterator begin() const { return m_data.begin(); } |
| 242 const_iterator end() const { return m_data.end(); } | 242 const_iterator end() const { return m_data.end(); } |
| 243 | 243 |
| 244 protected: | 244 protected: |
| 245 JSONObjectBase(); | 245 JSONObjectBase(); |
| 246 | 246 |
| 247 private: | 247 private: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 280 |
| 281 using JSONObjectBase::size; | 281 using JSONObjectBase::size; |
| 282 }; | 282 }; |
| 283 | 283 |
| 284 | 284 |
| 285 class PLATFORM_EXPORT JSONArrayBase : public JSONValue { | 285 class PLATFORM_EXPORT JSONArrayBase : public JSONValue { |
| 286 public: | 286 public: |
| 287 typedef Vector<RefPtr<JSONValue>>::iterator iterator; | 287 typedef Vector<RefPtr<JSONValue>>::iterator iterator; |
| 288 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; | 288 typedef Vector<RefPtr<JSONValue>>::const_iterator const_iterator; |
| 289 | 289 |
| 290 virtual PassRefPtr<JSONArray> asArray() override; | 290 PassRefPtr<JSONArray> asArray() override; |
| 291 | 291 |
| 292 unsigned length() const { return m_data.size(); } | 292 unsigned length() const { return m_data.size(); } |
| 293 | 293 |
| 294 virtual void writeJSON(StringBuilder* output) const override; | 294 void writeJSON(StringBuilder* output) const override; |
| 295 | 295 |
| 296 protected: | 296 protected: |
| 297 virtual ~JSONArrayBase(); | 297 ~JSONArrayBase() override; |
| 298 | 298 |
| 299 virtual bool asArray(RefPtr<JSONArray>* output) override; | 299 bool asArray(RefPtr<JSONArray>* output) override; |
| 300 | 300 |
| 301 void pushBoolean(bool); | 301 void pushBoolean(bool); |
| 302 void pushInt(int); | 302 void pushInt(int); |
| 303 void pushNumber(double); | 303 void pushNumber(double); |
| 304 void pushString(const String&); | 304 void pushString(const String&); |
| 305 void pushValue(PassRefPtr<JSONValue>); | 305 void pushValue(PassRefPtr<JSONValue>); |
| 306 void pushObject(PassRefPtr<JSONObject>); | 306 void pushObject(PassRefPtr<JSONObject>); |
| 307 void pushArray(PassRefPtr<JSONArray>); | 307 void pushArray(PassRefPtr<JSONArray>); |
| 308 | 308 |
| 309 PassRefPtr<JSONValue> get(size_t index); | 309 PassRefPtr<JSONValue> get(size_t index); |
| 310 | 310 |
| 311 virtual void prettyWriteJSONInternal(StringBuilder* output, int depth) const
override; | 311 void prettyWriteJSONInternal(StringBuilder* output, int depth) const overrid
e; |
| 312 | 312 |
| 313 iterator begin() { return m_data.begin(); } | 313 iterator begin() { return m_data.begin(); } |
| 314 iterator end() { return m_data.end(); } | 314 iterator end() { return m_data.end(); } |
| 315 const_iterator begin() const { return m_data.begin(); } | 315 const_iterator begin() const { return m_data.begin(); } |
| 316 const_iterator end() const { return m_data.end(); } | 316 const_iterator end() const { return m_data.end(); } |
| 317 | 317 |
| 318 protected: | 318 protected: |
| 319 JSONArrayBase(); | 319 JSONArrayBase(); |
| 320 | 320 |
| 321 private: | 321 private: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 341 | 341 |
| 342 using JSONArrayBase::get; | 342 using JSONArrayBase::get; |
| 343 | 343 |
| 344 using JSONArrayBase::begin; | 344 using JSONArrayBase::begin; |
| 345 using JSONArrayBase::end; | 345 using JSONArrayBase::end; |
| 346 }; | 346 }; |
| 347 | 347 |
| 348 } // namespace blink | 348 } // namespace blink |
| 349 | 349 |
| 350 #endif // JSONValues_h | 350 #endif // JSONValues_h |
| OLD | NEW |