StaticJsonDocument.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("StaticJsonDocument") {
  7. SECTION("serializeJson()") {
  8. StaticJsonDocument<200> doc;
  9. JsonObject obj = doc.to<JsonObject>();
  10. obj["hello"] = "world";
  11. std::string json;
  12. serializeJson(doc, json);
  13. REQUIRE(json == "{\"hello\":\"world\"}");
  14. }
  15. SECTION("Copy assignment") {
  16. StaticJsonDocument<200> doc1, doc2;
  17. doc1.to<JsonVariant>().set(666);
  18. deserializeJson(doc2, "{\"hello\":\"world\"}");
  19. doc2.nestingLimit = 42;
  20. doc1 = doc2;
  21. std::string json;
  22. serializeJson(doc1, json);
  23. REQUIRE(json == "{\"hello\":\"world\"}");
  24. REQUIRE(doc1.nestingLimit == 42);
  25. }
  26. SECTION("Copy constructor") {
  27. StaticJsonDocument<200> doc1;
  28. deserializeJson(doc1, "{\"hello\":\"world\"}");
  29. doc1.nestingLimit = 42;
  30. StaticJsonDocument<200> doc2 = doc1;
  31. std::string json;
  32. serializeJson(doc2, json);
  33. REQUIRE(json == "{\"hello\":\"world\"}");
  34. REQUIRE(doc2.nestingLimit == 42);
  35. }
  36. SECTION("Assign from StaticJsonDocument of different capacity") {
  37. StaticJsonDocument<200> doc1;
  38. StaticJsonDocument<300> doc2;
  39. doc1.to<JsonVariant>().set(666);
  40. deserializeJson(doc2, "{\"hello\":\"world\"}");
  41. doc2.nestingLimit = 42;
  42. doc1 = doc2;
  43. std::string json;
  44. serializeJson(doc1, json);
  45. REQUIRE(json == "{\"hello\":\"world\"}");
  46. REQUIRE(doc1.nestingLimit == 42);
  47. }
  48. SECTION("Assign from DynamicJsonDocument") {
  49. StaticJsonDocument<200> doc1;
  50. DynamicJsonDocument doc2;
  51. doc1.to<JsonVariant>().set(666);
  52. deserializeJson(doc2, "{\"hello\":\"world\"}");
  53. doc2.nestingLimit = 42;
  54. doc1 = doc2;
  55. std::string json;
  56. serializeJson(doc1, json);
  57. REQUIRE(json == "{\"hello\":\"world\"}");
  58. REQUIRE(doc1.nestingLimit == 42);
  59. }
  60. SECTION("Construct from StaticJsonDocument of different size") {
  61. StaticJsonDocument<300> doc2;
  62. deserializeJson(doc2, "{\"hello\":\"world\"}");
  63. doc2.nestingLimit = 42;
  64. StaticJsonDocument<200> doc1 = doc2;
  65. std::string json;
  66. serializeJson(doc1, json);
  67. REQUIRE(json == "{\"hello\":\"world\"}");
  68. REQUIRE(doc1.nestingLimit == 42);
  69. }
  70. SECTION("Construct from DynamicJsonDocument") {
  71. DynamicJsonDocument doc2;
  72. deserializeJson(doc2, "{\"hello\":\"world\"}");
  73. doc2.nestingLimit = 42;
  74. StaticJsonDocument<200> doc1 = doc2;
  75. std::string json;
  76. serializeJson(doc1, json);
  77. REQUIRE(json == "{\"hello\":\"world\"}");
  78. REQUIRE(doc1.nestingLimit == 42);
  79. }
  80. }