forked from bblanchon/ArduinoJson
Reorganized writer classes
This commit is contained in:
@ -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('[');
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
|
@ -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
|
@ -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
|
47
src/ArduinoJson/Serialization/Writer.hpp
Normal file
47
src/ArduinoJson/Serialization/Writer.hpp
Normal 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
|
@ -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
|
@ -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
|
28
src/ArduinoJson/Serialization/Writers/PrintWriter.hpp
Normal file
28
src/ArduinoJson/Serialization/Writers/PrintWriter.hpp
Normal 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
|
@ -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
|
40
src/ArduinoJson/Serialization/Writers/StdStringWriter.hpp
Normal file
40
src/ArduinoJson/Serialization/Writers/StdStringWriter.hpp
Normal 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
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Serialization/DummyWriter.hpp>
|
||||
#include <ArduinoJson/Serialization/Writers/DummyWriter.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
37
src/ArduinoJson/Strings/IsWriteableString.hpp
Normal file
37
src/ArduinoJson/Strings/IsWriteableString.hpp
Normal 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
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Serialization/DynamicStringWriter.hpp>
|
||||
#include <ArduinoJson/Strings/IsWriteableString.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
|
@ -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>
|
||||
|
||||
|
Reference in New Issue
Block a user