msgpack-c
msgpack-c copied to clipboard
Make it possible to iterate over object that is a MAP or ARRAY type-safely
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 , it is nice to have. Could you send a pull request?