mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-23 15:27:30 +02:00
Add a stub for createNestedObject()
This commit is contained in:
@ -17,6 +17,7 @@ endif()
|
||||
add_executable(DeprecatedTests
|
||||
add.cpp
|
||||
createNestedArray.cpp
|
||||
createNestedObject.cpp
|
||||
BasicJsonDocument.cpp
|
||||
DynamicJsonDocument.cpp
|
||||
memoryUsage.cpp
|
||||
|
111
extras/tests/Deprecated/createNestedObject.cpp
Normal file
111
extras/tests/Deprecated/createNestedObject.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedObject()") {
|
||||
JsonDocument doc;
|
||||
|
||||
SECTION("createNestedObject()") {
|
||||
JsonObject object = doc.createNestedObject();
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
}
|
||||
|
||||
SECTION("createNestedObject(const char*)") {
|
||||
JsonObject object = doc.createNestedObject("key");
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
SECTION("createNestedObject(std::string)") {
|
||||
JsonObject object = doc.createNestedObject(std::string("key"));
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("createNestedObject(VLA)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "key");
|
||||
JsonObject object = doc.createNestedObject(vla);
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::createNestedObject()") {
|
||||
JsonDocument doc;
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
JsonObject object = array.createNestedObject();
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::createNestedObject()") {
|
||||
JsonDocument doc;
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
|
||||
SECTION("createNestedObject(const char*)") {
|
||||
JsonObject nestedObject = object.createNestedObject("key");
|
||||
nestedObject["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
SECTION("createNestedObject(std::string)") {
|
||||
JsonObject nestedObject = object.createNestedObject(std::string("key"));
|
||||
nestedObject["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("createNestedObject(VLA)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "key");
|
||||
JsonObject nestedObject = object.createNestedObject(vla);
|
||||
nestedObject["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::createNestedObject()") {
|
||||
JsonDocument doc;
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("createNestedObject()") {
|
||||
JsonObject object = variant.createNestedObject();
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
|
||||
}
|
||||
|
||||
SECTION("createNestedObject(const char*)") {
|
||||
JsonObject object = variant.createNestedObject("key");
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
SECTION("createNestedObject(std::string)") {
|
||||
JsonObject object = variant.createNestedObject(std::string("key"));
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("createNestedObject(VLA)") {
|
||||
size_t i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "key");
|
||||
JsonObject object = variant.createNestedObject(vla);
|
||||
object["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
#endif
|
||||
}
|
@ -170,6 +170,10 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
return add<JsonArray>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use add<JsonObject>() instead
|
||||
ARDUINOJSON_DEPRECATED("use add<JsonObject>() instead")
|
||||
JsonObject createNestedObject() const;
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
ARDUINOJSON_DEPRECATED("always returns zero")
|
||||
size_t memoryUsage() const {
|
||||
|
@ -317,6 +317,26 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
return operator[](key).template to<JsonArray>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use add<JsonObject>() instead
|
||||
ARDUINOJSON_DEPRECATED("use add<JsonObject>() instead")
|
||||
JsonObject createNestedObject() {
|
||||
return add<JsonObject>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use doc[key].to<JsonObject>() instead
|
||||
template <typename TChar>
|
||||
ARDUINOJSON_DEPRECATED("use doc[key].to<JsonObject>() instead")
|
||||
JsonObject createNestedObject(TChar* key) {
|
||||
return operator[](key).template to<JsonObject>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use doc[key].to<JsonObject>() instead
|
||||
template <typename TString>
|
||||
ARDUINOJSON_DEPRECATED("use doc[key].to<JsonObject>() instead")
|
||||
JsonObject createNestedObject(const TString& key) {
|
||||
return operator[](key).template to<JsonObject>();
|
||||
}
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
ARDUINOJSON_DEPRECATED("always returns zero")
|
||||
size_t memoryUsage() const {
|
||||
|
@ -180,6 +180,20 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
return operator[](key).template to<JsonArray>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].to<JsonObject>() instead
|
||||
template <typename TChar>
|
||||
ARDUINOJSON_DEPRECATED("use obj[key].to<JsonObject>() instead")
|
||||
JsonObject createNestedObject(TChar* key) {
|
||||
return operator[](key).template to<JsonObject>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].to<JsonObject>() instead
|
||||
template <typename TString>
|
||||
ARDUINOJSON_DEPRECATED("use obj[key].to<JsonObject>() instead")
|
||||
JsonObject createNestedObject(const TString& key) {
|
||||
return operator[](key).template to<JsonObject>();
|
||||
}
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
ARDUINOJSON_DEPRECATED("always returns zero")
|
||||
size_t memoryUsage() const {
|
||||
|
@ -239,6 +239,20 @@ class VariantRefBase : public VariantTag {
|
||||
ARDUINOJSON_DEPRECATED("use var[key].to<JsonArray>() instead")
|
||||
JsonArray createNestedArray(const TString& key) const;
|
||||
|
||||
// DEPRECATED: use add<JsonObject>() instead
|
||||
ARDUINOJSON_DEPRECATED("use add<JsonObject>() instead")
|
||||
JsonObject createNestedObject() const;
|
||||
|
||||
// DEPRECATED: use var[key].to<JsonObject>() instead
|
||||
template <typename TChar>
|
||||
ARDUINOJSON_DEPRECATED("use var[key].to<JsonObject>() instead")
|
||||
JsonObject createNestedObject(TChar* key) const;
|
||||
|
||||
// DEPRECATED: use var[key].to<JsonObject>() instead
|
||||
template <typename TString>
|
||||
ARDUINOJSON_DEPRECATED("use var[key].to<JsonObject>() instead")
|
||||
JsonObject createNestedObject(const TString& key) const;
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
ARDUINOJSON_DEPRECATED("always returns zero")
|
||||
size_t memoryUsage() const {
|
||||
|
@ -33,6 +33,25 @@ inline JsonArray VariantRefBase<TDerived>::createNestedArray(
|
||||
return operator[](key).template to<JsonArray>();
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
inline JsonObject VariantRefBase<TDerived>::createNestedObject() const {
|
||||
return add<JsonObject>();
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename TChar>
|
||||
inline JsonObject VariantRefBase<TDerived>::createNestedObject(
|
||||
TChar* key) const {
|
||||
return operator[](key).template to<JsonObject>();
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename TString>
|
||||
inline JsonObject VariantRefBase<TDerived>::createNestedObject(
|
||||
const TString& key) const {
|
||||
return operator[](key).template to<JsonObject>();
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
inline void convertToJson(const VariantRefBase<TDerived>& src,
|
||||
JsonVariant dst) {
|
||||
|
@ -109,4 +109,8 @@ class ARDUINOJSON_DEPRECATED("use JsonDocument instead") DynamicJsonDocument
|
||||
size_t _capacity;
|
||||
};
|
||||
|
||||
inline JsonObject JsonArray::createNestedObject() const {
|
||||
return add<JsonObject>();
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
Reference in New Issue
Block a user