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