| 6 | #define BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 |
| 14 | #include "base/base_export.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/macros.h" |
| 17 | #include "base/memory/ptr_util.h" |
| 18 | #include "base/strings/string16.h" |
| 19 | #include "base/strings/string_piece.h" |
| 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 | // |
| 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 | // |
| 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; |
| 62 | // static void RegisterJSONConverter(...) { |
| 63 | // ... |
| 64 | // converter->RegisterNestedField("foo", &Nested::foo); |
| 65 | // } |
| 66 | // }; |
| 67 | // |
| 68 | // For repeated field, we just assume std::vector<std::unique_ptr<ElementType>> |
| 69 | // for its container and you can put RegisterRepeatedInt or some other types. |
| 70 | // Use RegisterRepeatedMessage for nested repeated fields. |
| 71 | // |
| 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(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 | // }; |
| 87 |
| 88 | namespace base { |
| 89 |
| 90 | template <typename StructType> |
| 91 | class JSONValueConverter; |
| 92 |
| 93 | namespace internal { |
| 94 |
| 95 | template<typename StructType> |
| 96 | class FieldConverterBase { |
| 97 | public: |
| 98 | explicit FieldConverterBase(const std::string& path) : field_path_(path) {} |
| 99 | virtual ~FieldConverterBase() = default; |
| 100 | virtual bool ConvertField(const base::Value& value, StructType* obj) |
| 101 | const = 0; |
| 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() = default; |
| 113 | virtual bool Convert(const base::Value& value, FieldType* field) const = 0; |
| 114 | }; |
| 115 |
| 116 | template <typename StructType, typename FieldType> |
| 117 | class FieldConverter : public FieldConverterBase<StructType> { |
| 118 | public: |
| 119 | explicit FieldConverter(const std::string& path, |
| 120 | FieldType StructType::* field, |
| 121 | ValueConverter<FieldType>* converter) |
| 122 | : FieldConverterBase<StructType>(path), |
| 123 | field_pointer_(field), |
| 124 | value_converter_(converter) { |
| 125 | } |
| 126 |
| 127 | bool ConvertField(const base::Value& value, StructType* dst) const override { |
| 128 | return value_converter_->Convert(value, &(dst->*field_pointer_)); |
| 129 | } |
| 130 |
| 131 | private: |
| 132 | FieldType StructType::* field_pointer_; |
| 133 | std::unique_ptr<ValueConverter<FieldType>> value_converter_; |
| 134 | DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
| 135 | }; |
| 136 |
| 137 | template <typename FieldType> |
| 138 | class BasicValueConverter; |
| 139 |
| 140 | template <> |
| 141 | class BASE_EXPORT BasicValueConverter<int> : public ValueConverter<int> { |
| 142 | public: |
| 143 | BasicValueConverter() = default; |
| 144 |
| 145 | bool Convert(const base::Value& value, int* field) const override; |
| 146 |
| 147 | private: |
| 148 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 149 | }; |
| 150 |
| 151 | template <> |
| 152 | class BASE_EXPORT BasicValueConverter<std::string> |
| 153 | : public ValueConverter<std::string> { |
| 154 | public: |
| 155 | BasicValueConverter() = default; |
| 156 |
| 157 | bool Convert(const base::Value& value, std::string* field) const override; |
| 158 |
| 159 | private: |
| 160 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 161 | }; |
| 162 |
| 163 | template <> |
| 164 | class BASE_EXPORT BasicValueConverter<string16> |
| 165 | : public ValueConverter<string16> { |
| 166 | public: |
| 167 | BasicValueConverter() = default; |
| 168 |
| 169 | bool Convert(const base::Value& value, string16* field) const override; |
| 170 |
| 171 | private: |
| 172 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 173 | }; |
| 174 |
| 175 | template <> |
| 176 | class BASE_EXPORT BasicValueConverter<double> : public ValueConverter<double> { |
| 177 | public: |
| 178 | BasicValueConverter() = default; |
| 179 |
| 180 | bool Convert(const base::Value& value, double* field) const override; |
| 181 |
| 182 | private: |
| 183 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 184 | }; |
| 185 |
| 186 | template <> |
| 187 | class BASE_EXPORT BasicValueConverter<bool> : public ValueConverter<bool> { |
| 188 | public: |
| 189 | BasicValueConverter() = default; |
| 190 |
| 191 | bool Convert(const base::Value& value, bool* field) const override; |
| 192 |
| 193 | private: |
| 194 | DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
| 195 | }; |
| 196 |
| 197 | template <typename FieldType> |
| 198 | class ValueFieldConverter : public ValueConverter<FieldType> { |
| 199 | public: |
| 200 | typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); |
| 201 |
| 202 | explicit ValueFieldConverter(ConvertFunc convert_func) |
| 203 | : convert_func_(convert_func) {} |
| 204 |
| 205 | bool Convert(const base::Value& value, FieldType* field) const override { |
| 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> |
| 216 | class CustomFieldConverter : public ValueConverter<FieldType> { |
| 217 | public: |
| 218 | typedef bool (*ConvertFunc)(StringPiece value, FieldType* field); |
| 219 |
| 220 | explicit CustomFieldConverter(ConvertFunc convert_func) |
| 221 | : convert_func_(convert_func) {} |
| 222 |
| 223 | bool Convert(const base::Value& value, FieldType* field) const override { |
| 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 |
| 235 | template <typename NestedType> |
| 236 | class NestedValueConverter : public ValueConverter<NestedType> { |
| 237 | public: |
| 238 | NestedValueConverter() = default; |
| 239 |
| 240 | bool Convert(const base::Value& value, NestedType* field) const override { |
| 241 | return converter_.Convert(value, field); |
| 242 | } |
| 243 |
| 244 | private: |
| 245 | JSONValueConverter<NestedType> converter_; |
| 246 | DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); |
| 247 | }; |
| 248 |
| 249 | template <typename Element> |
| 250 | class RepeatedValueConverter |
| 251 | : public ValueConverter<std::vector<std::unique_ptr<Element>>> { |
| 252 | public: |
| 253 | RepeatedValueConverter() = default; |
| 254 |
| 255 | bool Convert(const base::Value& value, |
| 256 | std::vector<std::unique_ptr<Element>>* field) const override { |
| 257 | if (!value.is_list()) { |
| 258 | // The field is not a list. |
| 259 | return false; |
| 260 | } |
| 261 |
| 262 | field->reserve(value.GetList().size()); |
| 263 | size_t i = 0; |
| 264 | for (const Value& element : value.GetList()) { |
| 265 | auto e = std::make_unique<Element>(); |
| 266 | if (basic_converter_.Convert(element, e.get())) { |
| 267 | field->push_back(std::move(e)); |
| 268 | } else { |
| 269 | DVLOG(1) << "failure at " << i << "-th element"; |
| 270 | return false; |
| 271 | } |
| 272 | i++; |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 |
| 277 | private: |
| 278 | BasicValueConverter<Element> basic_converter_; |
| 279 | DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); |
| 280 | }; |
| 281 |
| 282 | template <typename NestedType> |
| 283 | class RepeatedMessageConverter |
| 284 | : public ValueConverter<std::vector<std::unique_ptr<NestedType>>> { |
| 285 | public: |
| 286 | RepeatedMessageConverter() = default; |
| 287 |
| 288 | bool Convert(const base::Value& value, |
| 289 | std::vector<std::unique_ptr<NestedType>>* field) const override { |
| 290 | if (!value.is_list()) |
| 291 | return false; |
| 292 |
| 293 | field->reserve(value.GetList().size()); |
| 294 | size_t i = 0; |
| 295 | for (const Value& element : value.GetList()) { |
| 296 | auto nested = std::make_unique<NestedType>(); |
| 297 | if (converter_.Convert(element, nested.get())) { |
| 298 | field->push_back(std::move(nested)); |
| 299 | } else { |
| 300 | DVLOG(1) << "failure at " << i << "-th element"; |
| 301 | return false; |
| 302 | } |
| 303 | i++; |
| 304 | } |
| 305 | return true; |
| 306 | } |
| 307 |
| 308 | private: |
| 309 | JSONValueConverter<NestedType> converter_; |
| 310 | DISALLOW_COPY_AND_ASSIGN(RepeatedMessageConverter); |
| 311 | }; |
| 312 |
| 313 | template <typename NestedType> |
| 314 | class RepeatedCustomValueConverter |
| 315 | : public ValueConverter<std::vector<std::unique_ptr<NestedType>>> { |
| 316 | public: |
| 317 | typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); |
| 318 |
| 319 | explicit RepeatedCustomValueConverter(ConvertFunc convert_func) |
| 320 | : convert_func_(convert_func) {} |
| 321 |
| 322 | bool Convert(const base::Value& value, |
| 323 | std::vector<std::unique_ptr<NestedType>>* field) const override { |
| 324 | if (!value.is_list()) |
| 325 | return false; |
| 326 |
| 327 | field->reserve(value.GetList().size()); |
| 328 | size_t i = 0; |
| 329 | for (const Value& element : value.GetList()) { |
| 330 | auto nested = std::make_unique<NestedType>(); |
| 331 | if ((*convert_func_)(&element, nested.get())) { |
| 332 | field->push_back(std::move(nested)); |
| 333 | } else { |
| 334 | DVLOG(1) << "failure at " << i << "-th element"; |
| 335 | return false; |
| 336 | } |
| 337 | i++; |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 |
| 342 | private: |
| 343 | ConvertFunc convert_func_; |
| 344 | DISALLOW_COPY_AND_ASSIGN(RepeatedCustomValueConverter); |
| 345 | }; |
| 346 |
| 347 |
| 348 | } // namespace internal |
| 349 |
| 350 | template <class StructType> |
| 351 | class JSONValueConverter { |
| 352 | public: |
| 353 | JSONValueConverter() { |
| 354 | StructType::RegisterJSONConverter(this); |
| 355 | } |
| 356 |
| 357 | void RegisterIntField(const std::string& field_name, |
| 358 | int StructType::* field) { |
| 359 | fields_.push_back( |
| 360 | std::make_unique<internal::FieldConverter<StructType, int>>( |
| 361 | field_name, field, new internal::BasicValueConverter<int>)); |
| 362 | } |
| 363 |
| 364 | void RegisterStringField(const std::string& field_name, |
| 365 | std::string StructType::* field) { |
| 366 | fields_.push_back( |
| 367 | std::make_unique<internal::FieldConverter<StructType, std::string>>( |
| 368 | field_name, field, new internal::BasicValueConverter<std::string>)); |
| 369 | } |
| 370 |
| 371 | void RegisterStringField(const std::string& field_name, |
| 372 | string16 StructType::* field) { |
| 373 | fields_.push_back( |
| 374 | std::make_unique<internal::FieldConverter<StructType, string16>>( |
| 375 | field_name, field, new internal::BasicValueConverter<string16>)); |
| 376 | } |
| 377 |
| 378 | void RegisterBoolField(const std::string& field_name, |
| 379 | bool StructType::* field) { |
| 380 | fields_.push_back( |
| 381 | std::make_unique<internal::FieldConverter<StructType, bool>>( |
| 382 | field_name, field, new internal::BasicValueConverter<bool>)); |
| 383 | } |
| 384 |
| 385 | void RegisterDoubleField(const std::string& field_name, |
| 386 | double StructType::* field) { |
| 387 | fields_.push_back( |
| 388 | std::make_unique<internal::FieldConverter<StructType, double>>( |
| 389 | field_name, field, new internal::BasicValueConverter<double>)); |
| 390 | } |
| 391 |
| 392 | template <class NestedType> |
| 393 | void RegisterNestedField( |
| 394 | const std::string& field_name, NestedType StructType::* field) { |
| 395 | fields_.push_back( |
| 396 | std::make_unique<internal::FieldConverter<StructType, NestedType>>( |
| 397 | field_name, field, new internal::NestedValueConverter<NestedType>)); |
| 398 | } |
| 399 |
| 400 | template <typename FieldType> |
| 401 | void RegisterCustomField(const std::string& field_name, |
| 402 | FieldType StructType::*field, |
| 403 | bool (*convert_func)(StringPiece, FieldType*)) { |
| 404 | fields_.push_back( |
| 405 | std::make_unique<internal::FieldConverter<StructType, FieldType>>( |
| 406 | field_name, field, |
| 407 | new internal::CustomFieldConverter<FieldType>(convert_func))); |
| 408 | } |
| 409 |
| 410 | template <typename FieldType> |
| 411 | void RegisterCustomValueField( |
| 412 | const std::string& field_name, |
| 413 | FieldType StructType::* field, |
| 414 | bool (*convert_func)(const base::Value*, FieldType*)) { |
| 415 | fields_.push_back( |
| 416 | std::make_unique<internal::FieldConverter<StructType, FieldType>>( |
| 417 | field_name, field, |
| 418 | new internal::ValueFieldConverter<FieldType>(convert_func))); |
| 419 | } |
| 420 |
| 421 | void RegisterRepeatedInt( |
| 422 | const std::string& field_name, |
| 423 | std::vector<std::unique_ptr<int>> StructType::*field) { |
| 424 | fields_.push_back(std::make_unique<internal::FieldConverter< |
| 425 | StructType, std::vector<std::unique_ptr<int>>>>( |
| 426 | field_name, field, new internal::RepeatedValueConverter<int>)); |
| 427 | } |
| 428 |
| 429 | void RegisterRepeatedString( |
| 430 | const std::string& field_name, |
| 431 | std::vector<std::unique_ptr<std::string>> StructType::*field) { |
| 432 | fields_.push_back( |
| 433 | std::make_unique<internal::FieldConverter< |
| 434 | StructType, std::vector<std::unique_ptr<std::string>>>>( |
| 435 | field_name, field, |
| 436 | new internal::RepeatedValueConverter<std::string>)); |
| 437 | } |
| 438 |
| 439 | void RegisterRepeatedString( |
| 440 | const std::string& field_name, |
| 441 | std::vector<std::unique_ptr<string16>> StructType::*field) { |
| 442 | fields_.push_back(std::make_unique<internal::FieldConverter< |
| 443 | StructType, std::vector<std::unique_ptr<string16>>>>( |
| 444 | field_name, field, new internal::RepeatedValueConverter<string16>)); |
| 445 | } |
| 446 |
| 447 | void RegisterRepeatedDouble( |
| 448 | const std::string& field_name, |
| 449 | std::vector<std::unique_ptr<double>> StructType::*field) { |
| 450 | fields_.push_back(std::make_unique<internal::FieldConverter< |
| 451 | StructType, std::vector<std::unique_ptr<double>>>>( |
| 452 | field_name, field, new internal::RepeatedValueConverter<double>)); |
| 453 | } |
| 454 |
| 455 | void RegisterRepeatedBool( |
| 456 | const std::string& field_name, |
| 457 | std::vector<std::unique_ptr<bool>> StructType::*field) { |
| 458 | fields_.push_back(std::make_unique<internal::FieldConverter< |
| 459 | StructType, std::vector<std::unique_ptr<bool>>>>( |
| 460 | field_name, field, new internal::RepeatedValueConverter<bool>)); |
| 461 | } |
| 462 |
| 463 | template <class NestedType> |
| 464 | void RegisterRepeatedCustomValue( |
| 465 | const std::string& field_name, |
| 466 | std::vector<std::unique_ptr<NestedType>> StructType::*field, |
| 467 | bool (*convert_func)(const base::Value*, NestedType*)) { |
| 468 | fields_.push_back( |
| 469 | std::make_unique<internal::FieldConverter< |
| 470 | StructType, std::vector<std::unique_ptr<NestedType>>>>( |
| 471 | field_name, field, |
| 472 | new internal::RepeatedCustomValueConverter<NestedType>( |
| 473 | convert_func))); |
| 474 | } |
| 475 |
| 476 | template <class NestedType> |
| 477 | void RegisterRepeatedMessage( |
| 478 | const std::string& field_name, |
| 479 | std::vector<std::unique_ptr<NestedType>> StructType::*field) { |
| 480 | fields_.push_back( |
| 481 | std::make_unique<internal::FieldConverter< |
| 482 | StructType, std::vector<std::unique_ptr<NestedType>>>>( |
| 483 | field_name, field, |
| 484 | new internal::RepeatedMessageConverter<NestedType>)); |
| 485 | } |
| 486 |
| 487 | bool Convert(const base::Value& value, StructType* output) const { |
| 488 | if (!value.is_dict()) |
| 489 | return false; |
| 490 |
| 491 | for (size_t i = 0; i < fields_.size(); ++i) { |
| 492 | const internal::FieldConverterBase<StructType>* field_converter = |
| 493 | fields_[i].get(); |
| 494 | const base::Value* field = value.FindPath(field_converter->field_path()); |
| 495 | if (field) { |
| 496 | if (!field_converter->ConvertField(*field, output)) { |
| 497 | DVLOG(1) << "failure at field " << field_converter->field_path(); |
| 498 | return false; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | return true; |
| 503 | } |
| 504 |
| 505 | private: |
| 506 | std::vector<std::unique_ptr<internal::FieldConverterBase<StructType>>> |
| 507 | fields_; |
| 508 |
| 509 | DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
| 510 | }; |
| 511 |
| 512 | } // namespace base |
| 513 |
| 514 | #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
| 515 |