mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 10:47:34 +02:00
Implement the object copy at the JsonObject
level
This commit is contained in:
@ -95,7 +95,16 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// Copies an object.
|
// Copies an object.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/set/
|
// https://arduinojson.org/v6/api/jsonobject/set/
|
||||||
FORCE_INLINE bool set(JsonObjectConst src) {
|
FORCE_INLINE bool set(JsonObjectConst src) {
|
||||||
return detail::ObjectData::copy(data_, src.data_, resources_);
|
if (!data_ || !src.data_)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
clear();
|
||||||
|
for (auto kvp : src) {
|
||||||
|
if (!operator[](kvp.key()).set(kvp.value()))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets the member with specified key.
|
// Gets or sets the member with specified key.
|
||||||
|
@ -39,16 +39,6 @@ class ObjectData : public CollectionData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool copyFrom(const ObjectData& src, ResourceManager* resources);
|
|
||||||
|
|
||||||
static bool copy(ObjectData* dst, const ObjectData* src,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!dst || !src)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return dst->copyFrom(*src, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
|
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
|
||||||
|
|
||||||
|
@ -9,22 +9,6 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
inline bool ObjectData::copyFrom(const ObjectData& src,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
clear(resources);
|
|
||||||
|
|
||||||
for (auto it = src.createIterator(); !it.done(); it.next()) {
|
|
||||||
ARDUINOJSON_ASSERT(it.key() != 0);
|
|
||||||
JsonString key(it.key(),
|
|
||||||
it.ownsKey() ? JsonString::Copied : JsonString::Linked);
|
|
||||||
auto var = addMember(adaptString(key), resources);
|
|
||||||
if (!copyVariant(JsonVariant(var, resources),
|
|
||||||
JsonVariantConst(it.data(), resources)))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline VariantData* ObjectData::getMember(TAdaptedString key) const {
|
inline VariantData* ObjectData::getMember(TAdaptedString key) const {
|
||||||
return findKey(key).data();
|
return findKey(key).data();
|
||||||
|
Reference in New Issue
Block a user