From af678374bcae06fe8c57abc6f430e22723bbf657 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 23 Jun 2022 14:54:29 +0200 Subject: [PATCH] Util: Add emplace() to Json::Value --- src/util/json/value.cpp | 32 +++++++++++++++++++++++++++++++- src/util/json/value.h | 3 +++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/util/json/value.cpp b/src/util/json/value.cpp index 1f56fd5..431208c 100644 --- a/src/util/json/value.cpp +++ b/src/util/json/value.cpp @@ -116,17 +116,47 @@ std::string Value::dump(const uint32_t indent, const char indentCharacter) const return stringify.dump(); } +void Value::emplace_back(Value value) +{ + // Implicitly convert null to an array + if (m_type == Type::Null) { + m_type = Type::Array; + m_value.asArray = new Array; + } + + assert(m_type == Type::Array); + m_value.asArray->emplace_back(value); +} + +void Value::emplace(const std::string& key, Value value) +{ + // Implicitly convert null to an object + if (m_type == Type::Null) { + m_type = Type::Object; + m_value.asObject = new Object; + } + + assert(m_type == Type::Array); + m_value.asObject->emplace(key, value); +} + // ------------------------------------------ Value& Value::operator[](size_t index) { + // Implicitly convert null to an array + if (m_type == Type::Null) { + m_type = Type::Array; + m_value.asArray = new Array; + } + assert(m_type == Type::Array); return m_value.asArray->at(index); } Value& Value::operator[](const std::string& key) { - // Implicit conversion to an object + // Implicitly convert null to an object if (m_type == Type::Null) { m_type = Type::Object; m_value.asObject = new Object; diff --git a/src/util/json/value.h b/src/util/json/value.h index f564496..92e90cb 100644 --- a/src/util/json/value.h +++ b/src/util/json/value.h @@ -51,6 +51,9 @@ public: static Value parse(const std::string& input); std::string dump(const uint32_t indent = 0, const char indentCharacter = ' ') const; + void emplace_back(Value value); + void emplace(const std::string& key, Value value); + // ------------------------------------------ // Array index operator