Added serializeMsgPack() and measureMsgPack() (closes #358)

This commit is contained in:
Benoit Blanchon
2018-05-29 08:31:17 +02:00
parent 58cb793c96
commit fc2e3a4ab3
59 changed files with 975 additions and 391 deletions

View File

@ -5,11 +5,13 @@
#pragma once
#include "ArduinoJson/DynamicJsonDocument.hpp"
#include "ArduinoJson/StaticJsonDocument.hpp"
#include "ArduinoJson/deserializeJson.hpp"
#include "ArduinoJson/deserializeMsgPack.hpp"
#include "ArduinoJson/Json/JsonDeserializer.hpp"
#include "ArduinoJson/Json/JsonSerializer.hpp"
#include "ArduinoJson/Json/PrettyJsonSerializer.hpp"
#include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp"
#include "ArduinoJson/MsgPack/MsgPackSerializer.hpp"
#include "ArduinoJson/StaticJsonDocument.hpp"
#include "ArduinoJson/JsonArrayImpl.hpp"
#include "ArduinoJson/JsonObjectImpl.hpp"
#include "ArduinoJson/JsonVariantImpl.hpp"

View File

@ -0,0 +1,82 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../StringStorage/StringStorage.hpp"
#include "./ArduinoStreamReader.hpp"
#include "./CharPointerReader.hpp"
#include "./DeserializationError.hpp"
#include "./FlashStringReader.hpp"
#include "./IteratorReader.hpp"
#include "./StdStreamReader.hpp"
namespace ArduinoJson {
namespace Internals {
template <template <typename, typename> class TDeserializer,
typename TJsonBuffer, typename TReader, typename TWriter>
TDeserializer<TReader, TWriter> makeDeserializer(TJsonBuffer *buffer,
TReader reader, TWriter writer,
uint8_t nestingLimit) {
return TDeserializer<TReader, TWriter>(buffer, reader, writer, nestingLimit);
}
// DeserializationError deserialize(TDocument& doc, TString input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TString = const std::string&, const String&
template <template <typename, typename> class TDeserializer, typename TDocument,
typename TString>
typename Internals::enable_if<!Internals::is_array<TString>::value,
DeserializationError>::type
deserialize(TDocument &doc, const TString &input) {
using namespace Internals;
return makeDeserializer<TDeserializer>(&doc.buffer(), makeReader(input),
makeStringStorage(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserialize(TDocument& doc, TChar* input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TChar* = char*, const char*, const FlashStringHelper*
template <template <typename, typename> class TDeserializer, typename TDocument,
typename TChar>
DeserializationError deserialize(TDocument &doc, TChar *input) {
using namespace Internals;
return makeDeserializer<TDeserializer>(&doc.buffer(), makeReader(input),
makeStringStorage(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserialize(TDocument& doc, TChar* input, size_t
// inputSize);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TChar* = char*, const char*, const FlashStringHelper*
template <template <typename, typename> class TDeserializer, typename TDocument,
typename TChar>
DeserializationError deserialize(TDocument &doc, TChar *input,
size_t inputSize) {
using namespace Internals;
return makeDeserializer<TDeserializer>(
&doc.buffer(), makeReader(input, inputSize),
makeStringStorage(doc.buffer(), input), doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserialize(TDocument& doc, TStream input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TStream = std::istream&, Stream&
template <template <typename, typename> class TDeserializer, typename TDocument,
typename TStream>
DeserializationError deserialize(TDocument &doc, TStream &input) {
using namespace Internals;
return makeDeserializer<TDeserializer>(&doc.buffer(), makeReader(input),
makeStringStorage(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -79,7 +79,7 @@ class DynamicJsonDocument {
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor& visitor) const {
return _root.visit(visitor);
}
};

View File

@ -4,24 +4,23 @@
#pragma once
#include "../DeserializationError.hpp"
#include "../Deserialization/deserialize.hpp"
#include "../JsonVariant.hpp"
#include "../Memory/JsonBuffer.hpp"
#include "../Polyfills/type_traits.hpp"
#include "../Reading/Reader.hpp"
#include "./EscapeSequence.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TReader, typename TWriter>
template <typename TReader, typename TStringStorage>
class JsonDeserializer {
public:
JsonDeserializer(JsonBuffer *buffer, TReader reader, TWriter writer,
uint8_t nestingLimit)
JsonDeserializer(JsonBuffer *buffer, TReader reader,
TStringStorage stringStorage, uint8_t nestingLimit)
: _buffer(buffer),
_reader(reader),
_writer(writer),
_stringStorage(stringStorage),
_nestingLimit(nestingLimit),
_loaded(false) {}
DeserializationError parse(JsonVariant &variant) {
@ -168,8 +167,8 @@ class JsonDeserializer {
}
DeserializationError parseString(const char **result) {
typename remove_reference<TWriter>::type::String str =
_writer.startString();
typename remove_reference<TStringStorage>::type::String str =
_stringStorage.startString();
char c = current();
if (c == '\0') return DeserializationError::IncompleteInput;
@ -285,19 +284,35 @@ class JsonDeserializer {
JsonBuffer *_buffer;
TReader _reader;
TWriter _writer;
TStringStorage _stringStorage;
uint8_t _nestingLimit;
char _current;
bool _loaded;
};
template <typename TJsonBuffer, typename TReader, typename TWriter>
JsonDeserializer<TReader, TWriter> makeJsonDeserializer(TJsonBuffer *buffer,
TReader reader,
TWriter writer,
uint8_t nestingLimit) {
return JsonDeserializer<TReader, TWriter>(buffer, reader, writer,
nestingLimit);
}
} // namespace Internals
template <typename TDocument, typename TInput>
DeserializationError deserializeJson(TDocument &doc, const TInput &input) {
using namespace Internals;
return deserialize<JsonDeserializer>(doc, input);
}
template <typename TDocument, typename TInput>
DeserializationError deserializeJson(TDocument &doc, TInput *input) {
using namespace Internals;
return deserialize<JsonDeserializer>(doc, input);
}
template <typename TDocument, typename TInput>
DeserializationError deserializeJson(TDocument &doc, TInput *input,
size_t inputSize) {
using namespace Internals;
return deserialize<JsonDeserializer>(doc, input, inputSize);
}
template <typename TDocument, typename TInput>
DeserializationError deserializeJson(TDocument &doc, TInput &input) {
using namespace Internals;
return deserialize<JsonDeserializer>(doc, input);
}
} // namespace ArduinoJson

View File

@ -4,180 +4,105 @@
#pragma once
#include "../Print/DummyPrint.hpp"
#include "../Print/DynamicStringBuilder.hpp"
#include "../Print/StaticStringBuilder.hpp"
#include "./IndentedPrint.hpp"
#include "../Serialization/measure.hpp"
#include "../Serialization/serialize.hpp"
#include "./JsonWriter.hpp"
#include "./Prettyfier.hpp"
#if ARDUINOJSON_ENABLE_STD_STREAM
#include "../Print/StreamPrintAdapter.hpp"
#endif
namespace ArduinoJson {
namespace Internals {
template <typename Writer>
template <typename TPrint>
class JsonSerializer {
public:
template <typename TSource>
static void serialize(const TSource &source, Writer &writer) {
source.visit(Visitor(&writer));
JsonSerializer(TPrint &destination) : _writer(destination) {}
void acceptFloat(JsonFloat value) {
_writer.writeFloat(value);
}
struct Visitor {
Visitor(Writer *writer) : _writer(writer) {}
void acceptArray(const JsonArray &array) {
_writer.beginArray();
void acceptFloat(JsonFloat value) {
_writer->writeFloat(value);
JsonArray::const_iterator it = array.begin();
while (it != array.end()) {
it->visit(*this);
++it;
if (it == array.end()) break;
_writer.writeComma();
}
void acceptArray(const JsonArray &array) {
_writer->beginArray();
_writer.endArray();
}
JsonArray::const_iterator it = array.begin();
while (it != array.end()) {
it->visit(*this);
void acceptObject(const JsonObject &object) {
_writer.beginObject();
++it;
if (it == array.end()) break;
JsonObject::const_iterator it = object.begin();
while (it != object.end()) {
_writer.writeString(it->key);
_writer.writeColon();
it->value.visit(*this);
_writer->writeComma();
}
++it;
if (it == object.end()) break;
_writer->endArray();
_writer.writeComma();
}
void acceptObject(const JsonObject &object) {
_writer->beginObject();
_writer.endObject();
}
JsonObject::const_iterator it = object.begin();
while (it != object.end()) {
_writer->writeString(it->key);
_writer->writeColon();
it->value.visit(*this);
void acceptString(const char *value) {
_writer.writeString(value);
}
++it;
if (it == object.end()) break;
void acceptRawJson(const char *value) {
_writer.writeRaw(value);
}
_writer->writeComma();
}
void acceptNegativeInteger(JsonUInt value) {
_writer.writeRaw('-');
_writer.writeInteger(value);
}
_writer->endObject();
}
void acceptPositiveInteger(JsonUInt value) {
_writer.writeInteger(value);
}
void acceptString(const char *value) {
_writer->writeString(value);
}
void acceptBoolean(bool value) {
_writer.writeBoolean(value);
}
void acceptRawJson(const char *value) {
_writer->writeRaw(value);
}
void acceptUndefined() {}
void acceptNegativeInteger(JsonUInt value) {
_writer->writeRaw('-');
_writer->writeInteger(value);
}
size_t bytesWritten() const {
return _writer.bytesWritten();
}
void acceptPositiveInteger(JsonUInt value) {
_writer->writeInteger(value);
}
void acceptBoolean(bool value) {
_writer->writeBoolean(value);
}
void acceptUndefined() {}
Writer *_writer;
};
private:
JsonWriter<TPrint> _writer;
};
} // namespace Internals
template <typename TSource, typename TDestination>
typename Internals::enable_if<
!Internals::StringTraits<TDestination>::has_append, size_t>::type
serializeJson(const TSource &source, TDestination &destination) {
Internals::JsonWriter<TDestination> writer(destination);
Internals::JsonSerializer<Internals::JsonWriter<TDestination> >::serialize(
source, writer);
return writer.bytesWritten();
size_t serializeJson(TSource &source, TDestination &destination) {
using namespace Internals;
return serialize<JsonSerializer>(source, destination);
}
#if ARDUINOJSON_ENABLE_STD_STREAM
template <typename TSource>
std::ostream &serializeJson(const TSource &source, std::ostream &os) {
Internals::StreamPrintAdapter adapter(os);
serializeJson(source, adapter);
return os;
}
#endif
template <typename TSource>
size_t serializeJson(const TSource &source, char *buffer, size_t bufferSize) {
Internals::StaticStringBuilder sb(buffer, bufferSize);
return serializeJson(source, sb);
}
template <typename TSource, size_t N>
size_t serializeJson(const TSource &source, char (&buffer)[N]) {
return serializeJson(source, buffer, N);
}
template <typename TSource, typename TDestination>
typename Internals::enable_if<Internals::StringTraits<TDestination>::has_append,
size_t>::type
serializeJson(const TSource &source, TDestination &str) {
Internals::DynamicStringBuilder<TDestination> sb(str);
return serializeJson(source, sb);
}
template <typename TSource, typename TDestination>
size_t serializeJsonPretty(const TSource &source,
Internals::IndentedPrint<TDestination> &print) {
Internals::Prettyfier<TDestination> p(print);
return serializeJson(source, p);
}
template <typename TSource>
size_t serializeJsonPretty(const TSource &source, char *buffer,
size_t bufferSize) {
Internals::StaticStringBuilder sb(buffer, bufferSize);
return serializeJsonPretty(source, sb);
}
template <typename TSource, size_t N>
size_t serializeJsonPretty(const TSource &source, char (&buffer)[N]) {
return serializeJsonPretty(source, buffer, N);
}
template <typename TSource, typename TDestination>
typename Internals::enable_if<
!Internals::StringTraits<TDestination>::has_append, size_t>::type
serializeJsonPretty(const TSource &source, TDestination &print) {
Internals::IndentedPrint<TDestination> indentedPrint(print);
return serializeJsonPretty(source, indentedPrint);
}
template <typename TSource, typename TDestination>
typename Internals::enable_if<Internals::StringTraits<TDestination>::has_append,
size_t>::type
serializeJsonPretty(const TSource &source, TDestination &str) {
Internals::DynamicStringBuilder<TDestination> sb(str);
return serializeJsonPretty(source, sb);
using namespace Internals;
return serialize<JsonSerializer>(source, buffer, bufferSize);
}
template <typename TSource>
size_t measureJson(const TSource &source) {
Internals::DummyPrint dp;
return serializeJson(source, dp);
}
template <typename TSource>
size_t measureJsonPretty(const TSource &source) {
Internals::DummyPrint dp;
return serializeJsonPretty(source, dp);
using namespace Internals;
return measure<JsonSerializer>(source);
}
#if ARDUINOJSON_ENABLE_STD_STREAM

View File

@ -0,0 +1,57 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Serialization/measure.hpp"
#include "../Serialization/serialize.hpp"
#include "./IndentedPrint.hpp"
#include "./JsonSerializer.hpp"
#include "./Prettyfier.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TPrint>
class PrettyJsonSerializer_Base {
public:
PrettyJsonSerializer_Base(TPrint &output)
: _indentedPrint(output), _prettyfier(_indentedPrint) {}
protected:
IndentedPrint<TPrint> _indentedPrint;
Prettyfier<TPrint> _prettyfier;
};
template <typename TPrint>
class PrettyJsonSerializer : PrettyJsonSerializer_Base<TPrint>,
public JsonSerializer<Prettyfier<TPrint> > {
public:
PrettyJsonSerializer(TPrint &output)
: PrettyJsonSerializer_Base<TPrint>(output),
JsonSerializer<Prettyfier<TPrint> >(
PrettyJsonSerializer_Base<TPrint>::_prettyfier) {}
};
} // namespace Internals
template <typename TSource, typename TDestination>
size_t serializeJsonPretty(TSource &source, TDestination &destination) {
using namespace Internals;
return serialize<PrettyJsonSerializer>(source, destination);
}
template <typename TSource>
size_t serializeJsonPretty(const TSource &source, char *buffer,
size_t bufferSize) {
using namespace Internals;
return serialize<PrettyJsonSerializer>(source, buffer, bufferSize);
}
template <typename TSource>
size_t measureJsonPretty(const TSource &source) {
using namespace Internals;
return measure<PrettyJsonSerializer>(source);
}
} // namespace ArduinoJson

View File

@ -179,7 +179,7 @@ class JsonArray : public Internals::ReferenceType,
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor &visitor) const {
return visitor.acceptArray(*this);
}

View File

@ -75,7 +75,7 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor& visitor) const {
return _array.get<JsonVariant>(_index).visit(visitor);
}

View File

@ -227,7 +227,7 @@ class JsonObject : public Internals::ReferenceType,
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor& visitor) const {
return visitor.acceptObject(*this);
}

View File

@ -83,7 +83,7 @@ class JsonObjectSubscript
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor& visitor) const {
return _object.get<JsonVariant>(_key).visit(visitor);
}

View File

@ -70,7 +70,7 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
_content.asInteger = static_cast<JsonUInt>(value);
} else {
_type = JSON_NEGATIVE_INTEGER;
_content.asInteger = static_cast<JsonUInt>(-value);
_content.asInteger = ~static_cast<JsonUInt>(value) + 1;
}
}
// JsonVariant(unsigned short)
@ -308,7 +308,7 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor &visitor) const {
using namespace Internals;
switch (_type) {
case JSON_FLOAT:

View File

@ -4,26 +4,24 @@
#pragma once
#include "../DeserializationError.hpp"
#include "../Deserialization/deserialize.hpp"
#include "../JsonVariant.hpp"
#include "../Memory/JsonBuffer.hpp"
#include "../Polyfills/type_traits.hpp"
#include "../Reading/Reader.hpp"
#include "../Writing/Writer.hpp"
#include "./endianess.hpp"
#include "./ieee754.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TReader, typename TWriter>
template <typename TReader, typename TStringStorage>
class MsgPackDeserializer {
public:
MsgPackDeserializer(JsonBuffer *buffer, TReader reader, TWriter writer,
uint8_t nestingLimit)
MsgPackDeserializer(JsonBuffer *buffer, TReader reader,
TStringStorage stringStorage, uint8_t nestingLimit)
: _buffer(buffer),
_reader(reader),
_writer(writer),
_stringStorage(stringStorage),
_nestingLimit(nestingLimit) {}
DeserializationError parse(JsonVariant &variant) {
@ -221,8 +219,8 @@ class MsgPackDeserializer {
}
DeserializationError readString(JsonVariant &variant, size_t n) {
typename remove_reference<TWriter>::type::String str =
_writer.startString();
typename remove_reference<TStringStorage>::type::String str =
_stringStorage.startString();
for (; n; --n) {
uint8_t c;
if (!readBytes(c)) return DeserializationError::IncompleteInput;
@ -295,15 +293,33 @@ class MsgPackDeserializer {
JsonBuffer *_buffer;
TReader _reader;
TWriter _writer;
TStringStorage _stringStorage;
uint8_t _nestingLimit;
};
template <typename TJsonBuffer, typename TReader, typename TWriter>
MsgPackDeserializer<TReader, TWriter> makeMsgPackDeserializer(
TJsonBuffer *buffer, TReader reader, TWriter writer, uint8_t nestingLimit) {
return MsgPackDeserializer<TReader, TWriter>(buffer, reader, writer,
nestingLimit);
}
} // namespace Internals
template <typename TDocument, typename TInput>
DeserializationError deserializeMsgPack(TDocument &doc, const TInput &input) {
using namespace Internals;
return deserialize<MsgPackDeserializer>(doc, input);
}
template <typename TDocument, typename TInput>
DeserializationError deserializeMsgPack(TDocument &doc, TInput *input) {
using namespace Internals;
return deserialize<MsgPackDeserializer>(doc, input);
}
template <typename TDocument, typename TInput>
DeserializationError deserializeMsgPack(TDocument &doc, TInput *input,
size_t inputSize) {
using namespace Internals;
return deserialize<MsgPackDeserializer>(doc, input, inputSize);
}
template <typename TDocument, typename TInput>
DeserializationError deserializeMsgPack(TDocument &doc, TInput &input) {
using namespace Internals;
return deserialize<MsgPackDeserializer>(doc, input);
}
} // namespace ArduinoJson

View File

@ -0,0 +1,191 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../JsonVariant.hpp"
#include "../Polyfills/type_traits.hpp"
#include "../Serialization/measure.hpp"
#include "../Serialization/serialize.hpp"
#include "./endianess.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TPrint>
class MsgPackSerializer {
public:
MsgPackSerializer(TPrint& output) : _output(&output), _bytesWritten(0) {}
template <typename T>
typename enable_if<sizeof(T) == 4>::type acceptFloat(T value32) {
writeByte(0xCA);
writeInteger(value32);
}
template <typename T>
typename enable_if<sizeof(T) == 8>::type acceptFloat(T value64) {
float value32 = float(value64);
if (value32 == value64) {
writeByte(0xCA);
writeInteger(value32);
} else {
writeByte(0xCB);
writeInteger(value64);
}
}
void acceptArray(const JsonArray& array) {
size_t n = array.size();
if (n < 0x10) {
writeByte(uint8_t(0x90 + array.size()));
} else if (n < 0x10000) {
writeByte(0xDC);
writeInteger(uint16_t(n));
} else {
writeByte(0xDD);
writeInteger(uint32_t(n));
}
for (JsonArray::const_iterator it = array.begin(); it != array.end();
++it) {
it->visit(*this);
}
}
void acceptObject(const JsonObject& object) {
size_t n = object.size();
if (n < 0x10) {
writeByte(uint8_t(0x80 + n));
} else if (n < 0x10000) {
writeByte(0xDE);
writeInteger(uint16_t(n));
} else {
writeByte(0xDF);
writeInteger(uint32_t(n));
}
for (JsonObject::const_iterator it = object.begin(); it != object.end();
++it) {
acceptString(it->key);
it->value.visit(*this);
}
}
void acceptString(const char* value) {
if (!value) return writeByte(0xC0); // nil
size_t n = strlen(value);
if (n < 0x20) {
writeByte(uint8_t(0xA0 + n));
} else if (n < 0x100) {
writeByte(0xD9);
writeInteger(uint8_t(n));
} else if (n < 0x10000) {
writeByte(0xDA);
writeInteger(uint16_t(n));
} else {
writeByte(0xDB);
writeInteger(uint32_t(n));
}
writeBytes(reinterpret_cast<const uint8_t*>(value), n);
}
void acceptRawJson(const char* /*value*/) {}
void acceptNegativeInteger(JsonUInt value) {
JsonUInt negated = JsonUInt(~value + 1);
if (value <= 0x20) {
writeInteger(int8_t(negated));
} else if (value <= 0x80) {
writeByte(0xD0);
writeInteger(int8_t(negated));
} else if (value <= 0x8000) {
writeByte(0xD1);
writeInteger(int16_t(negated));
} else if (value <= 0x80000000) {
writeByte(0xD2);
writeInteger(int32_t(negated));
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
else {
writeByte(0xD3);
writeInteger(int64_t(negated));
}
#endif
}
void acceptPositiveInteger(JsonUInt value) {
if (value <= 0x7F) {
writeInteger(uint8_t(value));
} else if (value <= 0xFF) {
writeByte(0xCC);
writeInteger(uint8_t(value));
} else if (value <= 0xFFFF) {
writeByte(0xCD);
writeInteger(uint16_t(value));
} else if (value <= 0xFFFFFFFF) {
writeByte(0xCE);
writeInteger(uint32_t(value));
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
else {
writeByte(0xCF);
writeInteger(uint64_t(value));
}
#endif
}
void acceptBoolean(bool value) {
writeByte(value ? 0xC3 : 0xC2);
}
void acceptUndefined() {
writeByte(0xC0);
}
size_t bytesWritten() const {
return _bytesWritten;
}
private:
void writeByte(uint8_t c) {
_output->print(char(c));
_bytesWritten++;
}
void writeBytes(const uint8_t* c, size_t n) {
for (; n > 0; --n, ++c) writeByte(*c);
}
template <typename T>
void writeInteger(T value) {
fixEndianess(value);
writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
}
TPrint* _output;
size_t _bytesWritten;
};
} // namespace Internals
template <typename TSource, typename TDestination>
inline size_t serializeMsgPack(const TSource& source, TDestination& output) {
using namespace Internals;
return serialize<MsgPackSerializer>(source, output);
}
template <typename TSource, typename TDestination>
inline size_t serializeMsgPack(const TSource& source, TDestination* output,
size_t size) {
using namespace Internals;
return serialize<MsgPackSerializer>(source, output, size);
}
template <typename TSource>
inline size_t measureMsgPack(const TSource& source) {
using namespace Internals;
return measure<MsgPackSerializer>(source);
}
} // namespace ArduinoJson

View File

@ -5,17 +5,11 @@
#pragma once
#include "../Polyfills/type_traits.hpp"
#include "../Polyfills/utility.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename T>
inline void swap(T& a, T& b) {
T t(a);
a = b;
b = t;
}
inline void fixEndianess(uint8_t* p, integral_constant<size_t, 8>) {
swap(p[0], p[7]);
swap(p[1], p[6]);

View File

@ -0,0 +1,16 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
namespace ArduinoJson {
namespace Internals {
template <typename T>
inline void swap(T& a, T& b) {
T t(a);
a = b;
b = t;
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -1,11 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "./ArduinoStreamReader.hpp"
#include "./CharPointerReader.hpp"
#include "./FlashStringReader.hpp"
#include "./IteratorReader.hpp"
#include "./StdStreamReader.hpp"

View File

@ -7,7 +7,6 @@
namespace ArduinoJson {
namespace Internals {
// A dummy Print implementation used in measureJson()
class DummyPrint {
public:
size_t print(char) {

View File

@ -32,5 +32,5 @@ class StaticStringBuilder {
char *end;
char *p;
};
}
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -0,0 +1,21 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "./DummyPrint.hpp"
namespace ArduinoJson {
namespace Internals {
template <template <typename> class TSerializer, typename TSource>
size_t measure(const TSource &source) {
DummyPrint dp;
TSerializer<DummyPrint> serializer(dp);
source.visit(serializer);
return serializer.bytesWritten();
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -0,0 +1,55 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "./DynamicStringBuilder.hpp"
#include "./StaticStringBuilder.hpp"
#if ARDUINOJSON_ENABLE_STD_STREAM
#include "./StreamPrintAdapter.hpp"
#endif
namespace ArduinoJson {
namespace Internals {
template <template <typename> class TSerializer, typename TSource,
typename TPrint>
typename enable_if<!StringTraits<TPrint>::has_append, size_t>::type serialize(
const TSource &source, TPrint &destination) {
TSerializer<TPrint> serializer(destination);
source.visit(serializer);
return serializer.bytesWritten();
}
#if ARDUINOJSON_ENABLE_STD_STREAM
template <template <typename> class TSerializer, typename TSource>
size_t serialize(const TSource &source, std::ostream &os) {
StreamPrintAdapter adapter(os);
return serialize<TSerializer>(source, adapter);
}
#endif
template <template <typename> class TSerializer, typename TSource>
size_t serialize(const TSource &source, char *buffer, size_t bufferSize) {
StaticStringBuilder sb(buffer, bufferSize);
return serialize<TSerializer>(source, sb);
}
template <template <typename> class TSerializer, typename TSource, size_t N>
size_t serialize(const TSource &source, char (&buffer)[N]) {
StaticStringBuilder sb(buffer, N);
return serialize<TSerializer>(source, sb);
}
template <template <typename> class TSerializer, typename TSource,
typename TString>
typename enable_if<StringTraits<TString>::has_append, size_t>::type serialize(
const TSource &source, TString &str) {
DynamicStringBuilder<TString> sb(str);
return serialize<TSerializer>(source, sb);
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -76,7 +76,7 @@ class StaticJsonDocument {
}
template <typename Visitor>
void visit(Visitor visitor) const {
void visit(Visitor& visitor) const {
return _root.visit(visitor);
}
};

View File

@ -4,15 +4,13 @@
#pragma once
#include "./StringWriter.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TJsonBuffer>
class JsonBufferWriter {
class StringCopier {
public:
JsonBufferWriter(TJsonBuffer& jb) : _jb(&jb) {}
StringCopier(TJsonBuffer& jb) : _jb(&jb) {}
typedef typename TJsonBuffer::String String;

View File

@ -8,7 +8,7 @@ namespace ArduinoJson {
namespace Internals {
template <typename TChar>
class StringWriter {
class StringMover {
public:
class String {
public:
@ -28,7 +28,7 @@ class StringWriter {
TChar* _startPtr;
};
StringWriter(TChar* buffer) : _ptr(buffer) {}
StringMover(TChar* buffer) : _ptr(buffer) {}
String startString() {
return String(&_ptr);
@ -37,5 +37,5 @@ class StringWriter {
private:
TChar* _ptr;
};
}
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -0,0 +1,44 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "./StringCopier.hpp"
#include "./StringMover.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TJsonBuffer, typename TInput, typename Enable = void>
struct StringStorage {
typedef StringCopier<TJsonBuffer> type;
static type create(TJsonBuffer& jb, TInput&) {
return type(jb);
}
};
template <typename TJsonBuffer, typename TChar>
struct StringStorage<TJsonBuffer, TChar*,
typename enable_if<!is_const<TChar>::value>::type> {
typedef StringMover<TChar> type;
static type create(TJsonBuffer&, TChar* input) {
return type(input);
}
};
template <typename TJsonBuffer, typename TInput>
typename StringStorage<TJsonBuffer, TInput>::type makeStringStorage(
TJsonBuffer& jb, TInput& input) {
return StringStorage<TJsonBuffer, TInput>::create(jb, input);
}
template <typename TJsonBuffer, typename TChar>
typename StringStorage<TJsonBuffer, TChar*>::type makeStringStorage(
TJsonBuffer& jb, TChar* input) {
return StringStorage<TJsonBuffer, TChar*>::create(jb, input);
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -1,44 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "./JsonBufferWriter.hpp"
#include "./StringWriter.hpp"
namespace ArduinoJson {
namespace Internals {
template <typename TJsonBuffer, typename TInput, typename Enable = void>
struct Writer {
typedef JsonBufferWriter<TJsonBuffer> type;
static type create(TJsonBuffer& jb, TInput&) {
return type(jb);
}
};
template <typename TJsonBuffer, typename TChar>
struct Writer<TJsonBuffer, TChar*,
typename enable_if<!is_const<TChar>::value>::type> {
typedef StringWriter<TChar> type;
static type create(TJsonBuffer&, TChar* input) {
return type(input);
}
};
template <typename TJsonBuffer, typename TInput>
typename Writer<TJsonBuffer, TInput>::type makeWriter(TJsonBuffer& jb,
TInput& input) {
return Writer<TJsonBuffer, TInput>::create(jb, input);
}
template <typename TJsonBuffer, typename TChar>
typename Writer<TJsonBuffer, TChar*>::type makeWriter(TJsonBuffer& jb,
TChar* input) {
return Writer<TJsonBuffer, TChar*>::create(jb, input);
}
} // namespace Internals
} // namespace ArduinoJson

View File

@ -1,58 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "Json/JsonDeserializer.hpp"
#include "Reading/Reader.hpp"
#include "Writing/Writer.hpp"
namespace ArduinoJson {
// DeserializationError deserializeJson(TDocument& doc, TString input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TString = const std::string&, const String&
template <typename TDocument, typename TString>
typename Internals::enable_if<!Internals::is_array<TString>::value,
DeserializationError>::type
deserializeJson(TDocument &doc, const TString &input) {
using namespace Internals;
return makeJsonDeserializer(&doc.buffer(), makeReader(input),
makeWriter(doc.buffer(), input), doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserializeJson(TDocument& doc, TChar* input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TChar* = char*, const char*, const FlashStringHelper*
template <typename TDocument, typename TChar>
DeserializationError deserializeJson(TDocument &doc, TChar *input) {
using namespace Internals;
return makeJsonDeserializer(&doc.buffer(), makeReader(input),
makeWriter(doc.buffer(), input), doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserializeJson(TDocument& doc, TChar* input, size_t
// inputSize); TDocument = DynamicJsonDocument, StaticJsonDocument TChar* =
// char*, const char*, const FlashStringHelper*
template <typename TDocument, typename TChar>
DeserializationError deserializeJson(TDocument &doc, TChar *input,
size_t inputSize) {
using namespace Internals;
return makeJsonDeserializer(&doc.buffer(), makeReader(input, inputSize),
makeWriter(doc.buffer(), input), doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserializeJson(TDocument& doc, TStream input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TStream = std::istream&, Stream&
template <typename TDocument, typename TStream>
DeserializationError deserializeJson(TDocument &doc, TStream &input) {
using namespace Internals;
return makeJsonDeserializer(&doc.buffer(), makeReader(input),
makeWriter(doc.buffer(), input), doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
} // namespace ArduinoJson

View File

@ -1,63 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "MsgPack/MsgPackDeserializer.hpp"
#include "Reading/Reader.hpp"
#include "Writing/Writer.hpp"
namespace ArduinoJson {
// DeserializationError deserializeMsgPack(TDocument& doc, TString input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TString = const std::string&, const String&
template <typename TDocument, typename TString>
typename Internals::enable_if<!Internals::is_array<TString>::value,
DeserializationError>::type
deserializeMsgPack(TDocument &doc, const TString &input) {
using namespace Internals;
return makeMsgPackDeserializer(&doc.buffer(), makeReader(input),
makeWriter(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserializeMsgPack(TDocument& doc, TChar* input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TChar* = char*, const char*, const FlashStringHelper*
template <typename TDocument, typename TChar>
DeserializationError deserializeMsgPack(TDocument &doc, TChar *input) {
using namespace Internals;
return makeMsgPackDeserializer(&doc.buffer(), makeReader(input),
makeWriter(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserializeMsgPack(TDocument& doc, TChar* input, size_t
// inputSize);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TChar* = char*, const char*, const FlashStringHelper*
template <typename TDocument, typename TChar>
DeserializationError deserializeMsgPack(TDocument &doc, TChar *input,
size_t inputSize) {
using namespace Internals;
return makeMsgPackDeserializer(&doc.buffer(), makeReader(input, inputSize),
makeWriter(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
//
// DeserializationError deserializeMsgPack(TDocument& doc, TStream input);
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TStream = std::istream&, Stream&
template <typename TDocument, typename TStream>
DeserializationError deserializeMsgPack(TDocument &doc, TStream &input) {
using namespace Internals;
return makeMsgPackDeserializer(&doc.buffer(), makeReader(input),
makeWriter(doc.buffer(), input),
doc.nestingLimit)
.parse(doc.template to<JsonVariant>());
}
} // namespace ArduinoJson