Fixed invalid application of 'sizeof' to incomplete type (closes #783)

This commit is contained in:
Benoit Blanchon
2018-07-18 20:19:22 +02:00
parent 9bbfbd0a6a
commit 2059d610a8
4 changed files with 84 additions and 69 deletions

View File

@ -1,6 +1,12 @@
ArduinoJson: change log
=======================
HEAD
----
* Fixed `invalid application of 'sizeof' to incomplete type '__FlashStringHelper'` (issue #783)
* Fixed `char[]` not duplicated when passed to `JsonVariant::operator[]`
v6.2.1-beta
-----------

View File

@ -111,17 +111,17 @@ inline typename enable_if<IsString<TString>::value,
template <typename TImpl>
template <typename TString>
inline typename enable_if<IsString<const TString *>::value,
JsonObjectSubscript<const TString *> >::type
JsonVariantSubscripts<TImpl>::operator[](const TString *key) {
inline typename enable_if<IsString<TString *>::value,
JsonObjectSubscript<TString *> >::type
JsonVariantSubscripts<TImpl>::operator[](TString *key) {
return impl()->template as<JsonObject>()[key];
}
template <typename TImpl>
template <typename TString>
inline typename enable_if<IsString<TString *>::value,
const JsonObjectSubscript<const TString *> >::type
JsonVariantSubscripts<TImpl>::operator[](const TString *key) const {
const JsonObjectSubscript<TString *> >::type
JsonVariantSubscripts<TImpl>::operator[](TString *key) const {
return impl()->template as<JsonObject>()[key];
}

View File

@ -57,17 +57,16 @@ class JsonVariantSubscripts {
// JsonObjectSubscript operator[](TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
FORCE_INLINE typename enable_if<IsString<const TString *>::value,
JsonObjectSubscript<const TString *> >::type
operator[](const TString *key);
FORCE_INLINE typename enable_if<IsString<TString *>::value,
JsonObjectSubscript<TString *> >::type
operator[](TString *key);
//
// JsonObjectSubscript operator[](TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
FORCE_INLINE
typename enable_if<IsString<TString *>::value,
const JsonObjectSubscript<const TString *> >::type
operator[](const TString *key) const;
FORCE_INLINE typename enable_if<IsString<TString *>::value,
const JsonObjectSubscript<TString *> >::type
operator[](TString *key) const;
private:
const TImpl *impl() const {

View File

@ -6,79 +6,89 @@
#include <catch.hpp>
TEST_CASE("JsonVariant::operator[]") {
SECTION("Array") {
DynamicJsonDocument doc;
JsonArray array = doc.to<JsonArray>();
array.add("element at index 0");
array.add("element at index 1");
JsonVariant var = array;
REQUIRE(2 == var.size());
REQUIRE(std::string("element at index 0") == var[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
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
REQUIRE(var["0"].isNull());
}
SECTION("Object") {
DynamicJsonDocument doc;
JsonObject object = doc.to<JsonObject>();
object["a"] = "element at key \"a\"";
object["b"] = "element at key \"b\"";
JsonVariant var = object;
REQUIRE(2 == var.size());
REQUIRE(std::string("element at key \"a\"") == var["a"]);
REQUIRE(std::string("element at key \"b\"") == var["b"]);
REQUIRE(var["c"].isNull());
REQUIRE(var[0].isNull());
}
SECTION("Undefined") {
SECTION("The JsonVariant is undefined") {
JsonVariant var = JsonVariant();
REQUIRE(0 == var.size());
REQUIRE(var["0"].isNull());
REQUIRE(var[0].isNull());
}
SECTION("String") {
SECTION("The JsonVariant is a string") {
JsonVariant var = "hello world";
REQUIRE(0 == var.size());
REQUIRE(var["0"].isNull());
REQUIRE(var[0].isNull());
}
SECTION("ObjectSetValue") {
SECTION("The JsonVariant is a JsonArray") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
JsonVariant var = obj;
var["hello"] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
JsonArray array = doc.to<JsonArray>();
JsonVariant var = array;
SECTION("get value") {
array.add("element at index 0");
array.add("element at index 1");
REQUIRE(2 == var.size());
REQUIRE(std::string("element at index 0") == var[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
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
REQUIRE(var["0"].isNull());
}
SECTION("set value") {
array.add("hello");
var[0] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var[0]);
}
SECTION("set value in a nested object") {
array.createNestedObject();
var[0]["hello"] = "world";
REQUIRE(1 == var.size());
REQUIRE(1 == var[0].size());
REQUIRE(std::string("world") == var[0]["hello"]);
}
}
SECTION("ArraySetValue") {
SECTION("The JsonVariant is a JsonObject") {
DynamicJsonDocument doc;
JsonArray arr = doc.to<JsonArray>();
arr.add("hello");
JsonVariant var = arr;
var[0] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var[0]);
}
JsonObject object = doc.to<JsonObject>();
JsonVariant var = object;
SECTION("NestedObjectSetValue") {
DynamicJsonDocument doc;
deserializeJson(doc, "[{}]");
JsonVariant var = doc.as<JsonVariant>();
var[0]["hello"] = "world";
REQUIRE(1 == var.size());
REQUIRE(1 == var[0].size());
REQUIRE(std::string("world") == var[0]["hello"]);
SECTION("get value") {
object["a"] = "element at key \"a\"";
object["b"] = "element at key \"b\"";
REQUIRE(2 == var.size());
REQUIRE(std::string("element at key \"a\"") == var["a"]);
REQUIRE(std::string("element at key \"b\"") == var["b"]);
REQUIRE(var["c"].isNull());
REQUIRE(var[0].isNull());
}
SECTION("set value, key is a const char*") {
var["hello"] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
}
SECTION("set value, key is a char[]") {
char key[] = "hello";
var[key] = "world";
key[0] = '!'; // make sure the key is duplicated
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
}
}
}