mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Reorganized writer classes
This commit is contained in:
@ -5,18 +5,29 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
struct CustomWriter {
|
||||
std::string str;
|
||||
class CustomWriter {
|
||||
public:
|
||||
CustomWriter() {}
|
||||
|
||||
size_t write(uint8_t c) {
|
||||
str.append(1, static_cast<char>(c));
|
||||
_str.append(1, static_cast<char>(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t write(const uint8_t *s, size_t n) {
|
||||
str.append(reinterpret_cast<const char *>(s), n);
|
||||
_str.append(reinterpret_cast<const char *>(s), n);
|
||||
return n;
|
||||
}
|
||||
|
||||
const std::string &str() const {
|
||||
return _str;
|
||||
}
|
||||
|
||||
private:
|
||||
CustomWriter(const CustomWriter &); // non-copiable
|
||||
CustomWriter &operator=(const CustomWriter &);
|
||||
|
||||
std::string _str;
|
||||
};
|
||||
|
||||
TEST_CASE("CustomWriter") {
|
||||
@ -29,13 +40,13 @@ TEST_CASE("CustomWriter") {
|
||||
CustomWriter writer;
|
||||
serializeJson(array, writer);
|
||||
|
||||
REQUIRE("[4,2]" == writer.str);
|
||||
REQUIRE("[4,2]" == writer.str());
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
CustomWriter writer;
|
||||
serializeJsonPretty(array, writer);
|
||||
|
||||
REQUIRE("[\r\n 4,\r\n 2\r\n]" == writer.str);
|
||||
REQUIRE("[\r\n 4,\r\n 2\r\n]" == writer.str());
|
||||
}
|
||||
}
|
||||
|
@ -49,15 +49,15 @@ TEST_CASE("StaticStringWriter") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicStringWriter<std::string>") {
|
||||
TEST_CASE("Writer<std::string>") {
|
||||
std::string output;
|
||||
DynamicStringWriter<std::string> sb(output);
|
||||
Writer<std::string> sb(output);
|
||||
common_tests(sb, output);
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicStringWriter<custom_string>") {
|
||||
TEST_CASE("Writer<custom_string>") {
|
||||
custom_string output;
|
||||
DynamicStringWriter<custom_string> sb(output);
|
||||
Writer<custom_string> sb(output);
|
||||
|
||||
REQUIRE(4 == print(sb, "ABCD"));
|
||||
REQUIRE("ABCD" == output);
|
||||
|
@ -9,15 +9,15 @@
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
#include <ArduinoJson/Json/TextFormatter.hpp>
|
||||
#include <ArduinoJson/Serialization/DynamicStringWriter.hpp>
|
||||
#include <ArduinoJson/Serialization/Writer.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
template <typename TFloat>
|
||||
void check(TFloat input, const std::string& expected) {
|
||||
std::string output;
|
||||
DynamicStringWriter<std::string> sb(output);
|
||||
TextFormatter<DynamicStringWriter<std::string> > writer(sb);
|
||||
Writer<std::string> sb(output);
|
||||
TextFormatter<Writer<std::string> > writer(sb);
|
||||
writer.writeFloat(input);
|
||||
REQUIRE(writer.bytesWritten() == output.size());
|
||||
CHECK(expected == output);
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <ArduinoJson/Json/TextFormatter.hpp>
|
||||
#include <ArduinoJson/Serialization/StaticStringWriter.hpp>
|
||||
#include <ArduinoJson/Serialization/Writers/StaticStringWriter.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
|
Reference in New Issue
Block a user