Skip to content

Please provide object key (key,value) iterators #288

Open
@mgorny

Description

@mgorny

Right now iterating over objects with ValueIterator pretty much implies iterating over values, with keys being available through non-standard iterator member functions. This is a bit unfriendly, since in order to get all keys (and just keys), I can't use C++11 range-for loops and have to do something like this:

std::unordered_set<std::string> obj_keys;
for (Json::ValueConstIterator it = obj.begin(); it != obj.end(); ++it)
{
    obj_keys.insert(it.name());
}

What I would really prefer is something like this:

std::unordered_set<std::string> obj_keys;
for (const std::string& k : obj.keys())
{
    obj_keys.insert(k);
}

which would imply having keys() method return some wrapper object with begin() and end(), that would in turn give you an iterator over keys.

It would probably also make sense to have some items() method that would return a wrapper for iterator over (key, value) pairs, i.e.:

// not sure about the type signature here :)
for (const std::pair<std::string k, Json::Value v>& : obj.items())
{
}

Or possibly, even better for my case:

std::unordered_set<std::string> obj_keys{obj.keysBegin(), obj.keysEnd()};

if you don't want to go for wrapper objects :).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions