1
Objects and Arrays
GigabiteStudios edited this page 2026-06-17 19:04:13 -05:00

Objects and Arrays

Object roots and members

ikv::Value root(ikv::objectValue, "root");

root["name"] = "demo";
root["count"] = 3;

auto nested = root.makeObject("nested");
nested["enabled"] = true;

Relevant methods:

bool isMember(const std::string& key) const;
Value operator[](const std::string& key);
Value operator[](const char* key);
Value get(const std::string& key, const Value& fallback) const;
Value makeObject(const std::string& key);
Value makeArray(const std::string& key, ValueType element_type = nullValue);

Const overloads of operator[] return read-only handles. get returns the member when present or a shared copy of the supplied fallback.

Array roots and members

ikv::Value array_root(ikv::arrayValue, "items");

ikv::Value document;
auto values = document.makeArray("values");

nullValue creates a mixed array. A concrete type creates a typed array:

auto names = document.makeArray("names", ikv::stringValue);
auto scores = document.makeArray("scores", ikv::intValue);
auto flags = document.makeArray("flags", ikv::booleanValue);
auto players = document.makeArray("players", ikv::objectValue);

Append operations

Value append(const std::string& value);
Value append(const char* value);
Value append(std::int64_t value);
Value append(int value);
Value append(double value);
Value append(bool value);
Value appendObject();

Each successful append returns a handle to the new element.

scores.append(10);
scores.append(25);

auto player = players.appendObject();
player["name"] = "Ada";
player["score"] = 100;

A typed array rejects an incompatible append with ikv::Error. Arrays of arrays are not supported by the current public iKv C API.

Indexing

Value operator[](int index);
Value operator[](ArrayIndex index);

Both const and non-const overloads exist. Negative or out-of-range indexes throw std::out_of_range.

for (ikv::Value::ArrayIndex i = 0; i < scores.size(); ++i) {
    int score = scores[i].asInt();
}

Existing array elements cannot currently be replaced through assignment. Append a new value or rebuild the array instead.