Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MsgPack bin8/bin16/bin32 support #2078

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extras/tests/MixedConfiguration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_executable(MixedConfigurationTests
use_double_1.cpp
use_long_long_0.cpp
use_long_long_1.cpp
string_length_size_4.cpp
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
)

set_target_properties(MixedConfigurationTests PROPERTIES UNITY_BUILD OFF)
Expand Down
31 changes: 31 additions & 0 deletions extras/tests/MsgPackDeserializer/deserializeVariant.cpp
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// MIT License

#include <ArduinoJson.h>
#include <array>
#include <catch.hpp>

#include "Allocators.hpp"
Expand Down Expand Up @@ -139,6 +140,36 @@ TEST_CASE("deserialize MsgPack value") {
SECTION("str 32") {
checkValue<std::string>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
}

SECTION("bin 8") {
JsonDocument doc;

DeserializationError error = deserializeMsgPack(doc, "\xc4\x01\x05");

REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 1);
REQUIRE(binary.data() != nullptr);
REQUIRE(reinterpret_cast<const char*>(binary.data())[0] == 5);
}

SECTION("bin 16") {
JsonDocument doc;

auto array = std::array<char, 0x100>({5});
auto input = std::string("\xc5\x01\x00", 3) +
std::string(array.data(), array.size());

DeserializationError error = deserializeMsgPack(doc, input);

REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 0x100);
REQUIRE(binary.data() != nullptr);
REQUIRE(reinterpret_cast<const char*>(binary.data())[0] == 5);
}
}

TEST_CASE("deserializeMsgPack() under memory constaints") {
Expand Down
14 changes: 14 additions & 0 deletions extras/tests/MsgPackSerializer/serializeVariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// MIT License

#include <ArduinoJson.h>
#include <array>
#include <catch.hpp>

template <typename T>
Expand Down Expand Up @@ -146,6 +147,19 @@ TEST_CASE("serialize MsgPack value") {
checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
}

SECTION("bin 8") {
static const auto bin8 = std::array<char, 4>({1, 2, 3, 4});
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
checkVariant(MsgPackBinary(bin8.data(), bin8.size()),
"\xC4\x04\x01\x02\x03\x04");
}

SECTION("bin 16") {
static const auto bin16 = std::array<char, 0x100>({1, 2, 3, 4});
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
std::string bin16string(bin16.data(), bin16.size());
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
checkVariant(MsgPackBinary(bin16.data(), bin16.size()),
std::string("\xC5\x01\x00", 3) + bin16string);
}

SECTION("serialize round double as integer") { // Issue #1718
checkVariant(-32768.0, "\xD1\x80\x00");
checkVariant(-129.0, "\xD1\xFF\x7F");
Expand Down
4 changes: 4 additions & 0 deletions src/ArduinoJson/Json/JsonSerializer.hpp
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/ArduinoJson/MsgPack/MsgPackBinary.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE

class MsgPackBinary {
public:
explicit MsgPackBinary(const void* c, size_t size) : data_(c), size_(size) {}
operator const void*() const {
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
return data_;
}

const void* data() const {
return data_;
}

size_t size() const {
return size_;
}

private:
const void* data_;
size_t size_;
};

ARDUINOJSON_END_PUBLIC_NAMESPACE
44 changes: 38 additions & 6 deletions src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/ArduinoJson/MsgPack/MsgPackSerializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not covered by the tests.
Please add a test in string_length_size_4.cpp

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));
Expand Down
15 changes: 15 additions & 0 deletions src/ArduinoJson/Variant/ConverterImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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->asBinary();
}
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) {
Expand Down
6 changes: 6 additions & 0 deletions src/ArduinoJson/Variant/VariantCompare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ struct VariantComparer : ComparerBase {
return reverseResult(comparer);
}

CompareResult visit(MsgPackBinary value) {
Sanae6 marked this conversation as resolved.
Show resolved Hide resolved
RawComparer comparer(
RawString(reinterpret_cast<const char*>(value.data()), value.size()));
return reverseResult(comparer);
}

CompareResult visit(JsonInteger lhs) {
Comparer<JsonInteger> comparer(lhs);
return reverseResult(comparer);
Expand Down
1 change: 1 addition & 0 deletions src/ArduinoJson/Variant/VariantContent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions src/ArduinoJson/Variant/VariantData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not covered by the tests.

}
}

VariantData* getElement(size_t index,
const ResourceManager* resources) const {
return ArrayData::getElement(asArray(), index, resources);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not covered by the tests.

var->setBinary(value, resources);
}

template <typename TAdaptedString>
void setString(TAdaptedString value, ResourceManager* resources) {
setNull(resources);
Expand Down
Loading