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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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. |
69 // | 70 // |
| 71 // Sometimes JSON format uses string representations for other types such |
| 72 // like enum, timestamp, or URL. You can use RegisterCustomField method |
| 73 // and specify a function to convert a StringPiece to your type. |
| 74 // bool ConvertFunc(const StringPiece& s, YourEnum* result) { |
| 75 // // do something and return true if succeed... |
| 76 // } |
| 77 // struct Message { |
| 78 // YourEnum ye; |
| 79 // ... |
| 80 // static void RegisterJSONConverter(...) { |
| 81 // ... |
| 82 // converter->RegsiterCustomField<YourEnum>( |
| 83 // "your_enum", &Message::ye, &ConvertFunc); |
| 84 // } |
| 85 // }; |
70 | 86 |
71 namespace base { | 87 namespace base { |
72 | 88 |
73 template <typename StructType> | 89 template <typename StructType> |
74 class JSONValueConverter; | 90 class JSONValueConverter; |
75 | 91 |
76 namespace internal { | 92 namespace internal { |
77 | 93 |
78 class FieldConverterBase { | 94 class FieldConverterBase { |
79 public: | 95 public: |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 BasicValueConverter() {} | 196 BasicValueConverter() {} |
181 | 197 |
182 virtual bool Convert(const base::Value& value, bool* field) const OVERRIDE { | 198 virtual bool Convert(const base::Value& value, bool* field) const OVERRIDE { |
183 return value.GetAsBoolean(field); | 199 return value.GetAsBoolean(field); |
184 } | 200 } |
185 | 201 |
186 private: | 202 private: |
187 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 203 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
188 }; | 204 }; |
189 | 205 |
| 206 template <typename FieldType> |
| 207 class CustomFieldConverter : public ValueConverter<FieldType> { |
| 208 public: |
| 209 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); |
| 210 |
| 211 CustomFieldConverter(ConvertFunc convert_func) |
| 212 : convert_func_(convert_func) {} |
| 213 |
| 214 virtual bool Convert(const base::Value& value, |
| 215 FieldType* field) const OVERRIDE { |
| 216 std::string string_value; |
| 217 return value.GetAsString(&string_value) && |
| 218 convert_func_(string_value, field); |
| 219 } |
| 220 |
| 221 private: |
| 222 ConvertFunc convert_func_; |
| 223 |
| 224 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); |
| 225 }; |
| 226 |
190 template <typename NestedType> | 227 template <typename NestedType> |
191 class NestedValueConverter : public ValueConverter<NestedType> { | 228 class NestedValueConverter : public ValueConverter<NestedType> { |
192 public: | 229 public: |
193 NestedValueConverter() {} | 230 NestedValueConverter() {} |
194 | 231 |
195 virtual bool Convert( | 232 virtual bool Convert( |
196 const base::Value& value, NestedType* field) const OVERRIDE { | 233 const base::Value& value, NestedType* field) const OVERRIDE { |
197 return converter_.Convert(value, field); | 234 return converter_.Convert(value, field); |
198 } | 235 } |
199 | 236 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 | 350 |
314 template <class NestedType> | 351 template <class NestedType> |
315 void RegisterNestedField( | 352 void RegisterNestedField( |
316 const std::string& field_name, NestedType StructType::* field) { | 353 const std::string& field_name, NestedType StructType::* field) { |
317 fields_.push_back(new internal::FieldConverter<StructType, NestedType>( | 354 fields_.push_back(new internal::FieldConverter<StructType, NestedType>( |
318 field_name, | 355 field_name, |
319 field, | 356 field, |
320 new internal::NestedValueConverter<NestedType>)); | 357 new internal::NestedValueConverter<NestedType>)); |
321 } | 358 } |
322 | 359 |
| 360 template <typename FieldType> |
| 361 void RegisterCustomField( |
| 362 const std::string& field_name, |
| 363 FieldType StructType::* field, |
| 364 bool (*convert_func)(const StringPiece&, FieldType*)) { |
| 365 fields_.push_back(new internal::FieldConverter<StructType, FieldType>( |
| 366 field_name, |
| 367 field, |
| 368 new internal::CustomFieldConverter<FieldType>(convert_func))); |
| 369 } |
| 370 |
323 void RegisterRepeatedInt(const std::string& field_name, | 371 void RegisterRepeatedInt(const std::string& field_name, |
324 std::vector<int> StructType::* field) { | 372 std::vector<int> StructType::* field) { |
325 fields_.push_back( | 373 fields_.push_back( |
326 new internal::FieldConverter<StructType, std::vector<int> >( | 374 new internal::FieldConverter<StructType, std::vector<int> >( |
327 field_name, field, new internal::RepeatedValueConverter<int>)); | 375 field_name, field, new internal::RepeatedValueConverter<int>)); |
328 } | 376 } |
329 | 377 |
330 void RegisterRepeatedString(const std::string& field_name, | 378 void RegisterRepeatedString(const std::string& field_name, |
331 std::vector<std::string> StructType::* field) { | 379 std::vector<std::string> StructType::* field) { |
332 fields_.push_back( | 380 fields_.push_back( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 | 437 |
390 private: | 438 private: |
391 std::vector<internal::FieldConverterBase*> fields_; | 439 std::vector<internal::FieldConverterBase*> fields_; |
392 | 440 |
393 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 441 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
394 }; | 442 }; |
395 | 443 |
396 } // namespace base | 444 } // namespace base |
397 | 445 |
398 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 446 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
OLD | NEW |