converters.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. namespace {
  8. struct Date {
  9. int day;
  10. int month;
  11. int year;
  12. };
  13. bool convertToJson(const Date& src, JsonVariant dst) {
  14. dst["day"] = src.day;
  15. dst["month"] = src.month;
  16. dst["year"] = src.year;
  17. return true;
  18. }
  19. void convertFromJson(JsonVariantConst src, Date& dst) {
  20. dst.day = src["day"];
  21. dst.month = src["month"];
  22. dst.year = src["year"];
  23. }
  24. bool canConvertFromJson(JsonVariantConst src, const Date&) {
  25. return src["day"].is<int>() && src["month"].is<int>() &&
  26. src["year"].is<int>();
  27. }
  28. } // namespace
  29. TEST_CASE("Custom converter with overloading") {
  30. DynamicJsonDocument doc(4096);
  31. SECTION("convert JSON to Date") {
  32. doc["date"]["day"] = 2;
  33. doc["date"]["month"] = 3;
  34. doc["date"]["year"] = 2021;
  35. Date date = doc["date"];
  36. REQUIRE(date.day == 2);
  37. REQUIRE(date.month == 3);
  38. REQUIRE(date.year == 2021);
  39. }
  40. SECTION("is<Date>() returns true") {
  41. doc["date"]["day"] = 2;
  42. doc["date"]["month"] = 3;
  43. doc["date"]["year"] = 2021;
  44. REQUIRE(doc["date"].is<Date>());
  45. }
  46. SECTION("is<Date>() returns false") {
  47. doc["date"]["day"] = 2;
  48. doc["date"]["month"] = 3;
  49. doc["date"]["year"] = "2021";
  50. REQUIRE(doc["date"].is<Date>() == false);
  51. }
  52. SECTION("convert Date to JSON") {
  53. Date date = {19, 3, 2021};
  54. doc["date"] = date;
  55. REQUIRE(doc["date"]["day"] == 19);
  56. REQUIRE(doc["date"]["month"] == 3);
  57. REQUIRE(doc["date"]["year"] == 2021);
  58. }
  59. }
  60. class Complex {
  61. public:
  62. explicit Complex(double r, double i) : _real(r), _imag(i) {}
  63. double real() const {
  64. return _real;
  65. }
  66. double imag() const {
  67. return _imag;
  68. }
  69. private:
  70. double _real, _imag;
  71. };
  72. namespace ARDUINOJSON_NAMESPACE {
  73. template <>
  74. struct Converter<Complex> {
  75. static bool toJson(const Complex& src, VariantRef dst) {
  76. dst["real"] = src.real();
  77. dst["imag"] = src.imag();
  78. return true;
  79. }
  80. static Complex fromJson(VariantConstRef src) {
  81. return Complex(src["real"], src["imag"]);
  82. }
  83. static bool checkJson(VariantConstRef src) {
  84. return src["real"].is<double>() && src["imag"].is<double>();
  85. }
  86. };
  87. } // namespace ARDUINOJSON_NAMESPACE
  88. TEST_CASE("Custom converter with specialization") {
  89. DynamicJsonDocument doc(4096);
  90. SECTION("convert JSON to Complex") {
  91. doc["value"]["real"] = 2;
  92. doc["value"]["imag"] = 3;
  93. Complex value = doc["value"];
  94. REQUIRE(value.real() == 2);
  95. REQUIRE(value.imag() == 3);
  96. }
  97. SECTION("is<Complex>() returns true") {
  98. doc["value"]["real"] = 2;
  99. doc["value"]["imag"] = 3;
  100. REQUIRE(doc["value"].is<Complex>());
  101. }
  102. SECTION("is<Complex>() returns false") {
  103. doc["value"]["real"] = 2;
  104. doc["value"]["imag"] = "3";
  105. REQUIRE(doc["value"].is<Complex>() == false);
  106. }
  107. SECTION("convert value to JSON") {
  108. doc["value"] = Complex(19, 3);
  109. REQUIRE(doc["value"]["real"] == 19);
  110. REQUIRE(doc["value"]["imag"] == 3);
  111. }
  112. }