Adjusted json::value operator indices.

This commit is contained in:
2025-03-31 23:41:39 -04:00
parent 9546b2bcfe
commit 24a6034564
2 changed files with 36 additions and 14 deletions

View File

@@ -128,23 +128,27 @@ namespace JJX::json {
/// Converts all JSON values contained in this object / array to the derived json value types.
void convert_descendants();
#pragma region Object Access Members
/// @return The json value associated with the given key.
/// @throws std::runtime_error if value_type != object
value& at(const std::string& key);
/// @throws std::runtime_error if value_type != array
value& at(int index);
/// @see object::at()
/// @throws std::runtime_error if value_type != object
value& operator[] (const std::string& key);
/// @throws std::runtime_error if value_type != object
const value& operator[] (const std::string& key) const;
/// @throws std::runtime_error if value_type != array
value& operator[] (int index);
/// @throws std::runtime_error if value_type != array
const value& operator[] (int index) const;
/// @return True if a key-value pair in this object has a key that matches the given value.
bool contains(const std::string& key);
#pragma endregion
#pragma region Array Access Members
value& operator[] (int key);
#pragma endregion
};
/// A specialized json::value which provides STL-compatibility with std::string, and other functions for convenience.
@@ -229,6 +233,7 @@ namespace JJX::json {
explicit array(const array& v) = default;
void push_back(const value& element);
size_t size() const;
value& operator+= (const value& v);

View File

@@ -573,30 +573,45 @@ namespace JJX::json {
value::value(float v) : number(v), type(value_type::number) {}
value &value::at(const std::string &key) {
assert(type == value_type::object);
return this->as_object().at(key);
assert(type == value_type::object && "Not a JSON object");
return this->value::object->at(key);
}
value &value::operator[](const std::string &key) {
assert(type == value_type::object);
return this->as_object()[key];
// TODO: utilize std::map operator
return value::object->at(key);
}
const value &value::operator[](const std::string &key) const {
assert(type == value_type::object);
return this->as_object()[key];
// TODO: utilize std::map operator
return value::object->at(key);
}
bool value::contains(const std::string &key) {
assert(type == value_type::object);
return this->as_object().contains(key);
return this->value::object->contains(key);
}
value &value::operator[](int key) {
value &value::operator[](int index) {
assert(type == value_type::array);
return as_array()[key];
return value::array->operator[](index);
}
value &value::at(int index) {
assert(type == value_type::array && "Not a JSON array!");
return value::array->at(index);
}
const value& value::operator[](int index) const
{
assert(type == value_type::array && "Not a JSON array!");
return value::array->operator[](index);
}
struct string string(const std::string &text) {
struct string out;
out.type = value_type::string;
@@ -680,4 +695,6 @@ namespace JJX::json {
array::array(): value(std::vector<value>{}) {}
size_t array::size() const { return value::array->size();}
}