Util: Add Json::Value at() functions

This commit is contained in:
Riyyi
2022-07-08 13:55:40 +02:00
parent 4487c800df
commit a209452a68
6 changed files with 41 additions and 12 deletions
+2 -2
View File
@@ -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
+3 -3
View File
@@ -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<Value>& values() const { return m_values; }
+1 -1
View File
@@ -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, {});
+3 -3
View File
@@ -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<std::string, Value>& members() const { return m_members; }
+27 -3
View File
@@ -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);
+5
View File
@@ -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; }