mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Add support for MsgPack extension
This commit is contained in:
@ -155,7 +155,6 @@ TEST_CASE("deserialize MsgPack value") {
|
||||
|
||||
SECTION("bin 16") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto str = std::string(256, '?');
|
||||
auto input = std::string("\xc5\x01\x00", 3) + str;
|
||||
|
||||
@ -169,6 +168,127 @@ TEST_CASE("deserialize MsgPack value") {
|
||||
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
|
||||
binary.size()) == str);
|
||||
}
|
||||
|
||||
SECTION("fixext 1") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xd4\x01\x02");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 1);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == 2);
|
||||
}
|
||||
|
||||
SECTION("fixext 2") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xd5\x01\x02\x03");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 2);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == 2);
|
||||
REQUIRE(data[1] == 3);
|
||||
}
|
||||
|
||||
SECTION("fixext 4") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xd6\x01\x02\x03\x04\x05");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 4);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == 2);
|
||||
REQUIRE(data[1] == 3);
|
||||
REQUIRE(data[2] == 4);
|
||||
REQUIRE(data[3] == 5);
|
||||
}
|
||||
|
||||
SECTION("fixext 8") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xd7\x01????????");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 8);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == '?');
|
||||
REQUIRE(data[7] == '?');
|
||||
}
|
||||
|
||||
SECTION("fixext 16") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xd8\x01?????????????????");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 16);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == '?');
|
||||
REQUIRE(data[15] == '?');
|
||||
}
|
||||
|
||||
SECTION("ext 8") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xc7\x02\x01\x03\x04");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 2);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == 3);
|
||||
REQUIRE(data[1] == 4);
|
||||
}
|
||||
|
||||
SECTION("ext 16") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xc8\x00\x02\x01\x03\x04");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 2);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == 3);
|
||||
REQUIRE(data[1] == 4);
|
||||
}
|
||||
|
||||
SECTION("ext 32") {
|
||||
JsonDocument doc;
|
||||
|
||||
auto error = deserializeMsgPack(doc, "\xc9\x00\x00\x00\x02\x01\x03\x04");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto ext = doc.as<MsgPackExtension>();
|
||||
REQUIRE(ext.type() == 1);
|
||||
REQUIRE(ext.size() == 2);
|
||||
auto data = reinterpret_cast<const uint8_t*>(ext.data());
|
||||
REQUIRE(data[0] == 3);
|
||||
REQUIRE(data[1] == 4);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack() under memory constaints") {
|
||||
|
@ -184,53 +184,6 @@ TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
|
||||
}
|
||||
}
|
||||
|
||||
static std::string msgPackToJson(const char* input, size_t inputSize) {
|
||||
JsonDocument doc;
|
||||
auto err = deserializeMsgPack(doc, input, inputSize);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
return doc.as<std::string>();
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack() replaces ext types by null") {
|
||||
SECTION("ext 8") {
|
||||
REQUIRE(msgPackToJson("\x92\xc7\x01\x01\x01\x2A", 6) == "[null,42]");
|
||||
}
|
||||
|
||||
SECTION("ext 16") {
|
||||
REQUIRE(msgPackToJson("\x92\xc8\x00\x01\x01\x01\x2A", 7) == "[null,42]");
|
||||
}
|
||||
|
||||
SECTION("ext 32") {
|
||||
REQUIRE(msgPackToJson("\x92\xc9\x00\x00\x00\x01\x01\x01\x2A", 9) ==
|
||||
"[null,42]");
|
||||
}
|
||||
|
||||
SECTION("fixext 1") {
|
||||
REQUIRE(msgPackToJson("\x92\xd4\x01\x01\x2A", 5) == "[null,42]");
|
||||
}
|
||||
|
||||
SECTION("fixext 2") {
|
||||
REQUIRE(msgPackToJson("\x92\xd5\x01\x01\x02\x2A", 6) == "[null,42]");
|
||||
}
|
||||
|
||||
SECTION("fixext 4") {
|
||||
REQUIRE(msgPackToJson("\x92\xd6\x01\x01\x02\x03\x04\x2A", 8) ==
|
||||
"[null,42]");
|
||||
}
|
||||
|
||||
SECTION("fixext 8") {
|
||||
REQUIRE(msgPackToJson("\x92\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08\x2A",
|
||||
12) == "[null,42]");
|
||||
}
|
||||
|
||||
SECTION("fixext 16") {
|
||||
REQUIRE(msgPackToJson("\x92\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"
|
||||
"\x0B\x0C\x0D\x0E"
|
||||
"\x0F\x10\x2A",
|
||||
20) == "[null,42]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(
|
||||
"deserializeMsgPack() returns NoMemory when string allocation fails") {
|
||||
TimebombAllocator allocator(0);
|
||||
|
Reference in New Issue
Block a user