Browse Source

Util: Add size() getter to Json::Value

master
Riyyi 3 years ago
parent
commit
0465d5802a
  1. 22
      src/util/json/value.cpp
  2. 1
      src/util/json/value.h

22
src/util/json/value.cpp

@ -87,7 +87,7 @@ Value::Value(const std::initializer_list<Value>& values)
{ {
bool isObject = std::all_of(values.begin(), values.end(), [](const Value& value) { bool isObject = std::all_of(values.begin(), values.end(), [](const Value& value) {
return value.type() == Type::Array return value.type() == Type::Array
&& value.m_value.asArray->size() == 2 && value.size() == 2
&& value[0].m_type == Type::String; && 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() void Value::create()
{ {
switch (m_type) { switch (m_type) {

1
src/util/json/value.h

@ -66,6 +66,7 @@ public:
// -------------------------------------- // --------------------------------------
Type type() const { return m_type; } Type type() const { return m_type; }
size_t size() const;
bool asBool() const { return m_value.asBool; } bool asBool() const { return m_value.asBool; }
double asDouble() const { return m_value.asDouble; } double asDouble() const { return m_value.asDouble; }

Loading…
Cancel
Save