From a209452a685ab55e262fdb396913380a27cc32a2 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Fri, 8 Jul 2022 13:55:40 +0200 Subject: [PATCH] Util: Add Json::Value at() functions --- src/util/json/array.cpp | 4 ++-- src/util/json/array.h | 6 +++--- src/util/json/object.cpp | 2 +- src/util/json/object.h | 6 +++--- src/util/json/value.cpp | 30 +++++++++++++++++++++++++++--- src/util/json/value.h | 5 +++++ 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/util/json/array.cpp b/src/util/json/array.cpp index 50e46a2..a771574 100644 --- a/src/util/json/array.cpp +++ b/src/util/json/array.cpp @@ -14,13 +14,13 @@ void Array::emplace_back(Value value) m_values.emplace_back(std::move(value)); } -Value& Array::at(size_t index) +Value& Array::operator[](size_t index) { if (index + 1 > m_values.size()) { m_values.resize(index + 1); } - return m_values.at(index); + return m_values[index]; } } // namespace Json diff --git a/src/util/json/array.h b/src/util/json/array.h index b77463f..5ab64ca 100644 --- a/src/util/json/array.h +++ b/src/util/json/array.h @@ -32,10 +32,10 @@ public: void emplace_back(Value value); - Value& at(size_t index); - Value& operator[](size_t index) { return at(index); } + Value& operator[](size_t index); + + Value& at(size_t index) { return m_values.at(index); } const Value& at(size_t index) const { return m_values.at(index); } - const Value& operator[](size_t index) const { return m_values.at(index); } size_t size() const { return m_values.size(); } const std::vector& values() const { return m_values; } diff --git a/src/util/json/object.cpp b/src/util/json/object.cpp index 8612897..4288276 100644 --- a/src/util/json/object.cpp +++ b/src/util/json/object.cpp @@ -14,7 +14,7 @@ void Object::emplace(const std::string& name, Value value) m_members.emplace(name, std::move(value)); } -Value& Object::at(const std::string& name) +Value& Object::operator[](const std::string& name) { if (m_members.find(name) == m_members.end()) { emplace(name, {}); diff --git a/src/util/json/object.h b/src/util/json/object.h index 71538ba..cf3a929 100644 --- a/src/util/json/object.h +++ b/src/util/json/object.h @@ -29,10 +29,10 @@ public: void emplace(const std::string& name, Value value); - Value& at(const std::string& name); - Value& operator[](const std::string& name) { return at(name); } + Value& operator[](const std::string& name); + + Value& at(const std::string& name) { return m_members.at(name); } const Value& at(const std::string& name) const { return m_members.at(name); } - const Value& operator[](const std::string& name) const { return m_members.at(name); } size_t size() const { return m_members.size(); } const std::map& members() const { return m_members; } diff --git a/src/util/json/value.cpp b/src/util/json/value.cpp index 3f1481a..b535bc0 100644 --- a/src/util/json/value.cpp +++ b/src/util/json/value.cpp @@ -157,7 +157,7 @@ Value& Value::operator[](size_t index) } assert(m_type == Type::Array); - return m_value.asArray->at(index); + return (*m_value.asArray)[index]; } Value& Value::operator[](const std::string& key) @@ -169,16 +169,40 @@ Value& Value::operator[](const std::string& key) } assert(m_type == Type::Object); - return m_value.asObject->at(key); + return (*m_value.asObject)[key]; } const Value& Value::operator[](size_t index) const { assert(m_type == Type::Array); - return m_value.asArray->at(index); + return (*m_value.asArray)[index]; } const Value& Value::operator[](const std::string& key) const +{ + assert(m_type == Type::Object); + return (*m_value.asObject)[key]; +} + +Value& Value::at(size_t index) +{ + assert(m_type == Type::Array); + return m_value.asArray->at(index); +} + +Value& Value::at(const std::string& key) +{ + assert(m_type == Type::Object); + return m_value.asObject->at(key); +} + +const Value& Value::at(size_t index) const +{ + assert(m_type == Type::Array); + return m_value.asArray->at(index); +} + +const Value& Value::at(const std::string& key) const { assert(m_type == Type::Object); return m_value.asObject->at(key); diff --git a/src/util/json/value.h b/src/util/json/value.h index 3ff1b82..a7c8bf9 100644 --- a/src/util/json/value.h +++ b/src/util/json/value.h @@ -63,6 +63,11 @@ public: const Value& operator[](size_t index) const; const Value& operator[](const std::string& key) const; + Value& at(size_t index); + Value& at(const std::string& key); + const Value& at(size_t index) const; + const Value& at(const std::string& key) const; + // -------------------------------------- Type type() const { return m_type; }