JsonObjectPretty.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 json[256];
  9. size_t actualLen = serializeJsonPretty(obj, json);
  10. size_t measuredLen = measureJsonPretty(obj);
  11. REQUIRE(json == expected);
  12. REQUIRE(expected.size() == actualLen);
  13. REQUIRE(expected.size() == measuredLen);
  14. }
  15. TEST_CASE("serializeJsonPretty(JsonObject)") {
  16. DynamicJsonDocument doc(4096);
  17. JsonObject obj = doc.to<JsonObject>();
  18. SECTION("EmptyObject") {
  19. check(obj, "{}");
  20. }
  21. SECTION("OneMember") {
  22. obj["key"] = "value";
  23. check(obj,
  24. "{\r\n"
  25. " \"key\": \"value\"\r\n"
  26. "}");
  27. }
  28. SECTION("TwoMembers") {
  29. obj["key1"] = "value1";
  30. obj["key2"] = "value2";
  31. check(obj,
  32. "{\r\n"
  33. " \"key1\": \"value1\",\r\n"
  34. " \"key2\": \"value2\"\r\n"
  35. "}");
  36. }
  37. SECTION("EmptyNestedContainers") {
  38. obj.createNestedObject("key1");
  39. obj.createNestedArray("key2");
  40. check(obj,
  41. "{\r\n"
  42. " \"key1\": {},\r\n"
  43. " \"key2\": []\r\n"
  44. "}");
  45. }
  46. SECTION("NestedContainers") {
  47. JsonObject nested1 = obj.createNestedObject("key1");
  48. nested1["a"] = 1;
  49. JsonArray nested2 = obj.createNestedArray("key2");
  50. nested2.add(2);
  51. check(obj,
  52. "{\r\n"
  53. " \"key1\": {\r\n"
  54. " \"a\": 1\r\n"
  55. " },\r\n"
  56. " \"key2\": [\r\n"
  57. " 2\r\n"
  58. " ]\r\n"
  59. "}");
  60. }
  61. }