Browse Source

Util: Add Json::Value at() functions

master
Riyyi 2 years ago
parent
commit
a209452a68
  1. 4
      src/util/json/array.cpp
  2. 6
      src/util/json/array.h
  3. 2
      src/util/json/object.cpp
  4. 6
      src/util/json/object.h
  5. 30
      src/util/json/value.cpp
  6. 5
      src/util/json/value.h

4
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

6
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<Value>& values() const { return m_values; }

2
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, {});

6
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<std::string, Value>& members() const { return m_members; }

30
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);

5
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; }

Loading…
Cancel
Save