Create or assign a JsonDocument from a JsonArray/JsonObject/JsonVariant

This commit is contained in:
Benoit Blanchon
2019-01-23 10:47:20 +01:00
parent 2a3b51ac3a
commit 4167b11434
10 changed files with 334 additions and 98 deletions

View File

@ -5,6 +5,12 @@
#include <ArduinoJson.h>
#include <catch.hpp>
static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) {
std::string json;
serializeJson(doc, json);
REQUIRE(json == expected);
}
TEST_CASE("StaticJsonDocument") {
SECTION("capacity()") {
SECTION("matches template argument") {
@ -36,67 +42,171 @@ TEST_CASE("StaticJsonDocument") {
doc1 = doc2;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Copy constructor") {
StaticJsonDocument<200> doc1;
deserializeJson(doc1, "{\"hello\":\"world\"}");
SECTION("Contructor") {
SECTION("Copy constructor") {
StaticJsonDocument<200> doc1;
deserializeJson(doc1, "{\"hello\":\"world\"}");
StaticJsonDocument<200> doc2 = doc1;
StaticJsonDocument<200> doc2 = doc1;
std::string json;
serializeJson(doc2, json);
REQUIRE(json == "{\"hello\":\"world\"}");
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Construct from StaticJsonDocument of different size") {
StaticJsonDocument<300> doc1;
deserializeJson(doc1, "{\"hello\":\"world\"}");
StaticJsonDocument<200> doc2 = doc1;
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Construct from DynamicJsonDocument") {
DynamicJsonDocument doc1(4096);
deserializeJson(doc1, "{\"hello\":\"world\"}");
StaticJsonDocument<200> doc2 = doc1;
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Construct from JsonObject") {
DynamicJsonDocument doc1(4096);
deserializeJson(doc1, "{\"hello\":\"world\"}");
StaticJsonDocument<200> doc2 = doc1.as<JsonObject>();
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Construct from JsonArray") {
DynamicJsonDocument doc1(4096);
deserializeJson(doc1, "[\"hello\",\"world\"]");
StaticJsonDocument<200> doc2 = doc1.as<JsonArray>();
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
}
SECTION("Construct from JsonVariant") {
DynamicJsonDocument doc1(4096);
deserializeJson(doc1, "42");
StaticJsonDocument<200> doc2 = doc1.as<JsonVariant>();
REQUIRE_JSON(doc2, "42");
}
}
SECTION("Assign from StaticJsonDocument of different capacity") {
StaticJsonDocument<200> doc1;
StaticJsonDocument<300> doc2;
doc1.to<JsonVariant>().set(666);
deserializeJson(doc2, "{\"hello\":\"world\"}");
SECTION("Assignment") {
SECTION("Copy assignment") {
StaticJsonDocument<200> doc1, doc2;
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "{\"hello\":\"world\"}");
doc1 = doc2;
doc2 = doc1;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
}
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Assign from DynamicJsonDocument") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc2, "{\"hello\":\"world\"}");
SECTION("Assign from StaticJsonDocument of different capacity") {
StaticJsonDocument<200> doc1;
StaticJsonDocument<300> doc2;
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "{\"hello\":\"world\"}");
doc1 = doc2;
doc2 = doc1;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
}
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Construct from StaticJsonDocument of different size") {
StaticJsonDocument<300> doc2;
deserializeJson(doc2, "{\"hello\":\"world\"}");
SECTION("Assign from DynamicJsonDocument") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "{\"hello\":\"world\"}");
StaticJsonDocument<200> doc1 = doc2;
doc2 = doc1;
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
}
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Construct from DynamicJsonDocument") {
DynamicJsonDocument doc2(4096);
deserializeJson(doc2, "{\"hello\":\"world\"}");
SECTION("Assign from JsonArray") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "[\"hello\",\"world\"]");
StaticJsonDocument<200> doc1 = doc2;
doc2 = doc1.as<JsonArray>();
std::string json;
serializeJson(doc1, json);
REQUIRE(json == "{\"hello\":\"world\"}");
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
}
SECTION("Assign from JsonArrayConst") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "[\"hello\",\"world\"]");
doc2 = doc1.as<JsonArrayConst>();
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
}
SECTION("Assign from JsonObject") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "{\"hello\":\"world\"}");
doc2 = doc1.as<JsonObject>();
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Assign from JsonObjectConst") {
StaticJsonDocument<200> doc1;
DynamicJsonDocument doc2(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "{\"hello\":\"world\"}");
doc2 = doc1.as<JsonObjectConst>();
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
}
SECTION("Assign from JsonVariant") {
DynamicJsonDocument doc1(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "42");
StaticJsonDocument<200> doc2;
doc2 = doc1.as<JsonVariant>();
REQUIRE_JSON(doc2, "42");
}
SECTION("Assign from JsonVariantConst") {
DynamicJsonDocument doc1(4096);
doc1.to<JsonVariant>().set(666);
deserializeJson(doc1, "42");
StaticJsonDocument<200> doc2;
doc2 = doc1.as<JsonVariantConst>();
REQUIRE_JSON(doc2, "42");
}
}
}