Files
ArduinoJson/extras/tests/JsonSerializer/CustomWriter.cpp

53 lines
1020 B
C++
Raw Normal View History

// ArduinoJson - arduinojson.org
2020-01-09 15:48:38 +01:00
// Copyright Benoit Blanchon 2014-2020
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
2019-10-31 19:27:23 +01:00
class CustomWriter {
public:
CustomWriter() {}
size_t write(uint8_t c) {
2019-10-31 19:27:23 +01:00
_str.append(1, static_cast<char>(c));
return 1;
}
size_t write(const uint8_t *s, size_t n) {
2019-10-31 19:27:23 +01:00
_str.append(reinterpret_cast<const char *>(s), n);
return n;
}
2019-10-31 19:27:23 +01:00
const std::string &str() const {
return _str;
}
private:
CustomWriter(const CustomWriter &); // non-copiable
CustomWriter &operator=(const CustomWriter &);
std::string _str;
};
TEST_CASE("CustomWriter") {
DynamicJsonDocument doc(4096);
JsonArray array = doc.to<JsonArray>();
array.add(4);
array.add(2);
SECTION("serializeJson()") {
CustomWriter writer;
serializeJson(array, writer);
2019-10-31 19:27:23 +01:00
REQUIRE("[4,2]" == writer.str());
}
SECTION("serializeJsonPretty") {
CustomWriter writer;
serializeJsonPretty(array, writer);
2019-10-31 19:27:23 +01:00
REQUIRE("[\r\n 4,\r\n 2\r\n]" == writer.str());
}
}