msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

Make it possible to iterate over object that is a MAP or ARRAY type-safely

Open mika-fischer opened this issue 9 years ago • 1 comments

At the moment I see only the way of using object::via, which is dangerous. How about supporting `object::as<object_map>andobject::as<object_array>``?

Then you can iterate type-safely:

for (const auto& kv : obj.as<msgpack::object_map>())
template<>
struct convert<msgpack::object_array>
{
    const msgpack::object& operator()(const msgpack::object& o, msgpack::object_array& array) const
    {
        if (o.type != msgpack::type::ARRAY)
            throw msgpack::type_error();
        array = o.via.array;
        return o;
    }
};

template<>
struct convert<msgpack::object_map>
{
    const msgpack::object& operator()(const msgpack::object& o, msgpack::object_map& map) const
    {
        if (o.type != msgpack::type::MAP)
            throw msgpack::type_error();
        map = o.via.map;
        return o;
    }
};

mika-fischer avatar Oct 05 '16 17:10 mika-fischer

@mika-fischer , it is nice to have. Could you send a pull request?

redboltz avatar Oct 10 '16 11:10 redboltz