Add deserializeXxx(JsonVariant, ...) (resolves #1226)

This commit is contained in:
Benoit Blanchon
2023-07-29 05:07:20 +02:00
parent bc8ea36781
commit 43eed00cd9
8 changed files with 249 additions and 38 deletions

View File

@ -5,6 +5,7 @@
add_executable(JsonDeserializerTests
array.cpp
DeserializationError.cpp
destination_types.cpp
errors.cpp
filter.cpp
input_types.cpp

View File

@ -0,0 +1,104 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Allocators.hpp"
TEST_CASE("deserializeJson(JsonDocument&)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
auto err = deserializeJson(doc, "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[42]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
}
TEST_CASE("deserializeJson(JsonVariant)") {
SECTION("variant is bound") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
JsonVariant variant = doc[0];
auto err = deserializeJson(variant, "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("variant is unbound") {
JsonVariant variant;
auto err = deserializeJson(variant, "[42]");
REQUIRE(err == DeserializationError::NoMemory);
}
}
TEST_CASE("deserializeJson(ElementProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
SECTION("element already exists") {
auto err = deserializeJson(doc[0], "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("element must be created exists") {
auto err = deserializeJson(doc[1], "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[\"hello\",[42]]");
REQUIRE(spy.log() == AllocatorLog{});
}
}
TEST_CASE("deserializeJson(MemberProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc[std::string("hello")] = std::string("world");
spy.clearLog();
SECTION("member already exists") {
auto err = deserializeJson(doc["hello"], "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("member must be created exists") {
auto err = deserializeJson(doc["value"], "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() == AllocatorLog{});
}
}

View File

@ -6,6 +6,7 @@ add_executable(MsgPackDeserializerTests
deserializeArray.cpp
deserializeObject.cpp
deserializeVariant.cpp
destination_types.cpp
doubleToFloat.cpp
errors.cpp
filter.cpp

View File

@ -0,0 +1,104 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Allocators.hpp"
TEST_CASE("deserializeMsgPack(JsonDocument&)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
auto err = deserializeMsgPack(doc, "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[42]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
}
TEST_CASE("deserializeMsgPack(JsonVariant)") {
SECTION("variant is bound") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
JsonVariant variant = doc[0];
auto err = deserializeMsgPack(variant, "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("variant is unbound") {
JsonVariant variant;
auto err = deserializeMsgPack(variant, "\x91\x2A");
REQUIRE(err == DeserializationError::NoMemory);
}
}
TEST_CASE("deserializeMsgPack(ElementProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
SECTION("element already exists") {
auto err = deserializeMsgPack(doc[0], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("element must be created exists") {
auto err = deserializeMsgPack(doc[1], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[\"hello\",[42]]");
REQUIRE(spy.log() == AllocatorLog{});
}
}
TEST_CASE("deserializeMsgPack(MemberProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc[std::string("hello")] = std::string("world");
spy.clearLog();
SECTION("member already exists") {
auto err = deserializeMsgPack(doc["hello"], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("member must be created exists") {
auto err = deserializeMsgPack(doc["value"], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() == AllocatorLog{});
}
}