From 64ad0ad4a9cf82bc0f75f70535dbf9cea805281d Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 26 Jul 2021 22:50:04 +0200 Subject: [PATCH] Added JSON::Value::append() which returns a reference to a newly appended element --- lib/json.cpp | 12 ++++++++++++ lib/json.h | 1 + 2 files changed, 13 insertions(+) diff --git a/lib/json.cpp b/lib/json.cpp index 23f6c68c..e5b1d32e 100644 --- a/lib/json.cpp +++ b/lib/json.cpp @@ -1212,6 +1212,18 @@ void JSON::Value::append(const JSON::Value &rhs){ arrVal.push_back(new JSON::Value(rhs)); } +/// Appends a null value to the end of this JSON::Value array. +/// Turns this value into an array if it is not already one. +/// Returns a reference to the appended element. +JSON::Value & JSON::Value::append(){ + if (myType != ARRAY){ + null(); + myType = ARRAY; + } + arrVal.push_back(new JSON::Value()); + return **arrVal.rbegin(); +} + /// Prepends the given value to the beginning of this JSON::Value array. /// Turns this value into an array if it is not already one. void JSON::Value::prepend(const JSON::Value &rhs){ diff --git a/lib/json.h b/lib/json.h index e4c4839a..20799b9b 100644 --- a/lib/json.h +++ b/lib/json.h @@ -92,6 +92,7 @@ namespace JSON{ std::string toString() const; std::string toPrettyString(size_t indent = 0) const; void append(const Value &rhs); + Value & append(); void prepend(const Value &rhs); void shrink(uint32_t size); void removeMember(const std::string &name);