| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/base_export.h" | |
| 13 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 14 #include "base/logging.h" | 13 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 17 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 18 #include "base/string16.h" | 17 #include "base/string16.h" |
| 19 #include "base/string_piece.h" | 18 #include "base/string_piece.h" |
| 20 #include "base/values.h" | 19 #include "base/values.h" |
| 21 | 20 |
| 22 // JSONValueConverter converts a JSON value into a C++ struct in a | 21 // JSONValueConverter converts a JSON value into a C++ struct in a |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // } | 84 // } |
| 86 // }; | 85 // }; |
| 87 | 86 |
| 88 namespace base { | 87 namespace base { |
| 89 | 88 |
| 90 template <typename StructType> | 89 template <typename StructType> |
| 91 class JSONValueConverter; | 90 class JSONValueConverter; |
| 92 | 91 |
| 93 namespace internal { | 92 namespace internal { |
| 94 | 93 |
| 94 template<typename StructType> |
| 95 class FieldConverterBase { | 95 class FieldConverterBase { |
| 96 public: | 96 public: |
| 97 BASE_EXPORT explicit FieldConverterBase(const std::string& path); | 97 explicit FieldConverterBase(const std::string& path) : field_path_(path) {} |
| 98 BASE_EXPORT virtual ~FieldConverterBase(); | 98 virtual ~FieldConverterBase() {} |
| 99 virtual bool ConvertField(const base::Value& value, void* obj) const = 0; | 99 virtual bool ConvertField(const base::Value& value, StructType* obj) |
| 100 const = 0; |
| 100 const std::string& field_path() const { return field_path_; } | 101 const std::string& field_path() const { return field_path_; } |
| 101 | 102 |
| 102 private: | 103 private: |
| 103 std::string field_path_; | 104 std::string field_path_; |
| 104 DISALLOW_COPY_AND_ASSIGN(FieldConverterBase); | 105 DISALLOW_COPY_AND_ASSIGN(FieldConverterBase); |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 template <typename FieldType> | 108 template <typename FieldType> |
| 108 class ValueConverter { | 109 class ValueConverter { |
| 109 public: | 110 public: |
| 110 virtual ~ValueConverter() {} | 111 virtual ~ValueConverter() {} |
| 111 virtual bool Convert(const base::Value& value, FieldType* field) const = 0; | 112 virtual bool Convert(const base::Value& value, FieldType* field) const = 0; |
| 112 }; | 113 }; |
| 113 | 114 |
| 114 template <typename StructType, typename FieldType> | 115 template <typename StructType, typename FieldType> |
| 115 class FieldConverter : public FieldConverterBase { | 116 class FieldConverter : public FieldConverterBase<StructType> { |
| 116 public: | 117 public: |
| 117 explicit FieldConverter(const std::string& path, | 118 explicit FieldConverter(const std::string& path, |
| 118 FieldType StructType::* field, | 119 FieldType StructType::* field, |
| 119 ValueConverter<FieldType>* converter) | 120 ValueConverter<FieldType>* converter) |
| 120 : FieldConverterBase(path), | 121 : FieldConverterBase<StructType>(path), |
| 121 field_pointer_(field), | 122 field_pointer_(field), |
| 122 value_converter_(converter) { | 123 value_converter_(converter) { |
| 123 } | 124 } |
| 124 | 125 |
| 125 virtual bool ConvertField( | 126 virtual bool ConvertField( |
| 126 const base::Value& value, void* obj) const OVERRIDE { | 127 const base::Value& value, StructType* dst) const OVERRIDE { |
| 127 StructType* dst = reinterpret_cast<StructType*>(obj); | |
| 128 return value_converter_->Convert(value, &(dst->*field_pointer_)); | 128 return value_converter_->Convert(value, &(dst->*field_pointer_)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 private: | 131 private: |
| 132 FieldType StructType::* field_pointer_; | 132 FieldType StructType::* field_pointer_; |
| 133 scoped_ptr<ValueConverter<FieldType> > value_converter_; | 133 scoped_ptr<ValueConverter<FieldType> > value_converter_; |
| 134 DISALLOW_COPY_AND_ASSIGN(FieldConverter); | 134 DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 template <typename FieldType> | 137 template <typename FieldType> |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 field, | 416 field, |
| 417 new internal::RepeatedMessageConverter<NestedType>)); | 417 new internal::RepeatedMessageConverter<NestedType>)); |
| 418 } | 418 } |
| 419 | 419 |
| 420 bool Convert(const base::Value& value, StructType* output) const { | 420 bool Convert(const base::Value& value, StructType* output) const { |
| 421 const DictionaryValue* dictionary_value = NULL; | 421 const DictionaryValue* dictionary_value = NULL; |
| 422 if (!value.GetAsDictionary(&dictionary_value)) | 422 if (!value.GetAsDictionary(&dictionary_value)) |
| 423 return false; | 423 return false; |
| 424 | 424 |
| 425 for(size_t i = 0; i < fields_.size(); ++i) { | 425 for(size_t i = 0; i < fields_.size(); ++i) { |
| 426 const internal::FieldConverterBase* field_converter = fields_[i]; | 426 const internal::FieldConverterBase<StructType>* field_converter = |
| 427 fields_[i]; |
| 427 base::Value* field = NULL; | 428 base::Value* field = NULL; |
| 428 if (dictionary_value->Get(field_converter->field_path(), &field)) { | 429 if (dictionary_value->Get(field_converter->field_path(), &field)) { |
| 429 if (!field_converter->ConvertField(*field, output)) { | 430 if (!field_converter->ConvertField(*field, output)) { |
| 430 DVLOG(1) << "failure at field " << field_converter->field_path(); | 431 DVLOG(1) << "failure at field " << field_converter->field_path(); |
| 431 return false; | 432 return false; |
| 432 } | 433 } |
| 433 } | 434 } |
| 434 } | 435 } |
| 435 return true; | 436 return true; |
| 436 } | 437 } |
| 437 | 438 |
| 438 private: | 439 private: |
| 439 ScopedVector<internal::FieldConverterBase> fields_; | 440 ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
| 440 | 441 |
| 441 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 442 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
| 442 }; | 443 }; |
| 443 | 444 |
| 444 } // namespace base | 445 } // namespace base |
| 445 | 446 |
| 446 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 447 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| OLD | NEW |