forked from bblanchon/ArduinoJson
committed by
Benoit Blanchon
parent
cd4bf33132
commit
18a9a5b590
@@ -81,6 +81,10 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(MsgPackBinary) {
|
||||
return visit(nullptr);
|
||||
}
|
||||
|
||||
size_t visit(JsonInteger value) {
|
||||
formatter_.writeInteger(value);
|
||||
return bytesWritten();
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
|
||||
class MsgPackBinary {
|
||||
public:
|
||||
MsgPackBinary() : data_(nullptr), size_(0) {}
|
||||
explicit MsgPackBinary(const void* c, size_t size) : data_(c), size_(size) {}
|
||||
|
||||
const void* data() const {
|
||||
return data_;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
private:
|
||||
const void* data_;
|
||||
size_t size_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
@@ -69,14 +69,23 @@ class MsgPackDeserializer {
|
||||
variant->setBoolean(true);
|
||||
return DeserializationError::Ok;
|
||||
|
||||
case 0xc4: // bin 8 (not supported)
|
||||
return skipString<uint8_t>();
|
||||
case 0xc4:
|
||||
if (allowValue)
|
||||
return readBinary<uint8_t>(variant);
|
||||
else
|
||||
return skipString<uint8_t>();
|
||||
|
||||
case 0xc5: // bin 16 (not supported)
|
||||
return skipString<uint16_t>();
|
||||
case 0xc5:
|
||||
if (allowValue)
|
||||
return readBinary<uint16_t>(variant);
|
||||
else
|
||||
return skipString<uint16_t>();
|
||||
|
||||
case 0xc6: // bin 32 (not supported)
|
||||
return skipString<uint32_t>();
|
||||
case 0xc6:
|
||||
if (allowValue)
|
||||
return readBinary<uint32_t>(variant);
|
||||
else
|
||||
return skipString<uint32_t>();
|
||||
|
||||
case 0xc7: // ext 8 (not supported)
|
||||
return skipExt<uint8_t>();
|
||||
@@ -394,6 +403,29 @@ class MsgPackDeserializer {
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DeserializationError::Code readBinary(VariantData* variant) {
|
||||
DeserializationError::Code err;
|
||||
T size;
|
||||
|
||||
err = readInteger(size);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return readBinary(variant, size);
|
||||
}
|
||||
|
||||
DeserializationError::Code readBinary(VariantData* variant, size_t n) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
err = readString(n);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
variant->setBinary(stringBuilder_.save());
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
template <typename TSize, typename TFilter>
|
||||
DeserializationError::Code readArray(
|
||||
VariantData* variant, TFilter filter,
|
||||
|
||||
@@ -123,6 +123,21 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(MsgPackBinary value) {
|
||||
if (value.size() <= 0xFF) {
|
||||
writeByte(0xC4);
|
||||
writeInteger(uint8_t(value.size()));
|
||||
} else if (value.size() <= 0xFFFF) {
|
||||
writeByte(0xC5);
|
||||
writeInteger(uint16_t(value.size()));
|
||||
} else {
|
||||
writeByte(0xC6);
|
||||
writeInteger(uint32_t(value.size()));
|
||||
}
|
||||
writeBytes(reinterpret_cast<const uint8_t*>(value.data()), value.size());
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(JsonInteger value) {
|
||||
if (value > 0) {
|
||||
visit(static_cast<JsonUInt>(value));
|
||||
|
||||
@@ -192,6 +192,21 @@ struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<MsgPackBinary> : private detail::VariantAttorney {
|
||||
static void toJson(MsgPackBinary src, JsonVariant dst) {
|
||||
detail::VariantData::setBinary(getData(dst), src, getResourceManager(dst));
|
||||
}
|
||||
static MsgPackBinary fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
return data ? data->asBinary() : MsgPackBinary();
|
||||
}
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
return data && data->isBinary();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
|
||||
static void toJson(detail::nullptr_t, JsonVariant dst) {
|
||||
|
||||
@@ -134,6 +134,25 @@ struct RawComparer : ComparerBase {
|
||||
using ComparerBase::visit;
|
||||
};
|
||||
|
||||
struct MsgPackBinaryComparer : ComparerBase {
|
||||
MsgPackBinary rhs_;
|
||||
|
||||
explicit MsgPackBinaryComparer(MsgPackBinary rhs) : rhs_(rhs) {}
|
||||
|
||||
CompareResult visit(MsgPackBinary lhs) {
|
||||
size_t size = rhs_.size() < lhs.size() ? rhs_.size() : lhs.size();
|
||||
int n = memcmp(lhs.data(), rhs_.data(), size);
|
||||
if (n < 0)
|
||||
return COMPARE_RESULT_LESS;
|
||||
else if (n > 0)
|
||||
return COMPARE_RESULT_GREATER;
|
||||
else
|
||||
return COMPARE_RESULT_EQUAL;
|
||||
}
|
||||
|
||||
using ComparerBase::visit;
|
||||
};
|
||||
|
||||
struct VariantComparer : ComparerBase {
|
||||
JsonVariantConst rhs;
|
||||
|
||||
@@ -164,6 +183,11 @@ struct VariantComparer : ComparerBase {
|
||||
return reverseResult(comparer);
|
||||
}
|
||||
|
||||
CompareResult visit(MsgPackBinary value) {
|
||||
MsgPackBinaryComparer comparer(value);
|
||||
return reverseResult(comparer);
|
||||
}
|
||||
|
||||
CompareResult visit(JsonInteger lhs) {
|
||||
Comparer<JsonInteger> comparer(lhs);
|
||||
return reverseResult(comparer);
|
||||
|
||||
@@ -21,6 +21,7 @@ enum {
|
||||
VALUE_IS_RAW_STRING = 0x03,
|
||||
VALUE_IS_LINKED_STRING = 0x04,
|
||||
VALUE_IS_OWNED_STRING = 0x05,
|
||||
VALUE_IS_BINARY = 0x07,
|
||||
|
||||
// CAUTION: no OWNED_VALUE_BIT below
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <ArduinoJson/Memory/StringNode.hpp>
|
||||
#include <ArduinoJson/Misc/SerializedValue.hpp>
|
||||
#include <ArduinoJson/MsgPack/MsgPackBinary.hpp>
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
@@ -48,6 +49,10 @@ class VariantData {
|
||||
return visit.visit(RawString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length));
|
||||
|
||||
case VALUE_IS_BINARY:
|
||||
return visit.visit(MsgPackBinary(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length));
|
||||
|
||||
case VALUE_IS_SIGNED_INTEGER:
|
||||
return visit.visit(content_.asSignedInteger);
|
||||
|
||||
@@ -185,6 +190,16 @@ class VariantData {
|
||||
}
|
||||
}
|
||||
|
||||
MsgPackBinary asBinary() const {
|
||||
switch (type()) {
|
||||
case VALUE_IS_BINARY:
|
||||
return MsgPackBinary(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length);
|
||||
default:
|
||||
return MsgPackBinary(nullptr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
VariantData* getElement(size_t index,
|
||||
const ResourceManager* resources) const {
|
||||
return ArrayData::getElement(asArray(), index, resources);
|
||||
@@ -274,6 +289,10 @@ class VariantData {
|
||||
return type() == VALUE_IS_LINKED_STRING || type() == VALUE_IS_OWNED_STRING;
|
||||
}
|
||||
|
||||
bool isBinary() const {
|
||||
return type() == VALUE_IS_BINARY;
|
||||
}
|
||||
|
||||
size_t nesting(const ResourceManager* resources) const {
|
||||
auto collection = asCollection();
|
||||
if (collection)
|
||||
@@ -394,6 +413,28 @@ class VariantData {
|
||||
var->setRawString(value, resources);
|
||||
}
|
||||
|
||||
void setBinary(StringNode* s) {
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
setType(VALUE_IS_BINARY);
|
||||
content_.asOwnedString = s;
|
||||
}
|
||||
|
||||
void setBinary(MsgPackBinary value, ResourceManager* resources) {
|
||||
auto dup = resources->saveString(
|
||||
adaptString(reinterpret_cast<const char*>(value.data()), value.size()));
|
||||
if (dup)
|
||||
setBinary(dup);
|
||||
else
|
||||
setNull();
|
||||
}
|
||||
|
||||
static void setBinary(VariantData* var, MsgPackBinary value,
|
||||
ResourceManager* resources) {
|
||||
if (!var)
|
||||
return;
|
||||
var->setBinary(value, resources);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void setString(TAdaptedString value, ResourceManager* resources) {
|
||||
setNull(resources);
|
||||
|
||||
Reference in New Issue
Block a user