| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_JSON_JSON_VALUE_CONVERTER_H_ | 5 #ifndef BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| 6 #define BASE_JSON_JSON_VALUE_CONVERTER_H_ | 6 #define BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 class FieldConverter : public FieldConverterBase<StructType> { | 115 class FieldConverter : public FieldConverterBase<StructType> { |
| 116 public: | 116 public: |
| 117 explicit FieldConverter(const std::string& path, | 117 explicit FieldConverter(const std::string& path, |
| 118 FieldType StructType::* field, | 118 FieldType StructType::* field, |
| 119 ValueConverter<FieldType>* converter) | 119 ValueConverter<FieldType>* converter) |
| 120 : FieldConverterBase<StructType>(path), | 120 : FieldConverterBase<StructType>(path), |
| 121 field_pointer_(field), | 121 field_pointer_(field), |
| 122 value_converter_(converter) { | 122 value_converter_(converter) { |
| 123 } | 123 } |
| 124 | 124 |
| 125 virtual bool ConvertField( | 125 bool ConvertField(const base::Value& value, StructType* dst) const override { |
| 126 const base::Value& value, StructType* dst) const OVERRIDE { | |
| 127 return value_converter_->Convert(value, &(dst->*field_pointer_)); | 126 return value_converter_->Convert(value, &(dst->*field_pointer_)); |
| 128 } | 127 } |
| 129 | 128 |
| 130 private: | 129 private: |
| 131 FieldType StructType::* field_pointer_; | 130 FieldType StructType::* field_pointer_; |
| 132 scoped_ptr<ValueConverter<FieldType> > value_converter_; | 131 scoped_ptr<ValueConverter<FieldType> > value_converter_; |
| 133 DISALLOW_COPY_AND_ASSIGN(FieldConverter); | 132 DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
| 134 }; | 133 }; |
| 135 | 134 |
| 136 template <typename FieldType> | 135 template <typename FieldType> |
| 137 class BasicValueConverter; | 136 class BasicValueConverter; |
| 138 | 137 |
| 139 template <> | 138 template <> |
| 140 class BasicValueConverter<int> : public ValueConverter<int> { | 139 class BasicValueConverter<int> : public ValueConverter<int> { |
| 141 public: | 140 public: |
| 142 BasicValueConverter() {} | 141 BasicValueConverter() {} |
| 143 | 142 |
| 144 virtual bool Convert(const base::Value& value, int* field) const OVERRIDE { | 143 bool Convert(const base::Value& value, int* field) const override { |
| 145 return value.GetAsInteger(field); | 144 return value.GetAsInteger(field); |
| 146 } | 145 } |
| 147 | 146 |
| 148 private: | 147 private: |
| 149 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 148 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 150 }; | 149 }; |
| 151 | 150 |
| 152 template <> | 151 template <> |
| 153 class BasicValueConverter<std::string> : public ValueConverter<std::string> { | 152 class BasicValueConverter<std::string> : public ValueConverter<std::string> { |
| 154 public: | 153 public: |
| 155 BasicValueConverter() {} | 154 BasicValueConverter() {} |
| 156 | 155 |
| 157 virtual bool Convert( | 156 bool Convert(const base::Value& value, std::string* field) const override { |
| 158 const base::Value& value, std::string* field) const OVERRIDE { | |
| 159 return value.GetAsString(field); | 157 return value.GetAsString(field); |
| 160 } | 158 } |
| 161 | 159 |
| 162 private: | 160 private: |
| 163 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 161 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 164 }; | 162 }; |
| 165 | 163 |
| 166 template <> | 164 template <> |
| 167 class BasicValueConverter<string16> : public ValueConverter<string16> { | 165 class BasicValueConverter<string16> : public ValueConverter<string16> { |
| 168 public: | 166 public: |
| 169 BasicValueConverter() {} | 167 BasicValueConverter() {} |
| 170 | 168 |
| 171 virtual bool Convert( | 169 bool Convert(const base::Value& value, string16* field) const override { |
| 172 const base::Value& value, string16* field) const OVERRIDE { | |
| 173 return value.GetAsString(field); | 170 return value.GetAsString(field); |
| 174 } | 171 } |
| 175 | 172 |
| 176 private: | 173 private: |
| 177 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 174 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 178 }; | 175 }; |
| 179 | 176 |
| 180 template <> | 177 template <> |
| 181 class BasicValueConverter<double> : public ValueConverter<double> { | 178 class BasicValueConverter<double> : public ValueConverter<double> { |
| 182 public: | 179 public: |
| 183 BasicValueConverter() {} | 180 BasicValueConverter() {} |
| 184 | 181 |
| 185 virtual bool Convert(const base::Value& value, double* field) const OVERRIDE { | 182 bool Convert(const base::Value& value, double* field) const override { |
| 186 return value.GetAsDouble(field); | 183 return value.GetAsDouble(field); |
| 187 } | 184 } |
| 188 | 185 |
| 189 private: | 186 private: |
| 190 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 187 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 191 }; | 188 }; |
| 192 | 189 |
| 193 template <> | 190 template <> |
| 194 class BasicValueConverter<bool> : public ValueConverter<bool> { | 191 class BasicValueConverter<bool> : public ValueConverter<bool> { |
| 195 public: | 192 public: |
| 196 BasicValueConverter() {} | 193 BasicValueConverter() {} |
| 197 | 194 |
| 198 virtual bool Convert(const base::Value& value, bool* field) const OVERRIDE { | 195 bool Convert(const base::Value& value, bool* field) const override { |
| 199 return value.GetAsBoolean(field); | 196 return value.GetAsBoolean(field); |
| 200 } | 197 } |
| 201 | 198 |
| 202 private: | 199 private: |
| 203 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 200 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 204 }; | 201 }; |
| 205 | 202 |
| 206 template <typename FieldType> | 203 template <typename FieldType> |
| 207 class ValueFieldConverter : public ValueConverter<FieldType> { | 204 class ValueFieldConverter : public ValueConverter<FieldType> { |
| 208 public: | 205 public: |
| 209 typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); | 206 typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); |
| 210 | 207 |
| 211 ValueFieldConverter(ConvertFunc convert_func) | 208 ValueFieldConverter(ConvertFunc convert_func) |
| 212 : convert_func_(convert_func) {} | 209 : convert_func_(convert_func) {} |
| 213 | 210 |
| 214 virtual bool Convert(const base::Value& value, | 211 bool Convert(const base::Value& value, FieldType* field) const override { |
| 215 FieldType* field) const OVERRIDE { | |
| 216 return convert_func_(&value, field); | 212 return convert_func_(&value, field); |
| 217 } | 213 } |
| 218 | 214 |
| 219 private: | 215 private: |
| 220 ConvertFunc convert_func_; | 216 ConvertFunc convert_func_; |
| 221 | 217 |
| 222 DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); | 218 DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); |
| 223 }; | 219 }; |
| 224 | 220 |
| 225 template <typename FieldType> | 221 template <typename FieldType> |
| 226 class CustomFieldConverter : public ValueConverter<FieldType> { | 222 class CustomFieldConverter : public ValueConverter<FieldType> { |
| 227 public: | 223 public: |
| 228 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); | 224 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); |
| 229 | 225 |
| 230 CustomFieldConverter(ConvertFunc convert_func) | 226 CustomFieldConverter(ConvertFunc convert_func) |
| 231 : convert_func_(convert_func) {} | 227 : convert_func_(convert_func) {} |
| 232 | 228 |
| 233 virtual bool Convert(const base::Value& value, | 229 bool Convert(const base::Value& value, FieldType* field) const override { |
| 234 FieldType* field) const OVERRIDE { | |
| 235 std::string string_value; | 230 std::string string_value; |
| 236 return value.GetAsString(&string_value) && | 231 return value.GetAsString(&string_value) && |
| 237 convert_func_(string_value, field); | 232 convert_func_(string_value, field); |
| 238 } | 233 } |
| 239 | 234 |
| 240 private: | 235 private: |
| 241 ConvertFunc convert_func_; | 236 ConvertFunc convert_func_; |
| 242 | 237 |
| 243 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); | 238 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); |
| 244 }; | 239 }; |
| 245 | 240 |
| 246 template <typename NestedType> | 241 template <typename NestedType> |
| 247 class NestedValueConverter : public ValueConverter<NestedType> { | 242 class NestedValueConverter : public ValueConverter<NestedType> { |
| 248 public: | 243 public: |
| 249 NestedValueConverter() {} | 244 NestedValueConverter() {} |
| 250 | 245 |
| 251 virtual bool Convert( | 246 bool Convert(const base::Value& value, NestedType* field) const override { |
| 252 const base::Value& value, NestedType* field) const OVERRIDE { | |
| 253 return converter_.Convert(value, field); | 247 return converter_.Convert(value, field); |
| 254 } | 248 } |
| 255 | 249 |
| 256 private: | 250 private: |
| 257 JSONValueConverter<NestedType> converter_; | 251 JSONValueConverter<NestedType> converter_; |
| 258 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); | 252 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); |
| 259 }; | 253 }; |
| 260 | 254 |
| 261 template <typename Element> | 255 template <typename Element> |
| 262 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { | 256 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { |
| 263 public: | 257 public: |
| 264 RepeatedValueConverter() {} | 258 RepeatedValueConverter() {} |
| 265 | 259 |
| 266 virtual bool Convert( | 260 bool Convert(const base::Value& value, |
| 267 const base::Value& value, ScopedVector<Element>* field) const OVERRIDE { | 261 ScopedVector<Element>* field) const override { |
| 268 const base::ListValue* list = NULL; | 262 const base::ListValue* list = NULL; |
| 269 if (!value.GetAsList(&list)) { | 263 if (!value.GetAsList(&list)) { |
| 270 // The field is not a list. | 264 // The field is not a list. |
| 271 return false; | 265 return false; |
| 272 } | 266 } |
| 273 | 267 |
| 274 field->reserve(list->GetSize()); | 268 field->reserve(list->GetSize()); |
| 275 for (size_t i = 0; i < list->GetSize(); ++i) { | 269 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 276 const base::Value* element = NULL; | 270 const base::Value* element = NULL; |
| 277 if (!list->Get(i, &element)) | 271 if (!list->Get(i, &element)) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 292 BasicValueConverter<Element> basic_converter_; | 286 BasicValueConverter<Element> basic_converter_; |
| 293 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); | 287 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); |
| 294 }; | 288 }; |
| 295 | 289 |
| 296 template <typename NestedType> | 290 template <typename NestedType> |
| 297 class RepeatedMessageConverter | 291 class RepeatedMessageConverter |
| 298 : public ValueConverter<ScopedVector<NestedType> > { | 292 : public ValueConverter<ScopedVector<NestedType> > { |
| 299 public: | 293 public: |
| 300 RepeatedMessageConverter() {} | 294 RepeatedMessageConverter() {} |
| 301 | 295 |
| 302 virtual bool Convert(const base::Value& value, | 296 bool Convert(const base::Value& value, |
| 303 ScopedVector<NestedType>* field) const OVERRIDE { | 297 ScopedVector<NestedType>* field) const override { |
| 304 const base::ListValue* list = NULL; | 298 const base::ListValue* list = NULL; |
| 305 if (!value.GetAsList(&list)) | 299 if (!value.GetAsList(&list)) |
| 306 return false; | 300 return false; |
| 307 | 301 |
| 308 field->reserve(list->GetSize()); | 302 field->reserve(list->GetSize()); |
| 309 for (size_t i = 0; i < list->GetSize(); ++i) { | 303 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 310 const base::Value* element = NULL; | 304 const base::Value* element = NULL; |
| 311 if (!list->Get(i, &element)) | 305 if (!list->Get(i, &element)) |
| 312 continue; | 306 continue; |
| 313 | 307 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 329 | 323 |
| 330 template <typename NestedType> | 324 template <typename NestedType> |
| 331 class RepeatedCustomValueConverter | 325 class RepeatedCustomValueConverter |
| 332 : public ValueConverter<ScopedVector<NestedType> > { | 326 : public ValueConverter<ScopedVector<NestedType> > { |
| 333 public: | 327 public: |
| 334 typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); | 328 typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); |
| 335 | 329 |
| 336 RepeatedCustomValueConverter(ConvertFunc convert_func) | 330 RepeatedCustomValueConverter(ConvertFunc convert_func) |
| 337 : convert_func_(convert_func) {} | 331 : convert_func_(convert_func) {} |
| 338 | 332 |
| 339 virtual bool Convert(const base::Value& value, | 333 bool Convert(const base::Value& value, |
| 340 ScopedVector<NestedType>* field) const OVERRIDE { | 334 ScopedVector<NestedType>* field) const override { |
| 341 const base::ListValue* list = NULL; | 335 const base::ListValue* list = NULL; |
| 342 if (!value.GetAsList(&list)) | 336 if (!value.GetAsList(&list)) |
| 343 return false; | 337 return false; |
| 344 | 338 |
| 345 field->reserve(list->GetSize()); | 339 field->reserve(list->GetSize()); |
| 346 for (size_t i = 0; i < list->GetSize(); ++i) { | 340 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 347 const base::Value* element = NULL; | 341 const base::Value* element = NULL; |
| 348 if (!list->Get(i, &element)) | 342 if (!list->Get(i, &element)) |
| 349 continue; | 343 continue; |
| 350 | 344 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 | 512 |
| 519 private: | 513 private: |
| 520 ScopedVector<internal::FieldConverterBase<StructType> > fields_; | 514 ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
| 521 | 515 |
| 522 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 516 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
| 523 }; | 517 }; |
| 524 | 518 |
| 525 } // namespace base | 519 } // namespace base |
| 526 | 520 |
| 527 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 521 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| OLD | NEW |