forked from bblanchon/ArduinoJson
Added StaticJsonDocument and DynamicJsonDocument.
Removed StaticJsonArray and DynamicJsonArray. Removed StaticJsonObject and DynamicJsonObject. Removed StaticJsonVariant and DynamicJsonVariant.
This commit is contained in:
@ -24,8 +24,6 @@ TEST_CASE("MsgPackError") {
|
||||
TEST_STRINGIFICATION(Ok);
|
||||
TEST_STRINGIFICATION(NotSupported);
|
||||
TEST_STRINGIFICATION(NoMemory);
|
||||
TEST_STRINGIFICATION(NotAnArray);
|
||||
TEST_STRINGIFICATION(NotAnObject);
|
||||
TEST_STRINGIFICATION(TooDeep);
|
||||
}
|
||||
|
||||
@ -33,8 +31,12 @@ TEST_CASE("MsgPackError") {
|
||||
TEST_BOOLIFICATION(Ok, false);
|
||||
TEST_BOOLIFICATION(NotSupported, true);
|
||||
TEST_BOOLIFICATION(NoMemory, true);
|
||||
TEST_BOOLIFICATION(NotAnArray, true);
|
||||
TEST_BOOLIFICATION(NotAnObject, true);
|
||||
TEST_BOOLIFICATION(TooDeep, true);
|
||||
}
|
||||
|
||||
SECTION("ostream") {
|
||||
std::stringstream s;
|
||||
s << MsgPackError::NotSupported;
|
||||
REQUIRE(s.str() == "NotSupported");
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
static void check(const char* input, MsgPackError expected,
|
||||
uint8_t nestingLimit = 10) {
|
||||
DynamicJsonVariant variant;
|
||||
DynamicJsonDocument variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input, nestingLimit);
|
||||
|
||||
|
@ -6,21 +6,14 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("not an array") {
|
||||
const char* input = "\xA0";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::NotAnArray);
|
||||
}
|
||||
DynamicJsonDocument doc;
|
||||
|
||||
SECTION("fixarray") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\x90";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
@ -29,7 +22,8 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x92\x01\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
@ -42,7 +36,8 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDC\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
@ -51,7 +46,8 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
@ -64,7 +60,8 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDD\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
@ -74,7 +71,8 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
const char* input =
|
||||
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(array, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
|
@ -5,36 +5,32 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeMsgPack(JsonObject&)") {
|
||||
DynamicJsonObject object;
|
||||
|
||||
SECTION("not an object") {
|
||||
const char* input = "\xA0";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::NotAnObject);
|
||||
}
|
||||
TEST_CASE("deserialize MsgPack object") {
|
||||
DynamicJsonDocument doc;
|
||||
|
||||
SECTION("fixmap") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\x80";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 0);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x82\xA3one\x01\xA3two\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 2);
|
||||
REQUIRE(object["one"] == 1);
|
||||
REQUIRE(object["two"] == 2);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["one"] == 1);
|
||||
REQUIRE(obj["two"] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,21 +38,25 @@ TEST_CASE("deserializeMsgPack(JsonObject&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDE\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 0);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 2);
|
||||
REQUIRE(object["H"] == "hello");
|
||||
REQUIRE(object["W"] == "world");
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["H"] == "hello");
|
||||
REQUIRE(obj["W"] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,10 +64,12 @@ TEST_CASE("deserializeMsgPack(JsonObject&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDF\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 0);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two floats") {
|
||||
@ -75,12 +77,14 @@ TEST_CASE("deserializeMsgPack(JsonObject&)") {
|
||||
"\xDF\x00\x00\x00\x02\xA4zero\xCA\x00\x00\x00\x00\xA2pi\xCA\x40\x48"
|
||||
"\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(object, input);
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(object.size() == 2);
|
||||
REQUIRE(object["zero"] == 0.0f);
|
||||
REQUIRE(object["pi"] == 3.14f);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["zero"] == 0.0f);
|
||||
REQUIRE(obj["pi"] == 3.14f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,14 @@ static const size_t epsilon = sizeof(void*);
|
||||
|
||||
template <size_t Capacity>
|
||||
static void check(const char* input, MsgPackError expected) {
|
||||
StaticJsonVariant<Capacity> variant;
|
||||
StaticJsonDocument<Capacity> variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack(StaticJsonVariant&)") {
|
||||
TEST_CASE("deserializeMsgPack(StaticJsonDocument&)") {
|
||||
SECTION("single values always fit") {
|
||||
check<0>("\xc0", MsgPackError::Ok); // nil
|
||||
check<0>("\xc2", MsgPackError::Ok); // false
|
||||
@ -82,15 +82,14 @@ TEST_CASE("deserializeMsgPack(StaticJsonVariant&)") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\x80", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01", MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x81\xA1H\x01", MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2*epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,13 +100,14 @@ TEST_CASE("deserializeMsgPack(StaticJsonVariant&)") {
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x01\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x01\xA1H\x01", MsgPackError::Ok);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x01\xA1H\x01",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2*epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,13 +119,13 @@ TEST_CASE("deserializeMsgPack(StaticJsonVariant&)") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x01\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDF\x00\x00\x00\x01\xA1H\x01",
|
||||
MsgPackError::Ok);
|
||||
MsgPackError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2*epsilon>("\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>(
|
||||
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>(
|
||||
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", MsgPackError::Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
template <typename T, typename U>
|
||||
static void check(const char* input, U expected) {
|
||||
DynamicJsonVariant variant;
|
||||
DynamicJsonDocument variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
@ -127,151 +127,4 @@ TEST_CASE("deserializeMsgPack(JsonVariant&)") {
|
||||
SECTION("str 32") {
|
||||
check<const char*>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
|
||||
}
|
||||
|
||||
SECTION("fixarray") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\x90";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x92\x01\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant[0] == 1);
|
||||
REQUIRE(variant[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array 16") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDC\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant[0] == "hello");
|
||||
REQUIRE(variant[1] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array 32") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDD\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two floats") {
|
||||
const char* input =
|
||||
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant[0] == 0.0f);
|
||||
REQUIRE(variant[1] == 3.14f);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("fixmap") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\x80";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x82\xA3one\x01\xA3two\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant["one"] == 1);
|
||||
REQUIRE(variant["two"] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 16") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDE\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant["H"] == "hello");
|
||||
REQUIRE(variant["W"] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 32") {
|
||||
DynamicJsonVariant variant;
|
||||
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDF\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two floats") {
|
||||
const char* input =
|
||||
"\xDF\x00\x00\x00\x02\xA4zero\xCA\x00\x00\x00\x00\xA2pi\xCA\x40\x48"
|
||||
"\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(variant.size() == 2);
|
||||
REQUIRE(variant["zero"] == 0.0f);
|
||||
REQUIRE(variant["pi"] == 3.14f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user