mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-01 00:38:27 +01:00
Added a return value to visitors
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <ArduinoJson/MsgPack/endianess.hpp>
|
||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||
#include <ArduinoJson/Serialization/CountingDecorator.hpp>
|
||||
#include <ArduinoJson/Serialization/measure.hpp>
|
||||
#include <ArduinoJson/Serialization/serialize.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
@@ -14,19 +15,20 @@
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TWriter>
|
||||
class MsgPackSerializer {
|
||||
class MsgPackSerializer : public Visitor<size_t> {
|
||||
public:
|
||||
MsgPackSerializer(TWriter writer) : _writer(writer), _bytesWritten(0) {}
|
||||
MsgPackSerializer(TWriter writer) : _writer(writer) {}
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<sizeof(T) == 4>::type visitFloat(T value32) {
|
||||
typename enable_if<sizeof(T) == 4, size_t>::type visitFloat(T value32) {
|
||||
writeByte(0xCA);
|
||||
writeInteger(value32);
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
ARDUINOJSON_NO_SANITIZE("float-cast-overflow")
|
||||
typename enable_if<sizeof(T) == 8>::type visitFloat(T value64) {
|
||||
typename enable_if<sizeof(T) == 8, size_t>::type visitFloat(T value64) {
|
||||
float value32 = float(value64);
|
||||
if (value32 == value64) {
|
||||
writeByte(0xCA);
|
||||
@@ -35,9 +37,10 @@ class MsgPackSerializer {
|
||||
writeByte(0xCB);
|
||||
writeInteger(value64);
|
||||
}
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitArray(const CollectionData& array) {
|
||||
size_t visitArray(const CollectionData& array) {
|
||||
size_t n = array.size();
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x90 + array.size()));
|
||||
@@ -51,9 +54,10 @@ class MsgPackSerializer {
|
||||
for (VariantSlot* slot = array.head(); slot; slot = slot->next()) {
|
||||
slot->data()->accept(*this);
|
||||
}
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitObject(const CollectionData& object) {
|
||||
size_t visitObject(const CollectionData& object) {
|
||||
size_t n = object.size();
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x80 + n));
|
||||
@@ -68,9 +72,10 @@ class MsgPackSerializer {
|
||||
visitString(slot->key());
|
||||
slot->data()->accept(*this);
|
||||
}
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitString(const char* value) {
|
||||
size_t visitString(const char* value) {
|
||||
ARDUINOJSON_ASSERT(value != NULL);
|
||||
|
||||
size_t n = strlen(value);
|
||||
@@ -88,13 +93,15 @@ class MsgPackSerializer {
|
||||
writeInteger(uint32_t(n));
|
||||
}
|
||||
writeBytes(reinterpret_cast<const uint8_t*>(value), n);
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitRawJson(const char* data, size_t size) {
|
||||
size_t visitRawJson(const char* data, size_t size) {
|
||||
writeBytes(reinterpret_cast<const uint8_t*>(data), size);
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitNegativeInteger(UInt value) {
|
||||
size_t visitNegativeInteger(UInt value) {
|
||||
UInt negated = UInt(~value + 1);
|
||||
if (value <= 0x20) {
|
||||
writeInteger(int8_t(negated));
|
||||
@@ -114,9 +121,10 @@ class MsgPackSerializer {
|
||||
writeInteger(int64_t(negated));
|
||||
}
|
||||
#endif
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitPositiveInteger(UInt value) {
|
||||
size_t visitPositiveInteger(UInt value) {
|
||||
if (value <= 0x7F) {
|
||||
writeInteger(uint8_t(value));
|
||||
} else if (value <= 0xFF) {
|
||||
@@ -141,27 +149,30 @@ class MsgPackSerializer {
|
||||
writeInteger(uint64_t(value));
|
||||
}
|
||||
#endif
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitBoolean(bool value) {
|
||||
size_t visitBoolean(bool value) {
|
||||
writeByte(value ? 0xC3 : 0xC2);
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
void visitNull() {
|
||||
size_t visitNull() {
|
||||
writeByte(0xC0);
|
||||
}
|
||||
|
||||
size_t bytesWritten() const {
|
||||
return _bytesWritten;
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
private:
|
||||
size_t bytesWritten() const {
|
||||
return _writer.count();
|
||||
}
|
||||
|
||||
void writeByte(uint8_t c) {
|
||||
_bytesWritten += _writer.write(c);
|
||||
_writer.write(c);
|
||||
}
|
||||
|
||||
void writeBytes(const uint8_t* p, size_t n) {
|
||||
_bytesWritten += _writer.write(p, n);
|
||||
_writer.write(p, n);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -170,8 +181,7 @@ class MsgPackSerializer {
|
||||
writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
|
||||
}
|
||||
|
||||
TWriter _writer;
|
||||
size_t _bytesWritten;
|
||||
CountingDecorator<TWriter> _writer;
|
||||
};
|
||||
|
||||
template <typename TSource, typename TDestination>
|
||||
|
||||
Reference in New Issue
Block a user