converters.cpp 3.5 KB

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