JsonVariant automatically promotes to JsonObject or JsonArray on write

This commit is contained in:
Benoit Blanchon
2019-01-29 14:09:09 +01:00
parent 5aea1363cc
commit 6f55d1e58f
53 changed files with 1197 additions and 541 deletions

View File

@ -71,6 +71,7 @@ if(MSVC)
)
endif()
add_subdirectory(ElementProxy)
add_subdirectory(IntegrationTests)
add_subdirectory(JsonArray)
add_subdirectory(JsonDeserializer)
@ -78,10 +79,11 @@ add_subdirectory(JsonDocument)
add_subdirectory(JsonObject)
add_subdirectory(JsonSerializer)
add_subdirectory(JsonVariant)
add_subdirectory(TextFormatter)
add_subdirectory(MemberProxy)
add_subdirectory(MemoryPool)
add_subdirectory(Misc)
add_subdirectory(MixedConfiguration)
add_subdirectory(MsgPackDeserializer)
add_subdirectory(MsgPackSerializer)
add_subdirectory(Numbers)
add_subdirectory(TextFormatter)

View File

@ -0,0 +1,11 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2018
# MIT License
add_executable(ElementProxyTests
add.cpp
set.cpp
)
target_link_libraries(ElementProxyTests catch)
add_test(ElementProxy ElementProxyTests)

26
test/ElementProxy/add.cpp Normal file
View File

@ -0,0 +1,26 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("ElementProxy::add()") {
DynamicJsonDocument doc(4096);
doc.add();
ElementProxy<JsonDocument&> ep = doc[0];
SECTION("add(int)") {
ep.add(42);
REQUIRE(doc.as<std::string>() == "[[42]]");
}
SECTION("add(const char*)") {
ep.add("world");
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
}
}

26
test/ElementProxy/set.cpp Normal file
View File

@ -0,0 +1,26 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("ElementProxy::set()") {
DynamicJsonDocument doc(4096);
doc.add();
ElementProxy<JsonDocument&> ep = doc[0];
SECTION("set(int)") {
ep.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
SECTION("set(const char*)") {
ep.set("world");
REQUIRE(doc.as<std::string>() == "[\"world\"]");
}
}

View File

@ -3,9 +3,11 @@
# MIT License
add_executable(JsonDocumentTests
add.cpp
createNested.cpp
DynamicJsonDocument.cpp
nesting.cpp
isNull.cpp
nesting.cpp
StaticJsonDocument.cpp
subscript.cpp
)

22
test/JsonDocument/add.cpp Normal file
View File

@ -0,0 +1,22 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::add()") {
DynamicJsonDocument doc(4096);
SECTION("integer") {
doc.add(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
SECTION("const char*") {
doc.add("hello");
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
}
}

View File

@ -0,0 +1,66 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::createNestedArray()") {
DynamicJsonDocument doc(4096);
SECTION("promotes to array") {
doc.createNestedArray();
REQUIRE(doc.is<JsonArray>());
}
}
TEST_CASE("JsonDocument::createNestedArray(key)") {
DynamicJsonDocument doc(4096);
SECTION("key is const char*") {
SECTION("promotes to object") {
doc.createNestedArray("hello");
REQUIRE(doc.is<JsonObject>());
}
}
SECTION("key is std::string") {
SECTION("promotes to object") {
doc.createNestedArray(std::string("hello"));
REQUIRE(doc.is<JsonObject>());
}
}
}
TEST_CASE("JsonDocument::createNestedObject()") {
DynamicJsonDocument doc(4096);
SECTION("promotes to array") {
doc.createNestedObject();
REQUIRE(doc.is<JsonArray>());
}
}
TEST_CASE("JsonDocument::createNestedObject(key)") {
DynamicJsonDocument doc(4096);
SECTION("key is const char*") {
SECTION("promotes to object") {
doc.createNestedObject("hello");
REQUIRE(doc.is<JsonObject>());
}
}
SECTION("key is std::string") {
SECTION("promotes to object") {
doc.createNestedObject(std::string("hello"));
REQUIRE(doc.is<JsonObject>());
}
}
}

View File

@ -21,6 +21,11 @@ TEST_CASE("JsonDocument::operator[]") {
REQUIRE(doc[std::string("hello")] == "world");
REQUIRE(cdoc[std::string("hello")] == "world");
}
SECTION("supports operator|") {
REQUIRE((doc["hello"] | "nope") == std::string("world"));
REQUIRE((doc["world"] | "nope") == std::string("nope"));
}
}
SECTION("array") {
@ -30,3 +35,11 @@ TEST_CASE("JsonDocument::operator[]") {
REQUIRE(cdoc[1] == "world");
}
}
TEST_CASE("JsonDocument automatically promotes to object") {
DynamicJsonDocument doc(4096);
doc["one"]["two"]["three"] = 4;
REQUIRE(doc["one"]["two"]["three"] == 4);
}

View File

@ -12,7 +12,7 @@ void check(T value, const std::string &expected) {
REQUIRE(expected.size() == returnValue);
}
TEST_CASE("serializeJson(JsonObjectSubscript)") {
TEST_CASE("serializeJson(MemberProxy)") {
DynamicJsonDocument doc(4096);
deserializeJson(doc, "{\"hello\":42}");
JsonObject obj = doc.as<JsonObject>();
@ -23,7 +23,7 @@ TEST_CASE("serializeJson(JsonObjectSubscript)") {
REQUIRE(result == "42");
}
TEST_CASE("serializeJson(JsonArraySubscript)") {
TEST_CASE("serializeJson(ElementProxy)") {
DynamicJsonDocument doc(4096);
deserializeJson(doc, "[42]");
JsonArray arr = doc.as<JsonArray>();

View File

@ -37,7 +37,7 @@ TEST_CASE("operator<<(std::ostream)") {
REQUIRE("{\"key\":\"value\"}" == os.str());
}
SECTION("JsonObjectSubscript") {
SECTION("MemberProxy") {
JsonObject object = doc.to<JsonObject>();
object["key"] = "value";
@ -55,7 +55,7 @@ TEST_CASE("operator<<(std::ostream)") {
REQUIRE("[\"value\"]" == os.str());
}
SECTION("JsonArraySubscript") {
SECTION("ElementProxy") {
JsonArray array = doc.to<JsonArray>();
array.add("value");

View File

@ -3,18 +3,21 @@
# MIT License
add_executable(JsonVariantTests
add.cpp
as.cpp
compare.cpp
copy.cpp
createNested.cpp
get.cpp
is.cpp
isnull.cpp
memoryUsage.cpp
nesting.cpp
misc.cpp
nesting.cpp
or.cpp
set.cpp
subscript.cpp
types.cpp
undefined.cpp
)

39
test/JsonVariant/add.cpp Normal file
View File

@ -0,0 +1,39 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
static const char* null = 0;
TEST_CASE("JsonVariant::add()") {
DynamicJsonDocument doc(4096);
JsonVariant var = doc.to<JsonVariant>();
SECTION("No argument") {
JsonVariant nested = var.add();
REQUIRE(var.is<JsonArray>() == true);
REQUIRE(nested.isNull() == true);
}
SECTION("integer") {
var.add(42);
REQUIRE(var.as<std::string>() == "[42]");
}
SECTION("const char*") {
var.add("hello");
REQUIRE(var.as<std::string>() == "[\"hello\"]");
}
SECTION("std::string") {
var.add(std::string("hello"));
REQUIRE(var.as<std::string>() == "[\"hello\"]");
}
}

View File

@ -0,0 +1,88 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
static const char* null = 0;
TEST_CASE("JsonVariant::createNestedObject()") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to array") {
JsonObject obj = variant.createNestedObject();
obj["value"] = "42";
REQUIRE(variant.is<JsonArray>() == true);
REQUIRE(variant[0]["value"] == 42);
REQUIRE(obj.isNull() == false);
}
SECTION("works on MemberProxy") {
JsonObject obj = variant["items"].createNestedObject();
obj["value"] = "42";
REQUIRE(variant["items"][0]["value"] == 42);
}
}
TEST_CASE("JsonVariant::createNestedArray()") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to array") {
JsonArray arr = variant.createNestedArray();
REQUIRE(variant.is<JsonArray>() == true);
REQUIRE(arr.isNull() == false);
}
SECTION("works on MemberProxy") {
JsonArray arr = variant["items"].createNestedArray();
arr.add("42");
REQUIRE(variant["items"][0][0] == 42);
}
}
TEST_CASE("JsonVariant::createNestedObject(key)") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to object") {
JsonObject obj = variant.createNestedObject("weather");
obj["temp"] = "42";
REQUIRE(variant.is<JsonObject>() == true);
REQUIRE(variant["weather"]["temp"] == 42);
}
SECTION("works on MemberProxy") {
JsonObject obj = variant["status"].createNestedObject("weather");
obj["temp"] = "42";
REQUIRE(variant["status"]["weather"]["temp"] == 42);
}
}
TEST_CASE("JsonVariant::createNestedArray(key)") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("promotes to object") {
JsonArray arr = variant.createNestedArray("items");
REQUIRE(variant.is<JsonObject>() == true);
REQUIRE(arr.isNull() == false);
}
SECTION("works on MemberProxy") {
JsonArray arr = variant["weather"].createNestedArray("temp");
arr.add("42");
REQUIRE(variant["weather"]["temp"][0] == 42);
}
}

View File

@ -3,138 +3,27 @@
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
#include <limits>
template <typename T>
void checkValue(T expected) {
TEST_CASE("JsonVariant::get()") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
JsonVariant var = doc.to<JsonVariant>();
variant.set(expected);
REQUIRE(expected == variant.as<T>());
}
SECTION("get(const char*)") {
var["value"] = 42;
template <typename T>
void checkReference(T &expected) {
JsonVariant variant = expected;
REQUIRE(expected == variant.as<T &>());
}
template <typename T>
void checkNumericType() {
DynamicJsonDocument docMin(4096), docMax(4096);
JsonVariant variantMin = docMin.to<JsonVariant>();
JsonVariant variantMax = docMax.to<JsonVariant>();
T min = std::numeric_limits<T>::min();
T max = std::numeric_limits<T>::max();
variantMin.set(min);
variantMax.set(max);
REQUIRE(min == variantMin.as<T>());
REQUIRE(max == variantMax.as<T>());
}
TEST_CASE("JsonVariant set()/get()") {
#if ARDUINOJSON_USE_LONG_LONG
SECTION("SizeOfJsonInteger") {
REQUIRE(8 == sizeof(JsonInteger));
}
#endif
SECTION("Null") {
checkValue<const char *>(NULL);
}
SECTION("const char*") {
checkValue<const char *>("hello");
}
SECTION("std::string") {
checkValue<std::string>("hello");
REQUIRE(var.get("value") == 42);
}
SECTION("False") {
checkValue<bool>(false);
}
SECTION("True") {
checkValue<bool>(true);
SECTION("get(std::string)") {
var["value"] = 42;
REQUIRE(var.get(std::string("value")) == 42);
}
SECTION("Double") {
checkNumericType<double>();
}
SECTION("Float") {
checkNumericType<float>();
}
SECTION("Char") {
checkNumericType<char>();
}
SECTION("SChar") {
checkNumericType<signed char>();
}
SECTION("SInt") {
checkNumericType<signed int>();
}
SECTION("SLong") {
checkNumericType<signed long>();
}
SECTION("SShort") {
checkNumericType<signed short>();
}
SECTION("UChar") {
checkNumericType<unsigned char>();
}
SECTION("UInt") {
checkNumericType<unsigned int>();
}
SECTION("ULong") {
checkNumericType<unsigned long>();
}
SECTION("UShort") {
checkNumericType<unsigned short>();
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("LongLong") {
checkNumericType<unsigned long long>();
}
SECTION("ULongLong") {
checkNumericType<unsigned long long>();
}
#endif
SECTION("get(int)") {
var.add().set(42);
SECTION("Int8") {
checkNumericType<int8_t>();
}
SECTION("Uint8") {
checkNumericType<uint8_t>();
}
SECTION("Int16") {
checkNumericType<int16_t>();
}
SECTION("Uint16") {
checkNumericType<uint16_t>();
}
SECTION("Int32") {
checkNumericType<int32_t>();
}
SECTION("Uint32") {
checkNumericType<uint32_t>();
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("Int64") {
checkNumericType<int64_t>();
}
SECTION("Uint64") {
checkNumericType<uint64_t>();
}
#endif
SECTION("CanStoreObject") {
DynamicJsonDocument doc(4096);
JsonObject object = doc.to<JsonObject>();
checkValue<JsonObject>(object);
REQUIRE(var.get(0) == 42);
}
}

View File

@ -30,7 +30,8 @@ TEST_CASE("JsonVariant::operator[]") {
array.add("element at index 1");
REQUIRE(2 == var.size());
REQUIRE(std::string("element at index 0") == var[0]);
var[0].as<std::string>();
// REQUIRE(std::string("element at index 0") == );
REQUIRE(std::string("element at index 1") == var[1]);
REQUIRE(std::string("element at index 0") ==
var[static_cast<unsigned char>(0)]); // issue #381
@ -171,4 +172,24 @@ TEST_CASE("JsonVariantConst::operator[]") {
REQUIRE(cvar[0].isNull());
}
}
SECTION("Auto promote null JsonVariant to JsonObject") {
var["hello"] = "world";
REQUIRE(var.is<JsonObject>() == true);
}
SECTION("Don't auto promote non-null JsonVariant to JsonObject") {
var.set(42);
var["hello"] = "world";
REQUIRE(var.is<JsonObject>() == false);
}
SECTION("Don't auto promote null JsonVariant to JsonObject when reading") {
const char* value = var["hello"];
REQUIRE(var.is<JsonObject>() == false);
REQUIRE(value == 0);
}
}

140
test/JsonVariant/types.cpp Normal file
View File

@ -0,0 +1,140 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
#include <limits>
template <typename T>
void checkValue(T expected) {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
variant.set(expected);
REQUIRE(expected == variant.as<T>());
}
template <typename T>
void checkReference(T &expected) {
JsonVariant variant = expected;
REQUIRE(expected == variant.as<T &>());
}
template <typename T>
void checkNumericType() {
DynamicJsonDocument docMin(4096), docMax(4096);
JsonVariant variantMin = docMin.to<JsonVariant>();
JsonVariant variantMax = docMax.to<JsonVariant>();
T min = std::numeric_limits<T>::min();
T max = std::numeric_limits<T>::max();
variantMin.set(min);
variantMax.set(max);
REQUIRE(min == variantMin.as<T>());
REQUIRE(max == variantMax.as<T>());
}
TEST_CASE("JsonVariant set()/get()") {
#if ARDUINOJSON_USE_LONG_LONG
SECTION("SizeOfJsonInteger") {
REQUIRE(8 == sizeof(JsonInteger));
}
#endif
SECTION("Null") {
checkValue<const char *>(NULL);
}
SECTION("const char*") {
checkValue<const char *>("hello");
}
SECTION("std::string") {
checkValue<std::string>("hello");
}
SECTION("False") {
checkValue<bool>(false);
}
SECTION("True") {
checkValue<bool>(true);
}
SECTION("Double") {
checkNumericType<double>();
}
SECTION("Float") {
checkNumericType<float>();
}
SECTION("Char") {
checkNumericType<char>();
}
SECTION("SChar") {
checkNumericType<signed char>();
}
SECTION("SInt") {
checkNumericType<signed int>();
}
SECTION("SLong") {
checkNumericType<signed long>();
}
SECTION("SShort") {
checkNumericType<signed short>();
}
SECTION("UChar") {
checkNumericType<unsigned char>();
}
SECTION("UInt") {
checkNumericType<unsigned int>();
}
SECTION("ULong") {
checkNumericType<unsigned long>();
}
SECTION("UShort") {
checkNumericType<unsigned short>();
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("LongLong") {
checkNumericType<unsigned long long>();
}
SECTION("ULongLong") {
checkNumericType<unsigned long long>();
}
#endif
SECTION("Int8") {
checkNumericType<int8_t>();
}
SECTION("Uint8") {
checkNumericType<uint8_t>();
}
SECTION("Int16") {
checkNumericType<int16_t>();
}
SECTION("Uint16") {
checkNumericType<uint16_t>();
}
SECTION("Int32") {
checkNumericType<int32_t>();
}
SECTION("Uint32") {
checkNumericType<uint32_t>();
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("Int64") {
checkNumericType<int64_t>();
}
SECTION("Uint64") {
checkNumericType<uint64_t>();
}
#endif
SECTION("CanStoreObject") {
DynamicJsonDocument doc(4096);
JsonObject object = doc.to<JsonObject>();
checkValue<JsonObject>(object);
}
}

View File

@ -0,0 +1,12 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2018
# MIT License
add_executable(MemberProxyTests
add.cpp
subscript.cpp
set.cpp
)
target_link_libraries(MemberProxyTests catch)
add_test(MemberProxy MemberProxyTests)

25
test/MemberProxy/add.cpp Normal file
View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("MemberProxy::add()") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
SECTION("add(int)") {
mp.add(42);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
}
SECTION("add(const char*)") {
mp.add("world");
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
}
}

25
test/MemberProxy/set.cpp Normal file
View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("MemberProxy::set()") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
SECTION("set(int)") {
mp.set(42);
REQUIRE(doc.as<std::string>() == "{\"hello\":42}");
}
SECTION("set(const char*)") {
mp.set("world");
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
}
}

View File

@ -0,0 +1,19 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("MemberProxy::operator[]") {
DynamicJsonDocument doc(4096);
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
SECTION("set integer") {
mp["world"] = 42;
REQUIRE(doc.as<std::string>() == "{\"hello\":{\"world\":42}}");
}
}

View File

@ -54,10 +54,10 @@ TEST_CASE("Polyfills/type_traits") {
CHECK(IsVisitable<VariantRef>::value == true);
CHECK(IsVisitable<VariantConstRef>::value == true);
CHECK(IsVisitable<ArrayRef>::value == true);
CHECK(IsVisitable<ArraySubscript>::value == true);
CHECK(IsVisitable<ElementProxy<ArrayRef> >::value == true);
CHECK(IsVisitable<ArrayConstRef>::value == true);
CHECK(IsVisitable<ObjectRef>::value == true);
CHECK(IsVisitable<ObjectSubscript<const char*> >::value == true);
CHECK((IsVisitable<MemberProxy<ObjectRef, const char*> >::value == true));
CHECK(IsVisitable<ObjectConstRef>::value == true);
CHECK(IsVisitable<DynamicJsonDocument>::value == true);
CHECK(IsVisitable<StaticJsonDocument<10> >::value == true);

View File

@ -147,7 +147,7 @@ TEST_CASE("unsigned char[]") {
}
}
SECTION("JsonObjectSubscript") {
SECTION("MemberProxy") {
SECTION("operator=") { // issue #416
unsigned char value[] = "world";
@ -181,7 +181,7 @@ TEST_CASE("unsigned char[]") {
}
}
SECTION("JsonArraySubscript") {
SECTION("ElementProxy") {
SECTION("set()") {
unsigned char value[] = "world";

View File

@ -12,7 +12,7 @@ void check(T value, const std::string &expected) {
REQUIRE(expected.size() == returnValue);
}
TEST_CASE("serializeMsgPack(JsonObjectSubscript)") {
TEST_CASE("serializeMsgPack(MemberProxy)") {
DynamicJsonDocument doc(4096);
deserializeJson(doc, "{\"hello\":42}");
JsonObject obj = doc.as<JsonObject>();
@ -23,7 +23,7 @@ TEST_CASE("serializeMsgPack(JsonObjectSubscript)") {
REQUIRE(result == "*");
}
TEST_CASE("serializeMsgPack(JsonArraySubscript)") {
TEST_CASE("serializeMsgPack(ElementProxy)") {
DynamicJsonDocument doc(4096);
deserializeJson(doc, "[42]");
JsonArray arr = doc.as<JsonArray>();