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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 class FieldConverter : public FieldConverterBase<StructType> { | 116 class FieldConverter : public FieldConverterBase<StructType> { |
117 public: | 117 public: |
118 explicit FieldConverter(const std::string& path, | 118 explicit FieldConverter(const std::string& path, |
119 FieldType StructType::* field, | 119 FieldType StructType::* field, |
120 ValueConverter<FieldType>* converter) | 120 ValueConverter<FieldType>* converter) |
121 : FieldConverterBase<StructType>(path), | 121 : FieldConverterBase<StructType>(path), |
122 field_pointer_(field), | 122 field_pointer_(field), |
123 value_converter_(converter) { | 123 value_converter_(converter) { |
124 } | 124 } |
125 | 125 |
126 virtual bool ConvertField( | 126 bool ConvertField(const base::Value& value, StructType* dst) const override { |
127 const base::Value& value, StructType* dst) const override { | |
128 return value_converter_->Convert(value, &(dst->*field_pointer_)); | 127 return value_converter_->Convert(value, &(dst->*field_pointer_)); |
129 } | 128 } |
130 | 129 |
131 private: | 130 private: |
132 FieldType StructType::* field_pointer_; | 131 FieldType StructType::* field_pointer_; |
133 scoped_ptr<ValueConverter<FieldType> > value_converter_; | 132 scoped_ptr<ValueConverter<FieldType> > value_converter_; |
134 DISALLOW_COPY_AND_ASSIGN(FieldConverter); | 133 DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
135 }; | 134 }; |
136 | 135 |
137 template <typename FieldType> | 136 template <typename FieldType> |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 }; | 194 }; |
196 | 195 |
197 template <typename FieldType> | 196 template <typename FieldType> |
198 class ValueFieldConverter : public ValueConverter<FieldType> { | 197 class ValueFieldConverter : public ValueConverter<FieldType> { |
199 public: | 198 public: |
200 typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); | 199 typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); |
201 | 200 |
202 ValueFieldConverter(ConvertFunc convert_func) | 201 ValueFieldConverter(ConvertFunc convert_func) |
203 : convert_func_(convert_func) {} | 202 : convert_func_(convert_func) {} |
204 | 203 |
205 virtual bool Convert(const base::Value& value, | 204 bool Convert(const base::Value& value, FieldType* field) const override { |
206 FieldType* field) const override { | |
207 return convert_func_(&value, field); | 205 return convert_func_(&value, field); |
208 } | 206 } |
209 | 207 |
210 private: | 208 private: |
211 ConvertFunc convert_func_; | 209 ConvertFunc convert_func_; |
212 | 210 |
213 DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); | 211 DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); |
214 }; | 212 }; |
215 | 213 |
216 template <typename FieldType> | 214 template <typename FieldType> |
217 class CustomFieldConverter : public ValueConverter<FieldType> { | 215 class CustomFieldConverter : public ValueConverter<FieldType> { |
218 public: | 216 public: |
219 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); | 217 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); |
220 | 218 |
221 CustomFieldConverter(ConvertFunc convert_func) | 219 CustomFieldConverter(ConvertFunc convert_func) |
222 : convert_func_(convert_func) {} | 220 : convert_func_(convert_func) {} |
223 | 221 |
224 virtual bool Convert(const base::Value& value, | 222 bool Convert(const base::Value& value, FieldType* field) const override { |
225 FieldType* field) const override { | |
226 std::string string_value; | 223 std::string string_value; |
227 return value.GetAsString(&string_value) && | 224 return value.GetAsString(&string_value) && |
228 convert_func_(string_value, field); | 225 convert_func_(string_value, field); |
229 } | 226 } |
230 | 227 |
231 private: | 228 private: |
232 ConvertFunc convert_func_; | 229 ConvertFunc convert_func_; |
233 | 230 |
234 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); | 231 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); |
235 }; | 232 }; |
236 | 233 |
237 template <typename NestedType> | 234 template <typename NestedType> |
238 class NestedValueConverter : public ValueConverter<NestedType> { | 235 class NestedValueConverter : public ValueConverter<NestedType> { |
239 public: | 236 public: |
240 NestedValueConverter() {} | 237 NestedValueConverter() {} |
241 | 238 |
242 virtual bool Convert( | 239 bool Convert(const base::Value& value, NestedType* field) const override { |
243 const base::Value& value, NestedType* field) const override { | |
244 return converter_.Convert(value, field); | 240 return converter_.Convert(value, field); |
245 } | 241 } |
246 | 242 |
247 private: | 243 private: |
248 JSONValueConverter<NestedType> converter_; | 244 JSONValueConverter<NestedType> converter_; |
249 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); | 245 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); |
250 }; | 246 }; |
251 | 247 |
252 template <typename Element> | 248 template <typename Element> |
253 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { | 249 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { |
254 public: | 250 public: |
255 RepeatedValueConverter() {} | 251 RepeatedValueConverter() {} |
256 | 252 |
257 virtual bool Convert( | 253 bool Convert(const base::Value& value, |
258 const base::Value& value, ScopedVector<Element>* field) const override { | 254 ScopedVector<Element>* field) const override { |
259 const base::ListValue* list = NULL; | 255 const base::ListValue* list = NULL; |
260 if (!value.GetAsList(&list)) { | 256 if (!value.GetAsList(&list)) { |
261 // The field is not a list. | 257 // The field is not a list. |
262 return false; | 258 return false; |
263 } | 259 } |
264 | 260 |
265 field->reserve(list->GetSize()); | 261 field->reserve(list->GetSize()); |
266 for (size_t i = 0; i < list->GetSize(); ++i) { | 262 for (size_t i = 0; i < list->GetSize(); ++i) { |
267 const base::Value* element = NULL; | 263 const base::Value* element = NULL; |
268 if (!list->Get(i, &element)) | 264 if (!list->Get(i, &element)) |
(...skipping 14 matching lines...) Expand all Loading... |
283 BasicValueConverter<Element> basic_converter_; | 279 BasicValueConverter<Element> basic_converter_; |
284 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); | 280 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); |
285 }; | 281 }; |
286 | 282 |
287 template <typename NestedType> | 283 template <typename NestedType> |
288 class RepeatedMessageConverter | 284 class RepeatedMessageConverter |
289 : public ValueConverter<ScopedVector<NestedType> > { | 285 : public ValueConverter<ScopedVector<NestedType> > { |
290 public: | 286 public: |
291 RepeatedMessageConverter() {} | 287 RepeatedMessageConverter() {} |
292 | 288 |
293 virtual bool Convert(const base::Value& value, | 289 bool Convert(const base::Value& value, |
294 ScopedVector<NestedType>* field) const override { | 290 ScopedVector<NestedType>* field) const override { |
295 const base::ListValue* list = NULL; | 291 const base::ListValue* list = NULL; |
296 if (!value.GetAsList(&list)) | 292 if (!value.GetAsList(&list)) |
297 return false; | 293 return false; |
298 | 294 |
299 field->reserve(list->GetSize()); | 295 field->reserve(list->GetSize()); |
300 for (size_t i = 0; i < list->GetSize(); ++i) { | 296 for (size_t i = 0; i < list->GetSize(); ++i) { |
301 const base::Value* element = NULL; | 297 const base::Value* element = NULL; |
302 if (!list->Get(i, &element)) | 298 if (!list->Get(i, &element)) |
303 continue; | 299 continue; |
304 | 300 |
(...skipping 15 matching lines...) Expand all Loading... |
320 | 316 |
321 template <typename NestedType> | 317 template <typename NestedType> |
322 class RepeatedCustomValueConverter | 318 class RepeatedCustomValueConverter |
323 : public ValueConverter<ScopedVector<NestedType> > { | 319 : public ValueConverter<ScopedVector<NestedType> > { |
324 public: | 320 public: |
325 typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); | 321 typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); |
326 | 322 |
327 RepeatedCustomValueConverter(ConvertFunc convert_func) | 323 RepeatedCustomValueConverter(ConvertFunc convert_func) |
328 : convert_func_(convert_func) {} | 324 : convert_func_(convert_func) {} |
329 | 325 |
330 virtual bool Convert(const base::Value& value, | 326 bool Convert(const base::Value& value, |
331 ScopedVector<NestedType>* field) const override { | 327 ScopedVector<NestedType>* field) const override { |
332 const base::ListValue* list = NULL; | 328 const base::ListValue* list = NULL; |
333 if (!value.GetAsList(&list)) | 329 if (!value.GetAsList(&list)) |
334 return false; | 330 return false; |
335 | 331 |
336 field->reserve(list->GetSize()); | 332 field->reserve(list->GetSize()); |
337 for (size_t i = 0; i < list->GetSize(); ++i) { | 333 for (size_t i = 0; i < list->GetSize(); ++i) { |
338 const base::Value* element = NULL; | 334 const base::Value* element = NULL; |
339 if (!list->Get(i, &element)) | 335 if (!list->Get(i, &element)) |
340 continue; | 336 continue; |
341 | 337 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 | 505 |
510 private: | 506 private: |
511 ScopedVector<internal::FieldConverterBase<StructType> > fields_; | 507 ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
512 | 508 |
513 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 509 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
514 }; | 510 }; |
515 | 511 |
516 } // namespace base | 512 } // namespace base |
517 | 513 |
518 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 514 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
OLD | NEW |