Files
ArduinoJson/extras/tests/JsonSerializer/std_string.cpp
2023-04-07 14:50:54 +02:00

60 lines
1.3 KiB
C++

// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using ArduinoJson::detail::sizeofString;
TEST_CASE("serialize JsonArray to std::string") {
JsonDocument doc(4096);
JsonArray array = doc.to<JsonArray>();
array.add(4);
array.add(2);
SECTION("serializeJson()") {
std::string json;
serializeJson(array, json);
REQUIRE("[4,2]" == json);
}
SECTION("serializeJsonPretty") {
std::string json;
serializeJsonPretty(array, json);
REQUIRE("[\r\n 4,\r\n 2\r\n]" == json);
}
}
TEST_CASE("serialize JsonObject to std::string") {
JsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
obj["key"] = "value";
SECTION("object") {
std::string json;
serializeJson(doc, json);
REQUIRE("{\"key\":\"value\"}" == json);
}
SECTION("serializeJsonPretty") {
std::string json;
serializeJsonPretty(doc, json);
REQUIRE("{\r\n \"key\": \"value\"\r\n}" == json);
}
}
TEST_CASE("serialize an std::string containing a NUL") {
JsonDocument doc(256);
doc.set(std::string("hello\0world", 11));
CHECK(doc.memoryUsage() == sizeofString(11));
std::string json;
serializeJson(doc, json);
CHECK("\"hello\\u0000world\"" == json);
}