std_string.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("serialize JsonArray to std::string") {
  7. JsonDocument doc;
  8. JsonArray array = doc.to<JsonArray>();
  9. array.add(4);
  10. array.add(2);
  11. SECTION("serializeJson()") {
  12. std::string json = "erase me";
  13. serializeJson(array, json);
  14. REQUIRE("[4,2]" == json);
  15. }
  16. SECTION("serializeJsonPretty") {
  17. std::string json = "erase me";
  18. serializeJsonPretty(array, json);
  19. REQUIRE("[\r\n 4,\r\n 2\r\n]" == json);
  20. }
  21. }
  22. TEST_CASE("serialize JsonObject to std::string") {
  23. JsonDocument doc;
  24. JsonObject obj = doc.to<JsonObject>();
  25. obj["key"] = "value";
  26. SECTION("object") {
  27. std::string json = "erase me";
  28. serializeJson(doc, json);
  29. REQUIRE("{\"key\":\"value\"}" == json);
  30. }
  31. SECTION("serializeJsonPretty") {
  32. std::string json = "erase me";
  33. serializeJsonPretty(doc, json);
  34. REQUIRE("{\r\n \"key\": \"value\"\r\n}" == json);
  35. }
  36. }
  37. TEST_CASE("serialize an std::string containing a NUL") {
  38. JsonDocument doc;
  39. doc.set(std::string("hello\0world", 11));
  40. std::string json = "erase me";
  41. serializeJson(doc, json);
  42. CHECK("\"hello\\u0000world\"" == json);
  43. }