From 0465d5802a6fc3bc60577df4bcf63ba74f99a5c5 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 7 Jul 2022 09:47:34 +0200 Subject: [PATCH] Util: Add size() getter to Json::Value --- src/util/json/value.cpp | 22 +++++++++++++++++++++- src/util/json/value.h | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/util/json/value.cpp b/src/util/json/value.cpp index 2392088..3f1481a 100644 --- a/src/util/json/value.cpp +++ b/src/util/json/value.cpp @@ -87,7 +87,7 @@ Value::Value(const std::initializer_list& values) { bool isObject = std::all_of(values.begin(), values.end(), [](const Value& value) { return value.type() == Type::Array - && value.m_value.asArray->size() == 2 + && value.size() == 2 && value[0].m_type == Type::String; }); @@ -186,6 +186,26 @@ const Value& Value::operator[](const std::string& key) const // ------------------------------------------ +size_t Value::size() const +{ + switch (m_type) { + case Type::Null: + return 0; + case Type::Bool: + case Type::Number: + case Type::String: + return 1; + case Type::Array: + return m_value.asArray->size(); + case Type::Object: + return m_value.asObject->size(); + default: + return 1; + } +} + +// ------------------------------------------ + void Value::create() { switch (m_type) { diff --git a/src/util/json/value.h b/src/util/json/value.h index 51ed646..3ff1b82 100644 --- a/src/util/json/value.h +++ b/src/util/json/value.h @@ -66,6 +66,7 @@ public: // -------------------------------------- Type type() const { return m_type; } + size_t size() const; bool asBool() const { return m_value.asBool; } double asDouble() const { return m_value.asDouble; }