Chromium Code Reviews| 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" | 12 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/string16.h" | 17 #include "base/string16.h" |
| 18 #include "base/string_piece.h" | |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 | 20 |
| 20 // JSONValueConverter converts a JSON value into a C++ struct in a | 21 // JSONValueConverter converts a JSON value into a C++ struct in a |
| 21 // lightweight way. | 22 // lightweight way. |
| 22 // | 23 // |
| 23 // Usage: | 24 // Usage: |
| 24 // For real examples, you may want to refer to _unittest.cc file. | 25 // For real examples, you may want to refer to _unittest.cc file. |
| 25 // | 26 // |
| 26 // Assume that you have a struct like this: | 27 // Assume that you have a struct like this: |
| 27 // struct Message { | 28 // struct Message { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 58 // struct Nested { | 59 // struct Nested { |
| 59 // Message foo; | 60 // Message foo; |
| 60 // static void RegisterJSONConverter(...) { | 61 // static void RegisterJSONConverter(...) { |
| 61 // ... | 62 // ... |
| 62 // converter->RegisterNestedField("foo", &Nested::foo); | 63 // converter->RegisterNestedField("foo", &Nested::foo); |
| 63 // } | 64 // } |
| 64 // }; | 65 // }; |
| 65 // | 66 // |
| 66 // For repeated field, we just assume std::vector for its container | 67 // For repeated field, we just assume std::vector for its container |
| 67 // and you can put RegisterRepeatedInt or some other types. Use | 68 // and you can put RegisterRepeatedInt or some other types. Use |
| 68 // RegisterRepeatedMessage for nested repeated fields. | 69 // RegisterRepeatedMessage for nested repeated fields. |
|
awong
2012/01/11 17:32:13
Can we add some documentation in here to explain h
Jun Mukai
2012/01/12 07:17:18
Done.
| |
| 69 // | 70 // |
| 70 | 71 |
| 71 namespace base { | 72 namespace base { |
| 72 | 73 |
| 73 template <typename StructType> | 74 template <typename StructType> |
| 74 class JSONValueConverter; | 75 class JSONValueConverter; |
| 75 | 76 |
| 76 namespace internal { | 77 namespace internal { |
| 77 | 78 |
| 78 class FieldConverterBase { | 79 class FieldConverterBase { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 BasicValueConverter() {} | 181 BasicValueConverter() {} |
| 181 | 182 |
| 182 virtual bool Convert(const base::Value& value, bool* field) const OVERRIDE { | 183 virtual bool Convert(const base::Value& value, bool* field) const OVERRIDE { |
| 183 return value.GetAsBoolean(field); | 184 return value.GetAsBoolean(field); |
| 184 } | 185 } |
| 185 | 186 |
| 186 private: | 187 private: |
| 187 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 188 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 188 }; | 189 }; |
| 189 | 190 |
| 191 template <typename FieldType> | |
| 192 class CustomFieldConverter : public ValueConverter<FieldType> { | |
| 193 public: | |
| 194 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); | |
| 195 | |
| 196 CustomFieldConverter(ConvertFunc convert_func) | |
| 197 : convert_func_(convert_func) {} | |
| 198 | |
| 199 virtual bool Convert(const base::Value& value, | |
| 200 FieldType* field) const OVERRIDE { | |
| 201 std::string string_value; | |
| 202 return value.GetAsString(&string_value) && | |
| 203 convert_func_(string_value, field); | |
| 204 } | |
| 205 | |
| 206 private: | |
| 207 ConvertFunc convert_func_; | |
| 208 | |
| 209 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); | |
| 210 }; | |
| 211 | |
| 190 template <typename NestedType> | 212 template <typename NestedType> |
| 191 class NestedValueConverter : public ValueConverter<NestedType> { | 213 class NestedValueConverter : public ValueConverter<NestedType> { |
| 192 public: | 214 public: |
| 193 NestedValueConverter() {} | 215 NestedValueConverter() {} |
| 194 | 216 |
| 195 virtual bool Convert( | 217 virtual bool Convert( |
| 196 const base::Value& value, NestedType* field) const OVERRIDE { | 218 const base::Value& value, NestedType* field) const OVERRIDE { |
| 197 return converter_.Convert(value, field); | 219 return converter_.Convert(value, field); |
| 198 } | 220 } |
| 199 | 221 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 | 335 |
| 314 template <class NestedType> | 336 template <class NestedType> |
| 315 void RegisterNestedField( | 337 void RegisterNestedField( |
| 316 const std::string& field_name, NestedType StructType::* field) { | 338 const std::string& field_name, NestedType StructType::* field) { |
| 317 fields_.push_back(new internal::FieldConverter<StructType, NestedType>( | 339 fields_.push_back(new internal::FieldConverter<StructType, NestedType>( |
| 318 field_name, | 340 field_name, |
| 319 field, | 341 field, |
| 320 new internal::NestedValueConverter<NestedType>)); | 342 new internal::NestedValueConverter<NestedType>)); |
| 321 } | 343 } |
| 322 | 344 |
| 345 template <typename FieldType> | |
| 346 void RegisterCustomField( | |
| 347 const std::string& field_name, | |
| 348 FieldType StructType::* field, | |
| 349 bool (*convert_func)(const StringPiece&, FieldType*)) { | |
| 350 fields_.push_back(new internal::FieldConverter<StructType, FieldType>( | |
| 351 field_name, | |
| 352 field, | |
| 353 new internal::CustomFieldConverter<FieldType>(convert_func))); | |
| 354 } | |
| 355 | |
| 323 void RegisterRepeatedInt(const std::string& field_name, | 356 void RegisterRepeatedInt(const std::string& field_name, |
| 324 std::vector<int> StructType::* field) { | 357 std::vector<int> StructType::* field) { |
| 325 fields_.push_back( | 358 fields_.push_back( |
| 326 new internal::FieldConverter<StructType, std::vector<int> >( | 359 new internal::FieldConverter<StructType, std::vector<int> >( |
| 327 field_name, field, new internal::RepeatedValueConverter<int>)); | 360 field_name, field, new internal::RepeatedValueConverter<int>)); |
| 328 } | 361 } |
| 329 | 362 |
| 330 void RegisterRepeatedString(const std::string& field_name, | 363 void RegisterRepeatedString(const std::string& field_name, |
| 331 std::vector<std::string> StructType::* field) { | 364 std::vector<std::string> StructType::* field) { |
| 332 fields_.push_back( | 365 fields_.push_back( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 | 422 |
| 390 private: | 423 private: |
| 391 std::vector<internal::FieldConverterBase*> fields_; | 424 std::vector<internal::FieldConverterBase*> fields_; |
| 392 | 425 |
| 393 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 426 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
| 394 }; | 427 }; |
| 395 | 428 |
| 396 } // namespace base | 429 } // namespace base |
| 397 | 430 |
| 398 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 431 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| OLD | NEW |