Reorganized writer classes

This commit is contained in:
Benoit Blanchon
2019-10-31 19:27:23 +01:00
parent 6da6f921cd
commit 8721ac88b1
22 changed files with 235 additions and 201 deletions

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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('[');

View File

@ -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 {
@ -147,7 +147,7 @@ class TextFormatter {
}
protected:
TWriter &_writer;
TWriter _writer;
size_t _length;
private:

View File

@ -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) {
@ -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>
@ -163,7 +163,7 @@ class MsgPackSerializer {
writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
}
TWriter* _writer;
TWriter _writer;
size_t _bytesWritten;
};

View File

@ -1,91 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#include <ArduinoJson/Configuration.hpp>
#include <ArduinoJson/Polyfills/type_traits.hpp>
#include <ArduinoJson/Serialization/WriterSelector.hpp>
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
#include <WString.h>
#endif
#if ARDUINOJSON_ENABLE_STD_STRING
#include <string>
#endif
namespace ARDUINOJSON_NAMESPACE {
template <typename>
struct IsWriteableString : false_type {};
// A Print implementation that allows to write in a String
template <typename TString>
class DynamicStringWriter {};
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
template <>
struct IsWriteableString<String> : true_type {};
template <>
class DynamicStringWriter<String> {
public:
DynamicStringWriter(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;
};
#endif
#if ARDUINOJSON_ENABLE_STD_STRING
template <typename TCharTraits, typename TAllocator>
struct IsWriteableString<std::basic_string<char, TCharTraits, TAllocator> >
: true_type {};
template <typename TCharTraits, typename TAllocator>
class DynamicStringWriter<std::basic_string<char, TCharTraits, TAllocator> > {
typedef std::basic_string<char, TCharTraits, TAllocator> string_type;
public:
DynamicStringWriter(string_type &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) {
_str->append(reinterpret_cast<const char *>(s), n);
return n;
}
private:
string_type *_str;
};
#endif
template <typename TDestination>
struct WriterSelector<
TDestination,
typename enable_if<IsWriteableString<TDestination>::value>::type> {
typedef DynamicStringWriter<TDestination> writer_type;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -1,37 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#include <ArduinoJson/Polyfills/type_traits.hpp>
#include <ArduinoJson/Serialization/WriterSelector.hpp>
namespace ARDUINOJSON_NAMESPACE {
class PrintWriter {
public:
explicit PrintWriter(Print& print) : _print(print) {}
size_t write(uint8_t c) {
return _print.write(c);
}
size_t write(const uint8_t* s, size_t n) {
return _print.write(s, n);
}
private:
// cannot be assigned
PrintWriter& operator=(const PrintWriter&);
Print& _print;
};
template <typename TDestination>
struct WriterSelector<
TDestination,
typename enable_if<is_base_of<Print, TDestination>::value>::type> {
typedef PrintWriter writer_type;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -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

View File

@ -1,17 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#include <ArduinoJson/Namespace.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <typename TDestination, typename Enable = void>
struct WriterSelector {
// by default, assume destination implements the Writer concept
typedef TDestination& writer_type;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -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

View File

@ -0,0 +1,28 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
namespace ARDUINOJSON_NAMESPACE {
template <typename TDestination>
class Writer<
TDestination,
typename enable_if<is_base_of< ::Print, TDestination>::value>::type> {
public:
explicit Writer(::Print& print) : _print(&print) {}
size_t write(uint8_t c) {
return _print->write(c);
}
size_t write(const uint8_t* s, size_t n) {
return _print->write(s, n);
}
private:
::Print* _print;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -4,39 +4,29 @@
#pragma once
#include <ArduinoJson/Configuration.hpp>
#include <ArduinoJson/Serialization/WriterSelector.hpp>
#include <ostream>
namespace ARDUINOJSON_NAMESPACE {
class StreamWriter {
template <typename TDestination>
class Writer<
TDestination,
typename enable_if<is_base_of<std::ostream, TDestination>::value>::type> {
public:
explicit StreamWriter(std::ostream& os) : _os(os) {}
explicit Writer(std::ostream& os) : _os(&os) {}
size_t write(uint8_t c) {
_os << c;
_os->put(static_cast<char>(c));
return 1;
}
size_t write(const uint8_t* s, size_t n) {
_os.write(reinterpret_cast<const char*>(s),
static_cast<std::streamsize>(n));
_os->write(reinterpret_cast<const char*>(s),
static_cast<std::streamsize>(n));
return n;
}
private:
// cannot be assigned
StreamWriter& operator=(const StreamWriter&);
std::ostream& _os;
};
template <typename TDestination>
struct WriterSelector<
TDestination,
typename enable_if<is_base_of<std::ostream, TDestination>::value>::type> {
typedef StreamWriter writer_type;
std::ostream* _os;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -0,0 +1,40 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Polyfills/type_traits.hpp>
#include <string>
namespace ARDUINOJSON_NAMESPACE {
template <class T>
struct is_std_string : false_type {};
template <class TCharTraits, class TAllocator>
struct is_std_string<std::basic_string<char, TCharTraits, TAllocator> >
: true_type {};
template <typename TDestination>
class Writer<TDestination,
typename enable_if<is_std_string<TDestination>::value>::type> {
public:
Writer(TDestination &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) {
_str->append(reinterpret_cast<const char *>(s), n);
return n;
}
private:
TDestination *_str;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -4,7 +4,7 @@
#pragma once
#include <ArduinoJson/Serialization/DummyWriter.hpp>
#include <ArduinoJson/Serialization/Writers/DummyWriter.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -4,23 +4,13 @@
#pragma once
#include <ArduinoJson/Configuration.hpp>
#include <ArduinoJson/Serialization/DynamicStringWriter.hpp>
#include <ArduinoJson/Serialization/StaticStringWriter.hpp>
#if ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson/Serialization/StreamWriter.hpp>
#endif
#if ARDUINOJSON_ENABLE_ARDUINO_PRINT
#include <ArduinoJson/Serialization/PrintWriter.hpp>
#endif
#include <ArduinoJson/Serialization/Writer.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <template <typename> class TSerializer, typename TSource,
typename TWriter>
size_t doSerialize(const TSource &source, TWriter &writer) {
size_t doSerialize(const TSource &source, TWriter writer) {
TSerializer<TWriter> serializer(writer);
source.accept(serializer);
return serializer.bytesWritten();
@ -29,7 +19,7 @@ size_t doSerialize(const TSource &source, TWriter &writer) {
template <template <typename> class TSerializer, typename TSource,
typename TDestination>
size_t serialize(const TSource &source, TDestination &destination) {
typename WriterSelector<TDestination>::writer_type writer(destination);
Writer<TDestination> writer(destination);
return doSerialize<TSerializer>(source, writer);
}

View File

@ -0,0 +1,37 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#include <ArduinoJson/Configuration.hpp>
#include <ArduinoJson/Polyfills/type_traits.hpp>
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
#include <WString.h>
#endif
#if ARDUINOJSON_ENABLE_STD_STRING
#include <string>
#endif
namespace ARDUINOJSON_NAMESPACE {
template <typename>
struct IsWriteableString : false_type {};
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
template <>
struct IsWriteableString< ::String> : true_type {};
#endif
#if ARDUINOJSON_ENABLE_STD_STRING
template <typename TCharTraits, typename TAllocator>
struct IsWriteableString<std::basic_string<char, TCharTraits, TAllocator> >
: true_type {};
#endif
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -4,7 +4,7 @@
#pragma once
#include <ArduinoJson/Serialization/DynamicStringWriter.hpp>
#include <ArduinoJson/Strings/IsWriteableString.hpp>
namespace ARDUINOJSON_NAMESPACE {

View File

@ -4,7 +4,7 @@
#pragma once
#include <ArduinoJson/Serialization/DynamicStringWriter.hpp>
#include <ArduinoJson/Strings/IsWriteableString.hpp>
#include <ArduinoJson/Variant/VariantFunctions.hpp>
#include <ArduinoJson/Variant/VariantRef.hpp>