From b27a74c884e4d7404b44ea9c4a2ffa0c8d86777f Mon Sep 17 00:00:00 2001 From: Thulinma Date: Thu, 12 Dec 2019 00:19:14 +0100 Subject: [PATCH] Added JSON::Value::extend() --- lib/json.cpp | 18 ++++++++++++++++++ lib/json.h | 9 ++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/json.cpp b/lib/json.cpp index f67e5ef5..2316faf6 100644 --- a/lib/json.cpp +++ b/lib/json.cpp @@ -612,6 +612,24 @@ JSON::Value &JSON::Value::assignFrom(const Value &rhs, const std::set &skip){ + //Null values turn into objects automatically for sanity reasons + if (myType == EMPTY){myType = OBJECT;} + //Abort if either value is not an object + if (myType != rhs.myType || myType != OBJECT){return *this;} + jsonForEachConst(rhs, i){ + if (!skip.count(i.key())){ + if (!objVal.count(i.key()) || !i->isObject()){ + (*this)[i.key()] = *i; + }else{ + (*this)[i.key()].extend(*i, skip); + } + } + } + return *this; +} + /// Sets this JSON::Value to be equal to the given JSON::Value. JSON::Value &JSON::Value::operator=(const JSON::Value &rhs){ null(); diff --git a/lib/json.h b/lib/json.h index 9238457e..aae8717a 100644 --- a/lib/json.h +++ b/lib/json.h @@ -10,6 +10,8 @@ #include #include +static const std::set emptyset; + /// JSON-related classes and functions namespace JSON{ @@ -50,10 +52,11 @@ namespace JSON{ // comparison operators bool operator==(const Value &rhs) const; bool operator!=(const Value &rhs) const; - bool compareExcept(const Value &rhs, const std::set &skip) const; - bool compareOnly(const Value &rhs, const std::set &check) const; + bool compareExcept(const Value &rhs, const std::set &skip = emptyset) const; + bool compareOnly(const Value &rhs, const std::set &check = emptyset) const; // assignment operators - Value &assignFrom(const Value &rhs, const std::set &skip); + Value &extend(const Value &rhs, const std::set &skip = emptyset); + Value &assignFrom(const Value &rhs, const std::set &skip = emptyset); Value &operator=(const Value &rhs); Value &operator=(const std::string &rhs); Value &operator=(const char *rhs);