JsonObject.cpp 2.7 KB

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