diff --git a/include/mujincontrollerclient/mujinjson.h b/include/mujincontrollerclient/mujinjson.h index 3d23a788..e8fae003 100644 --- a/include/mujincontrollerclient/mujinjson.h +++ b/include/mujincontrollerclient/mujinjson.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -636,6 +637,23 @@ inline void LoadJsonValue(const rapidjson::GenericValue& v, } } +template , typename Allocator=rapidjson::MemoryPoolAllocator<> > +inline void LoadJsonValue(const rapidjson::GenericValue& v, std::unordered_set& t) +{ + // to construct an unordered set from an array + if (!v.IsArray()) { + throw MujinJSONException("Cannot convert json type " + GetJsonTypeName(v) + " to unordered_set"); + } + + // Ensure our output is a blank slate + t.clear(); + for (typename rapidjson::GenericValue::ConstValueIterator it = v.Begin(); it != v.End(); ++it) { + U value; + LoadJsonValue(*it, value); + t.insert(value); + } +} + template, typename Allocator=rapidjson::MemoryPoolAllocator<> > inline void LoadJsonValue(const rapidjson::GenericValue& v, std::unordered_map& t) { // It doesn't make sense to construct an unordered map from anything other @@ -841,6 +859,17 @@ inline void SaveJsonValue(rapidjson::GenericValue& v, const } } +template +inline void SaveJsonValue(rapidjson::GenericValue& v, const std::unordered_set& t, Allocator2& alloc) { + v.SetArray(); + v.Reserve(t.size(), alloc); + for (typename std::unordered_set::const_iterator it = t.begin(); it != t.end(); ++it) { + rapidjson::GenericValue value; + SaveJsonValue(value, *it, alloc); + v.PushBack(value, alloc); + } +} + template inline void SaveJsonValue(rapidjson::GenericValue& v, const std::unordered_map& t, Allocator2& alloc) { v.SetObject(); @@ -1195,6 +1224,7 @@ inline bool UpdateJsonRecursively(rapidjson::GenericValue& return hasChanged; } + } // namespace mujinjson #endif