| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 class PLATFORM_EXPORT JSONValue { | 57 class PLATFORM_EXPORT JSONValue { |
| 58 USING_FAST_MALLOC(JSONValue); | 58 USING_FAST_MALLOC(JSONValue); |
| 59 WTF_MAKE_NONCOPYABLE(JSONValue); | 59 WTF_MAKE_NONCOPYABLE(JSONValue); |
| 60 | 60 |
| 61 public: | 61 public: |
| 62 static const int maxDepth = 1000; | 62 static const int maxDepth = 1000; |
| 63 | 63 |
| 64 virtual ~JSONValue() {} | 64 virtual ~JSONValue() {} |
| 65 | 65 |
| 66 static std::unique_ptr<JSONValue> null() { | 66 static std::unique_ptr<JSONValue> null() { |
| 67 return wrapUnique(new JSONValue()); | 67 return WTF::wrapUnique(new JSONValue()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 enum ValueType { | 70 enum ValueType { |
| 71 TypeNull = 0, | 71 TypeNull = 0, |
| 72 TypeBoolean, | 72 TypeBoolean, |
| 73 TypeInteger, | 73 TypeInteger, |
| 74 TypeDouble, | 74 TypeDouble, |
| 75 TypeString, | 75 TypeString, |
| 76 TypeObject, | 76 TypeObject, |
| 77 TypeArray | 77 TypeArray |
| (...skipping 24 matching lines...) Expand all Loading... |
| 102 private: | 102 private: |
| 103 friend class JSONObject; | 103 friend class JSONObject; |
| 104 friend class JSONArray; | 104 friend class JSONArray; |
| 105 | 105 |
| 106 ValueType m_type; | 106 ValueType m_type; |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 class PLATFORM_EXPORT JSONBasicValue : public JSONValue { | 109 class PLATFORM_EXPORT JSONBasicValue : public JSONValue { |
| 110 public: | 110 public: |
| 111 static std::unique_ptr<JSONBasicValue> create(bool value) { | 111 static std::unique_ptr<JSONBasicValue> create(bool value) { |
| 112 return wrapUnique(new JSONBasicValue(value)); | 112 return WTF::wrapUnique(new JSONBasicValue(value)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 static std::unique_ptr<JSONBasicValue> create(int value) { | 115 static std::unique_ptr<JSONBasicValue> create(int value) { |
| 116 return wrapUnique(new JSONBasicValue(value)); | 116 return WTF::wrapUnique(new JSONBasicValue(value)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 static std::unique_ptr<JSONBasicValue> create(double value) { | 119 static std::unique_ptr<JSONBasicValue> create(double value) { |
| 120 return wrapUnique(new JSONBasicValue(value)); | 120 return WTF::wrapUnique(new JSONBasicValue(value)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 bool asBoolean(bool* output) const override; | 123 bool asBoolean(bool* output) const override; |
| 124 bool asDouble(double* output) const override; | 124 bool asDouble(double* output) const override; |
| 125 bool asInteger(int* output) const override; | 125 bool asInteger(int* output) const override; |
| 126 void writeJSON(StringBuilder* output) const override; | 126 void writeJSON(StringBuilder* output) const override; |
| 127 std::unique_ptr<JSONValue> clone() const override; | 127 std::unique_ptr<JSONValue> clone() const override; |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 explicit JSONBasicValue(bool value) | 130 explicit JSONBasicValue(bool value) |
| 131 : JSONValue(TypeBoolean), m_boolValue(value) {} | 131 : JSONValue(TypeBoolean), m_boolValue(value) {} |
| 132 explicit JSONBasicValue(int value) | 132 explicit JSONBasicValue(int value) |
| 133 : JSONValue(TypeInteger), m_integerValue(value) {} | 133 : JSONValue(TypeInteger), m_integerValue(value) {} |
| 134 explicit JSONBasicValue(double value) | 134 explicit JSONBasicValue(double value) |
| 135 : JSONValue(TypeDouble), m_doubleValue(value) {} | 135 : JSONValue(TypeDouble), m_doubleValue(value) {} |
| 136 | 136 |
| 137 union { | 137 union { |
| 138 bool m_boolValue; | 138 bool m_boolValue; |
| 139 double m_doubleValue; | 139 double m_doubleValue; |
| 140 int m_integerValue; | 140 int m_integerValue; |
| 141 }; | 141 }; |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 class PLATFORM_EXPORT JSONString : public JSONValue { | 144 class PLATFORM_EXPORT JSONString : public JSONValue { |
| 145 public: | 145 public: |
| 146 static std::unique_ptr<JSONString> create(const String& value) { | 146 static std::unique_ptr<JSONString> create(const String& value) { |
| 147 return wrapUnique(new JSONString(value)); | 147 return WTF::wrapUnique(new JSONString(value)); |
| 148 } | 148 } |
| 149 | 149 |
| 150 static std::unique_ptr<JSONString> create(const char* value) { | 150 static std::unique_ptr<JSONString> create(const char* value) { |
| 151 return wrapUnique(new JSONString(value)); | 151 return WTF::wrapUnique(new JSONString(value)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 bool asString(String* output) const override; | 154 bool asString(String* output) const override; |
| 155 void writeJSON(StringBuilder* output) const override; | 155 void writeJSON(StringBuilder* output) const override; |
| 156 std::unique_ptr<JSONValue> clone() const override; | 156 std::unique_ptr<JSONValue> clone() const override; |
| 157 | 157 |
| 158 private: | 158 private: |
| 159 explicit JSONString(const String& value) | 159 explicit JSONString(const String& value) |
| 160 : JSONValue(TypeString), m_stringValue(value) {} | 160 : JSONValue(TypeString), m_stringValue(value) {} |
| 161 explicit JSONString(const char* value) | 161 explicit JSONString(const char* value) |
| 162 : JSONValue(TypeString), m_stringValue(value) {} | 162 : JSONValue(TypeString), m_stringValue(value) {} |
| 163 | 163 |
| 164 String m_stringValue; | 164 String m_stringValue; |
| 165 }; | 165 }; |
| 166 | 166 |
| 167 class PLATFORM_EXPORT JSONObject : public JSONValue { | 167 class PLATFORM_EXPORT JSONObject : public JSONValue { |
| 168 public: | 168 public: |
| 169 using Entry = std::pair<String, JSONValue*>; | 169 using Entry = std::pair<String, JSONValue*>; |
| 170 static std::unique_ptr<JSONObject> create() { | 170 static std::unique_ptr<JSONObject> create() { |
| 171 return wrapUnique(new JSONObject()); | 171 return WTF::wrapUnique(new JSONObject()); |
| 172 } | 172 } |
| 173 | 173 |
| 174 static JSONObject* cast(JSONValue* value) { | 174 static JSONObject* cast(JSONValue* value) { |
| 175 if (!value || value->getType() != TypeObject) | 175 if (!value || value->getType() != TypeObject) |
| 176 return nullptr; | 176 return nullptr; |
| 177 return static_cast<JSONObject*>(value); | 177 return static_cast<JSONObject*>(value); |
| 178 } | 178 } |
| 179 | 179 |
| 180 static std::unique_ptr<JSONObject> from(std::unique_ptr<JSONValue> value) { | 180 static std::unique_ptr<JSONObject> from(std::unique_ptr<JSONValue> value) { |
| 181 auto maybeObject = wrapUnique(JSONObject::cast(value.get())); | 181 auto maybeObject = WTF::wrapUnique(JSONObject::cast(value.get())); |
| 182 if (maybeObject) | 182 if (maybeObject) |
| 183 value.release(); | 183 value.release(); |
| 184 return maybeObject; | 184 return maybeObject; |
| 185 } | 185 } |
| 186 | 186 |
| 187 static void cast(JSONObject*) = delete; | 187 static void cast(JSONObject*) = delete; |
| 188 static void cast(std::unique_ptr<JSONObject>) = delete; | 188 static void cast(std::unique_ptr<JSONObject>) = delete; |
| 189 | 189 |
| 190 void writeJSON(StringBuilder* output) const override; | 190 void writeJSON(StringBuilder* output) const override; |
| 191 std::unique_ptr<JSONValue> clone() const override; | 191 std::unique_ptr<JSONValue> clone() const override; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 230 } |
| 231 | 231 |
| 232 using Dictionary = HashMap<String, std::unique_ptr<JSONValue>>; | 232 using Dictionary = HashMap<String, std::unique_ptr<JSONValue>>; |
| 233 Dictionary m_data; | 233 Dictionary m_data; |
| 234 Vector<String> m_order; | 234 Vector<String> m_order; |
| 235 }; | 235 }; |
| 236 | 236 |
| 237 class PLATFORM_EXPORT JSONArray : public JSONValue { | 237 class PLATFORM_EXPORT JSONArray : public JSONValue { |
| 238 public: | 238 public: |
| 239 static std::unique_ptr<JSONArray> create() { | 239 static std::unique_ptr<JSONArray> create() { |
| 240 return wrapUnique(new JSONArray()); | 240 return WTF::wrapUnique(new JSONArray()); |
| 241 } | 241 } |
| 242 | 242 |
| 243 static JSONArray* cast(JSONValue* value) { | 243 static JSONArray* cast(JSONValue* value) { |
| 244 if (!value || value->getType() != TypeArray) | 244 if (!value || value->getType() != TypeArray) |
| 245 return nullptr; | 245 return nullptr; |
| 246 return static_cast<JSONArray*>(value); | 246 return static_cast<JSONArray*>(value); |
| 247 } | 247 } |
| 248 | 248 |
| 249 static std::unique_ptr<JSONArray> from(std::unique_ptr<JSONValue> value) { | 249 static std::unique_ptr<JSONArray> from(std::unique_ptr<JSONValue> value) { |
| 250 auto maybeArray = wrapUnique(JSONArray::cast(value.get())); | 250 auto maybeArray = WTF::wrapUnique(JSONArray::cast(value.get())); |
| 251 if (maybeArray) | 251 if (maybeArray) |
| 252 value.release(); | 252 value.release(); |
| 253 return maybeArray; | 253 return maybeArray; |
| 254 } | 254 } |
| 255 | 255 |
| 256 static void cast(JSONArray*) = delete; | 256 static void cast(JSONArray*) = delete; |
| 257 static void cast(std::unique_ptr<JSONArray>) = delete; | 257 static void cast(std::unique_ptr<JSONArray>) = delete; |
| 258 | 258 |
| 259 ~JSONArray() override; | 259 ~JSONArray() override; |
| 260 | 260 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 279 JSONArray(); | 279 JSONArray(); |
| 280 Vector<std::unique_ptr<JSONValue>> m_data; | 280 Vector<std::unique_ptr<JSONValue>> m_data; |
| 281 }; | 281 }; |
| 282 | 282 |
| 283 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); | 283 PLATFORM_EXPORT void escapeStringForJSON(const String&, StringBuilder*); |
| 284 void doubleQuoteStringForJSON(const String&, StringBuilder*); | 284 void doubleQuoteStringForJSON(const String&, StringBuilder*); |
| 285 | 285 |
| 286 } // namespace blink | 286 } // namespace blink |
| 287 | 287 |
| 288 #endif // JSONValues_h | 288 #endif // JSONValues_h |
| OLD | NEW |