Skip to content

Commit

Permalink
Reorganized writer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Oct 31, 2019
1 parent 6da6f92 commit 8721ac8
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 201 deletions.
23 changes: 17 additions & 6 deletions extras/tests/JsonSerializer/CustomWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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());
}
}
8 changes: 4 additions & 4 deletions extras/tests/Misc/StringWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions extras/tests/TextFormatter/writeFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion extras/tests/TextFormatter/writeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/Json/JsonSerializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TWriter>
class JsonSerializer {
public:
JsonSerializer(TWriter &writer) : _formatter(writer) {}
JsonSerializer(TWriter writer) : _formatter(writer) {}

FORCE_INLINE void visitArray(const CollectionData &array) {
write('[');
Expand Down
4 changes: 2 additions & 2 deletions src/ArduinoJson/Json/TextFormatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TWriter>
class TextFormatter {
public:
explicit TextFormatter(TWriter &writer) : _writer(writer), _length(0) {}
explicit TextFormatter(TWriter writer) : _writer(writer), _length(0) {}

// Returns the number of bytes sent to the TWriter implementation.
size_t bytesWritten() const {
Expand Down Expand Up @@ -147,7 +147,7 @@ class TextFormatter {
}

protected:
TWriter &_writer;
TWriter _writer;
size_t _length;

private:
Expand Down
8 changes: 4 additions & 4 deletions src/ArduinoJson/MsgPack/MsgPackSerializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TWriter>
class MsgPackSerializer {
public:
MsgPackSerializer(TWriter& writer) : _writer(&writer), _bytesWritten(0) {}
MsgPackSerializer(TWriter writer) : _writer(writer), _bytesWritten(0) {}

template <typename T>
typename enable_if<sizeof(T) == 4>::type visitFloat(T value32) {
Expand Down Expand Up @@ -150,11 +150,11 @@ class MsgPackSerializer {

private:
void writeByte(uint8_t c) {
_bytesWritten += _writer->write(c);
_bytesWritten += _writer.write(c);
}

void writeBytes(const uint8_t* p, size_t n) {
_bytesWritten += _writer->write(p, n);
_bytesWritten += _writer.write(p, n);
}

template <typename T>
Expand All @@ -163,7 +163,7 @@ class MsgPackSerializer {
writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
}

TWriter* _writer;
TWriter _writer;
size_t _bytesWritten;
};

Expand Down
91 changes: 0 additions & 91 deletions src/ArduinoJson/Serialization/DynamicStringWriter.hpp

This file was deleted.

37 changes: 0 additions & 37 deletions src/ArduinoJson/Serialization/PrintWriter.hpp

This file was deleted.

47 changes: 47 additions & 0 deletions src/ArduinoJson/Serialization/Writer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License

#pragma once

#include <ArduinoJson/Namespace.hpp>

namespace ARDUINOJSON_NAMESPACE {

// The default writer is a simple wrapper for Writers that are not copiable
template <typename TDestination, typename Enable = void>
class Writer {
public:
explicit Writer(TDestination& dest) : _dest(&dest) {}

size_t write(uint8_t c) {
return _dest->write(c);
}

size_t write(const uint8_t* s, size_t n) {
return _dest->write(s, n);
}

private:
TDestination* _dest;
};

} // namespace ARDUINOJSON_NAMESPACE

#include <ArduinoJson/Serialization/Writers/StaticStringWriter.hpp>

#if ARDUINOJSON_ENABLE_STD_STRING
#include <ArduinoJson/Serialization/Writers/StdStringWriter.hpp>
#endif

#if ARDUINOJSON_ENABLE_ARDUINO_STRING
#include <ArduinoJson/Serialization/Writers/ArduinoStringWriter.hpp>
#endif

#if ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson/Serialization/Writers/StdStreamWriter.hpp>
#endif

#if ARDUINOJSON_ENABLE_ARDUINO_PRINT
#include <ArduinoJson/Serialization/Writers/PrintWriter.hpp>
#endif
17 changes: 0 additions & 17 deletions src/ArduinoJson/Serialization/WriterSelector.hpp

This file was deleted.

36 changes: 36 additions & 0 deletions src/ArduinoJson/Serialization/Writers/ArduinoStringWriter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License

#pragma once

#include <WString.h>

namespace ARDUINOJSON_NAMESPACE {

template <>
class Writer< ::String, void> {
public:
explicit Writer(::String &str) : _str(&str) {}

size_t write(uint8_t c) {
_str->operator+=(static_cast<char>(c));
return 1;
}

size_t write(const uint8_t *s, size_t n) {
// CAUTION: Arduino String doesn't have append()
// and old version doesn't have size() either
_str->reserve(_str->length() + n);
while (n > 0) {
_str->operator+=(static_cast<char>(*s++));
n--;
}
return n;
}

private:
::String *_str;
};

} // namespace ARDUINOJSON_NAMESPACE
Loading

0 comments on commit 8721ac8

Please sign in to comment.