[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| 6 | #define BASE_JSON_JSON_VALUE_CONVERTER_H_ |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 10 | #include <memory> |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 14 | #include "base/base_export.h" |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 15 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 17 | #include "base/memory/scoped_vector.h" |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 18 | #include "base/stl_util.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 19 | #include "base/strings/string16.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 20 | #include "base/strings/string_piece.h" |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 21 | #include "base/values.h" |
| 22 | |
| 23 | // JSONValueConverter converts a JSON value into a C++ struct in a |
| 24 | // lightweight way. |
| 25 | // |
| 26 | // Usage: |
| 27 | // For real examples, you may want to refer to _unittest.cc file. |
| 28 | // |
| 29 | // Assume that you have a struct like this: |
| 30 | // struct Message { |
| 31 | // int foo; |
| 32 | // std::string bar; |
| 33 | // static void RegisterJSONConverter( |
| 34 | // JSONValueConverter<Message>* converter); |
| 35 | // }; |
| 36 | // |
| 37 | // And you want to parse a json data into this struct. First, you |
| 38 | // need to declare RegisterJSONConverter() method in your struct. |
| 39 | // // static |
| 40 | // void Message::RegisterJSONConverter( |
| 41 | // JSONValueConverter<Message>* converter) { |
| 42 | // converter->RegisterIntField("foo", &Message::foo); |
| 43 | // converter->RegisterStringField("bar", &Message::bar); |
| 44 | // } |
| 45 | // |
| 46 | // Then, you just instantiate your JSONValueConverter of your type and call |
| 47 | // Convert() method. |
| 48 | // Message message; |
| 49 | // JSONValueConverter<Message> converter; |
| 50 | // converter.Convert(json, &message); |
| 51 | // |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 52 | // Convert() returns false when it fails. Here "fail" means that the value is |
| 53 | // structurally different from expected, such like a string value appears |
| 54 | // for an int field. Do not report failures for missing fields. |
| 55 | // Also note that Convert() will modify the passed |message| even when it |
| 56 | // fails for performance reason. |
| 57 | // |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 58 | // For nested field, the internal message also has to implement the registration |
| 59 | // method. Then, just use RegisterNestedField() from the containing struct's |
| 60 | // RegisterJSONConverter method. |
| 61 | // struct Nested { |
| 62 | // Message foo; |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 63 | // static void RegisterJSONConverter(...) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 64 | // ... |
| 65 | // converter->RegisterNestedField("foo", &Nested::foo); |
| 66 | // } |
| 67 | // }; |
| 68 | // |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 69 | // For repeated field, we just assume ScopedVector for its container |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 70 | // and you can put RegisterRepeatedInt or some other types. Use |
| 71 | // RegisterRepeatedMessage for nested repeated fields. |
| 72 | // |
[email protected] | 6009ca9 | 2012-01-13 02:18:02 | [diff] [blame] | 73 | // Sometimes JSON format uses string representations for other types such |
| 74 | // like enum, timestamp, or URL. You can use RegisterCustomField method |
| 75 | // and specify a function to convert a StringPiece to your type. |
| 76 | // bool ConvertFunc(const StringPiece& s, YourEnum* result) { |
| 77 | // // do something and return true if succeed... |
| 78 | // } |
| 79 | // struct Message { |
| 80 | // YourEnum ye; |
| 81 | // ... |
| 82 | // static void RegisterJSONConverter(...) { |
| 83 | // ... |
| 84 | // converter->RegsiterCustomField<YourEnum>( |
| 85 | // "your_enum", &Message::ye, &ConvertFunc); |
| 86 | // } |
| 87 | // }; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 88 | |
| 89 | namespace base { |
| 90 | |
| 91 | template <typename StructType> |
| 92 | class JSONValueConverter; |
| 93 | |
| 94 | namespace internal { |
| 95 | |
[email protected] | 983b9b7 | 2012-01-27 07:12:21 | [diff] [blame] | 96 | template<typename StructType> |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 97 | class FieldConverterBase { |
| 98 | public: |
[email protected] | 983b9b7 | 2012-01-27 07:12:21 | [diff] [blame] | 99 | explicit FieldConverterBase(const std::string& path) : field_path_(path) {} |
| 100 | virtual ~FieldConverterBase() {} |
| 101 | virtual bool ConvertField(const base::Value& value, StructType* obj) |
| 102 | const = 0; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 103 | const std::string& field_path() const { return field_path_; } |
| 104 | |
| 105 | private: |
| 106 | std::string field_path_; |
| 107 | DISALLOW_COPY_AND_ASSIGN(FieldConverterBase); |
| 108 | }; |
| 109 | |
| 110 | template <typename FieldType> |
| 111 | class ValueConverter { |
| 112 | public: |
| 113 | virtual ~ValueConverter() {} |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 114 | virtual bool Convert(const base::Value& value, FieldType* field) const = 0; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | template <typename StructType, typename FieldType> |
[email protected] | 983b9b7 | 2012-01-27 07:12:21 | [diff] [blame] | 118 | class FieldConverter : public FieldConverterBase<StructType> { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 119 | public: |
| 120 | explicit FieldConverter(const std::string& path, |
| 121 | FieldType StructType::* field, |
| 122 | ValueConverter<FieldType>* converter) |
[email protected] | 983b9b7 | 2012-01-27 07:12:21 | [diff] [blame] | 123 | : FieldConverterBase<StructType>(path), |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 124 | field_pointer_(field), |
| 125 | value_converter_(converter) { |
| 126 | } |
| 127 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 128 | bool ConvertField(const base::Value& value, StructType* dst) const override { |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 129 | return value_converter_->Convert(value, &(dst->*field_pointer_)); |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private: |
| 133 | FieldType StructType::* field_pointer_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 134 | std::unique_ptr<ValueConverter<FieldType>> value_converter_; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
| 136 | }; |
| 137 | |
| 138 | template <typename FieldType> |
| 139 | class BasicValueConverter; |
| 140 | |
| 141 | template <> |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 142 | class BASE_EXPORT BasicValueConverter<int> : public ValueConverter<int> { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 143 | public: |
| 144 | BasicValueConverter() {} |
| 145 | |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 146 | bool Convert(const base::Value& value, int* field) const override; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 147 | |
| 148 | private: |
| 149 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 150 | }; |
| 151 | |
| 152 | template <> |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 153 | class BASE_EXPORT BasicValueConverter<std::string> |
| 154 | : public ValueConverter<std::string> { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 155 | public: |
| 156 | BasicValueConverter() {} |
| 157 | |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 158 | bool Convert(const base::Value& value, std::string* field) const override; |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 159 | |
| 160 | private: |
| 161 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 162 | }; |
| 163 | |
| 164 | template <> |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 165 | class BASE_EXPORT BasicValueConverter<string16> |
| 166 | : public ValueConverter<string16> { |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 167 | public: |
| 168 | BasicValueConverter() {} |
| 169 | |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 170 | bool Convert(const base::Value& value, string16* field) const override; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 171 | |
| 172 | private: |
| 173 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 174 | }; |
| 175 | |
| 176 | template <> |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 177 | class BASE_EXPORT BasicValueConverter<double> : public ValueConverter<double> { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 178 | public: |
| 179 | BasicValueConverter() {} |
| 180 | |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 181 | bool Convert(const base::Value& value, double* field) const override; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 182 | |
| 183 | private: |
| 184 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 185 | }; |
| 186 | |
| 187 | template <> |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 188 | class BASE_EXPORT BasicValueConverter<bool> : public ValueConverter<bool> { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 189 | public: |
| 190 | BasicValueConverter() {} |
| 191 | |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 192 | bool Convert(const base::Value& value, bool* field) const override; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 193 | |
| 194 | private: |
| 195 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 196 | }; |
| 197 | |
[email protected] | 6009ca9 | 2012-01-13 02:18:02 | [diff] [blame] | 198 | template <typename FieldType> |
[email protected] | 038cf35 | 2012-04-13 00:14:15 | [diff] [blame] | 199 | class ValueFieldConverter : public ValueConverter<FieldType> { |
| 200 | public: |
| 201 | typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); |
| 202 | |
| 203 | ValueFieldConverter(ConvertFunc convert_func) |
| 204 | : convert_func_(convert_func) {} |
| 205 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 206 | bool Convert(const base::Value& value, FieldType* field) const override { |
[email protected] | 038cf35 | 2012-04-13 00:14:15 | [diff] [blame] | 207 | return convert_func_(&value, field); |
| 208 | } |
| 209 | |
| 210 | private: |
| 211 | ConvertFunc convert_func_; |
| 212 | |
| 213 | DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); |
| 214 | }; |
| 215 | |
| 216 | template <typename FieldType> |
[email protected] | 6009ca9 | 2012-01-13 02:18:02 | [diff] [blame] | 217 | class CustomFieldConverter : public ValueConverter<FieldType> { |
| 218 | public: |
| 219 | typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); |
| 220 | |
| 221 | CustomFieldConverter(ConvertFunc convert_func) |
| 222 | : convert_func_(convert_func) {} |
| 223 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 224 | bool Convert(const base::Value& value, FieldType* field) const override { |
[email protected] | 6009ca9 | 2012-01-13 02:18:02 | [diff] [blame] | 225 | std::string string_value; |
| 226 | return value.GetAsString(&string_value) && |
| 227 | convert_func_(string_value, field); |
| 228 | } |
| 229 | |
| 230 | private: |
| 231 | ConvertFunc convert_func_; |
| 232 | |
| 233 | DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); |
| 234 | }; |
| 235 | |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 236 | template <typename NestedType> |
| 237 | class NestedValueConverter : public ValueConverter<NestedType> { |
| 238 | public: |
| 239 | NestedValueConverter() {} |
| 240 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 241 | bool Convert(const base::Value& value, NestedType* field) const override { |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 242 | return converter_.Convert(value, field); |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | private: |
| 246 | JSONValueConverter<NestedType> converter_; |
| 247 | DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); |
| 248 | }; |
| 249 | |
| 250 | template <typename Element> |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 251 | class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 252 | public: |
| 253 | RepeatedValueConverter() {} |
| 254 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 255 | bool Convert(const base::Value& value, |
| 256 | ScopedVector<Element>* field) const override { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 257 | const base::ListValue* list = NULL; |
| 258 | if (!value.GetAsList(&list)) { |
| 259 | // The field is not a list. |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 260 | return false; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | field->reserve(list->GetSize()); |
| 264 | for (size_t i = 0; i < list->GetSize(); ++i) { |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 265 | const base::Value* element = NULL; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 266 | if (!list->Get(i, &element)) |
| 267 | continue; |
| 268 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 269 | std::unique_ptr<Element> e(new Element); |
[email protected] | 50fa6f92 | 2012-01-26 19:32:36 | [diff] [blame] | 270 | if (basic_converter_.Convert(*element, e.get())) { |
| 271 | field->push_back(e.release()); |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 272 | } else { |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 273 | DVLOG(1) << "failure at " << i << "-th element"; |
| 274 | return false; |
| 275 | } |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 276 | } |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 277 | return true; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private: |
| 281 | BasicValueConverter<Element> basic_converter_; |
| 282 | DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); |
| 283 | }; |
| 284 | |
| 285 | template <typename NestedType> |
| 286 | class RepeatedMessageConverter |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 287 | : public ValueConverter<ScopedVector<NestedType> > { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 288 | public: |
| 289 | RepeatedMessageConverter() {} |
| 290 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 291 | bool Convert(const base::Value& value, |
| 292 | ScopedVector<NestedType>* field) const override { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 293 | const base::ListValue* list = NULL; |
| 294 | if (!value.GetAsList(&list)) |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 295 | return false; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 296 | |
| 297 | field->reserve(list->GetSize()); |
| 298 | for (size_t i = 0; i < list->GetSize(); ++i) { |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 299 | const base::Value* element = NULL; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 300 | if (!list->Get(i, &element)) |
| 301 | continue; |
| 302 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 303 | std::unique_ptr<NestedType> nested(new NestedType); |
[email protected] | 50fa6f92 | 2012-01-26 19:32:36 | [diff] [blame] | 304 | if (converter_.Convert(*element, nested.get())) { |
| 305 | field->push_back(nested.release()); |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 306 | } else { |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 307 | DVLOG(1) << "failure at " << i << "-th element"; |
| 308 | return false; |
| 309 | } |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 310 | } |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 311 | return true; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | private: |
| 315 | JSONValueConverter<NestedType> converter_; |
| 316 | DISALLOW_COPY_AND_ASSIGN(RepeatedMessageConverter); |
| 317 | }; |
| 318 | |
[email protected] | 4e1ec03 | 2012-05-03 18:31:54 | [diff] [blame] | 319 | template <typename NestedType> |
| 320 | class RepeatedCustomValueConverter |
| 321 | : public ValueConverter<ScopedVector<NestedType> > { |
| 322 | public: |
| 323 | typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); |
| 324 | |
| 325 | RepeatedCustomValueConverter(ConvertFunc convert_func) |
| 326 | : convert_func_(convert_func) {} |
| 327 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 328 | bool Convert(const base::Value& value, |
| 329 | ScopedVector<NestedType>* field) const override { |
[email protected] | 4e1ec03 | 2012-05-03 18:31:54 | [diff] [blame] | 330 | const base::ListValue* list = NULL; |
| 331 | if (!value.GetAsList(&list)) |
| 332 | return false; |
| 333 | |
| 334 | field->reserve(list->GetSize()); |
| 335 | for (size_t i = 0; i < list->GetSize(); ++i) { |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 336 | const base::Value* element = NULL; |
[email protected] | 4e1ec03 | 2012-05-03 18:31:54 | [diff] [blame] | 337 | if (!list->Get(i, &element)) |
| 338 | continue; |
| 339 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 340 | std::unique_ptr<NestedType> nested(new NestedType); |
[email protected] | 4e1ec03 | 2012-05-03 18:31:54 | [diff] [blame] | 341 | if ((*convert_func_)(element, nested.get())) { |
| 342 | field->push_back(nested.release()); |
| 343 | } else { |
| 344 | DVLOG(1) << "failure at " << i << "-th element"; |
| 345 | return false; |
| 346 | } |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | private: |
| 352 | ConvertFunc convert_func_; |
| 353 | DISALLOW_COPY_AND_ASSIGN(RepeatedCustomValueConverter); |
| 354 | }; |
| 355 | |
| 356 | |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 357 | } // namespace internal |
| 358 | |
| 359 | template <class StructType> |
| 360 | class JSONValueConverter { |
| 361 | public: |
| 362 | JSONValueConverter() { |
| 363 | StructType::RegisterJSONConverter(this); |
| 364 | } |
| 365 | |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 366 | void RegisterIntField(const std::string& field_name, |
| 367 | int StructType::* field) { |
| 368 | fields_.push_back(new internal::FieldConverter<StructType, int>( |
| 369 | field_name, field, new internal::BasicValueConverter<int>)); |
| 370 | } |
| 371 | |
| 372 | void RegisterStringField(const std::string& field_name, |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 373 | std::string StructType::* field) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 374 | fields_.push_back(new internal::FieldConverter<StructType, std::string>( |
| 375 | field_name, field, new internal::BasicValueConverter<std::string>)); |
| 376 | } |
| 377 | |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 378 | void RegisterStringField(const std::string& field_name, |
| 379 | string16 StructType::* field) { |
| 380 | fields_.push_back(new internal::FieldConverter<StructType, string16>( |
| 381 | field_name, field, new internal::BasicValueConverter<string16>)); |
| 382 | } |
| 383 | |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 384 | void RegisterBoolField(const std::string& field_name, |
| 385 | bool StructType::* field) { |
| 386 | fields_.push_back(new internal::FieldConverter<StructType, bool>( |
| 387 | field_name, field, new internal::BasicValueConverter<bool>)); |
| 388 | } |
| 389 | |
| 390 | void RegisterDoubleField(const std::string& field_name, |
| 391 | double StructType::* field) { |
| 392 | fields_.push_back(new internal::FieldConverter<StructType, double>( |
| 393 | field_name, field, new internal::BasicValueConverter<double>)); |
| 394 | } |
| 395 | |
| 396 | template <class NestedType> |
| 397 | void RegisterNestedField( |
| 398 | const std::string& field_name, NestedType StructType::* field) { |
| 399 | fields_.push_back(new internal::FieldConverter<StructType, NestedType>( |
| 400 | field_name, |
| 401 | field, |
| 402 | new internal::NestedValueConverter<NestedType>)); |
| 403 | } |
| 404 | |
[email protected] | 6009ca9 | 2012-01-13 02:18:02 | [diff] [blame] | 405 | template <typename FieldType> |
| 406 | void RegisterCustomField( |
| 407 | const std::string& field_name, |
| 408 | FieldType StructType::* field, |
| 409 | bool (*convert_func)(const StringPiece&, FieldType*)) { |
| 410 | fields_.push_back(new internal::FieldConverter<StructType, FieldType>( |
| 411 | field_name, |
| 412 | field, |
| 413 | new internal::CustomFieldConverter<FieldType>(convert_func))); |
| 414 | } |
| 415 | |
[email protected] | 038cf35 | 2012-04-13 00:14:15 | [diff] [blame] | 416 | template <typename FieldType> |
| 417 | void RegisterCustomValueField( |
| 418 | const std::string& field_name, |
| 419 | FieldType StructType::* field, |
| 420 | bool (*convert_func)(const base::Value*, FieldType*)) { |
| 421 | fields_.push_back(new internal::FieldConverter<StructType, FieldType>( |
| 422 | field_name, |
| 423 | field, |
| 424 | new internal::ValueFieldConverter<FieldType>(convert_func))); |
| 425 | } |
| 426 | |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 427 | void RegisterRepeatedInt(const std::string& field_name, |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 428 | ScopedVector<int> StructType::* field) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 429 | fields_.push_back( |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 430 | new internal::FieldConverter<StructType, ScopedVector<int> >( |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 431 | field_name, field, new internal::RepeatedValueConverter<int>)); |
| 432 | } |
| 433 | |
| 434 | void RegisterRepeatedString(const std::string& field_name, |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 435 | ScopedVector<std::string> StructType::* field) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 436 | fields_.push_back( |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 437 | new internal::FieldConverter<StructType, ScopedVector<std::string> >( |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 438 | field_name, |
| 439 | field, |
| 440 | new internal::RepeatedValueConverter<std::string>)); |
| 441 | } |
| 442 | |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 443 | void RegisterRepeatedString(const std::string& field_name, |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 444 | ScopedVector<string16> StructType::* field) { |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 445 | fields_.push_back( |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 446 | new internal::FieldConverter<StructType, ScopedVector<string16> >( |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 447 | field_name, |
| 448 | field, |
| 449 | new internal::RepeatedValueConverter<string16>)); |
| 450 | } |
| 451 | |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 452 | void RegisterRepeatedDouble(const std::string& field_name, |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 453 | ScopedVector<double> StructType::* field) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 454 | fields_.push_back( |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 455 | new internal::FieldConverter<StructType, ScopedVector<double> >( |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 456 | field_name, field, new internal::RepeatedValueConverter<double>)); |
| 457 | } |
| 458 | |
| 459 | void RegisterRepeatedBool(const std::string& field_name, |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 460 | ScopedVector<bool> StructType::* field) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 461 | fields_.push_back( |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 462 | new internal::FieldConverter<StructType, ScopedVector<bool> >( |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 463 | field_name, field, new internal::RepeatedValueConverter<bool>)); |
| 464 | } |
| 465 | |
| 466 | template <class NestedType> |
[email protected] | 4e1ec03 | 2012-05-03 18:31:54 | [diff] [blame] | 467 | void RegisterRepeatedCustomValue( |
| 468 | const std::string& field_name, |
| 469 | ScopedVector<NestedType> StructType::* field, |
| 470 | bool (*convert_func)(const base::Value*, NestedType*)) { |
| 471 | fields_.push_back( |
| 472 | new internal::FieldConverter<StructType, ScopedVector<NestedType> >( |
| 473 | field_name, |
| 474 | field, |
| 475 | new internal::RepeatedCustomValueConverter<NestedType>( |
| 476 | convert_func))); |
| 477 | } |
| 478 | |
| 479 | template <class NestedType> |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 480 | void RegisterRepeatedMessage(const std::string& field_name, |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 481 | ScopedVector<NestedType> StructType::* field) { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 482 | fields_.push_back( |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 483 | new internal::FieldConverter<StructType, ScopedVector<NestedType> >( |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 484 | field_name, |
| 485 | field, |
| 486 | new internal::RepeatedMessageConverter<NestedType>)); |
| 487 | } |
| 488 | |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 489 | bool Convert(const base::Value& value, StructType* output) const { |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 490 | const DictionaryValue* dictionary_value = NULL; |
| 491 | if (!value.GetAsDictionary(&dictionary_value)) |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 492 | return false; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 493 | |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 494 | for(size_t i = 0; i < fields_.size(); ++i) { |
[email protected] | 983b9b7 | 2012-01-27 07:12:21 | [diff] [blame] | 495 | const internal::FieldConverterBase<StructType>* field_converter = |
| 496 | fields_[i]; |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 497 | const base::Value* field = NULL; |
[email protected] | a9b8e0a | 2012-01-14 05:36:12 | [diff] [blame] | 498 | if (dictionary_value->Get(field_converter->field_path(), &field)) { |
| 499 | if (!field_converter->ConvertField(*field, output)) { |
| 500 | DVLOG(1) << "failure at field " << field_converter->field_path(); |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 501 | return false; |
| 502 | } |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 503 | } |
| 504 | } |
[email protected] | 261c877e | 2012-01-05 09:46:42 | [diff] [blame] | 505 | return true; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | private: |
[email protected] | 983b9b7 | 2012-01-27 07:12:21 | [diff] [blame] | 509 | ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
[email protected] | 193f946b | 2011-12-22 18:31:47 | [diff] [blame] | 510 | |
| 511 | DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
| 512 | }; |
| 513 | |
| 514 | } // namespace base |
| 515 | |
| 516 | #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |