JsonObject.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <string>
  7. void check(const JsonObject obj, const std::string &expected) {
  8. char actual[256];
  9. size_t actualLen = serializeJson(obj, actual);
  10. size_t measuredLen = measureJson(obj);
  11. REQUIRE(expected == actual);
  12. REQUIRE(expected.size() == actualLen);
  13. REQUIRE(expected.size() == measuredLen);
  14. }
  15. TEST_CASE("serializeJson(JsonObject)") {
  16. DynamicJsonDocument doc(4096);
  17. JsonObject obj = doc.to<JsonObject>();
  18. SECTION("EmptyObject") {
  19. check(obj, "{}");
  20. }
  21. SECTION("TwoStrings") {
  22. obj["key1"] = "value1";
  23. obj["key2"] = "value2";
  24. check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
  25. }
  26. SECTION("RemoveFirst") {
  27. obj["key1"] = "value1";
  28. obj["key2"] = "value2";
  29. obj.remove("key1");
  30. check(obj, "{\"key2\":\"value2\"}");
  31. }
  32. SECTION("RemoveLast") {
  33. obj["key1"] = "value1";
  34. obj["key2"] = "value2";
  35. obj.remove("key2");
  36. check(obj, "{\"key1\":\"value1\"}");
  37. }
  38. SECTION("RemoveUnexistingKey") {
  39. obj["key1"] = "value1";
  40. obj["key2"] = "value2";
  41. obj.remove("key3");
  42. check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
  43. }
  44. SECTION("ReplaceExistingKey") {
  45. obj["key"] = "value1";
  46. obj["key"] = "value2";
  47. check(obj, "{\"key\":\"value2\"}");
  48. }
  49. SECTION("TwoIntegers") {
  50. obj["a"] = 1;
  51. obj["b"] = 2;
  52. check(obj, "{\"a\":1,\"b\":2}");
  53. }
  54. SECTION("serialized(const char*)") {
  55. obj["a"] = serialized("[1,2]");
  56. obj["b"] = serialized("[4,5]");
  57. check(obj, "{\"a\":[1,2],\"b\":[4,5]}");
  58. }
  59. SECTION("Two doubles") {
  60. obj["a"] = 12.34;
  61. obj["b"] = 56.78;
  62. check(obj, "{\"a\":12.34,\"b\":56.78}");
  63. }
  64. SECTION("TwoNull") {
  65. obj["a"] = static_cast<char *>(0);
  66. obj["b"] = static_cast<char *>(0);
  67. check(obj, "{\"a\":null,\"b\":null}");
  68. }
  69. SECTION("TwoBooleans") {
  70. obj["a"] = true;
  71. obj["b"] = false;
  72. check(obj, "{\"a\":true,\"b\":false}");
  73. }
  74. SECTION("ThreeNestedArrays") {
  75. DynamicJsonDocument b(4096);
  76. DynamicJsonDocument c(4096);
  77. obj.createNestedArray("a");
  78. obj["b"] = b.to<JsonArray>();
  79. obj["c"] = c.to<JsonArray>();
  80. check(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
  81. }
  82. SECTION("ThreeNestedObjects") {
  83. DynamicJsonDocument b(4096);
  84. DynamicJsonDocument c(4096);
  85. obj.createNestedObject("a");
  86. obj["b"] = b.to<JsonObject>();
  87. obj["c"] = c.to<JsonObject>();
  88. check(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
  89. }
  90. }