Skip to content

Add ser/des for unordered_set #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions include/mujincontrollerclient/mujinjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdexcept>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <deque>
#include <iostream>
Expand Down Expand Up @@ -636,6 +637,23 @@ inline void LoadJsonValue(const rapidjson::GenericValue<Encoding, Allocator>& v,
}
}

template <typename U, typename Encoding=rapidjson::UTF8<>, typename Allocator=rapidjson::MemoryPoolAllocator<> >
inline void LoadJsonValue(const rapidjson::GenericValue<Encoding, Allocator>& v, std::unordered_set<U>& 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<Encoding, Allocator>::ConstValueIterator it = v.Begin(); it != v.End(); ++it) {
U value;
LoadJsonValue(*it, value);
t.insert(value);
}
}

template<typename U, typename Encoding=rapidjson::UTF8<>, typename Allocator=rapidjson::MemoryPoolAllocator<> >
inline void LoadJsonValue(const rapidjson::GenericValue<Encoding, Allocator>& v, std::unordered_map<std::string, U>& t) {
// It doesn't make sense to construct an unordered map from anything other
Expand Down Expand Up @@ -841,6 +859,17 @@ inline void SaveJsonValue(rapidjson::GenericValue<Encoding, Allocator>& v, const
}
}

template <typename U, typename Encoding, typename Allocator, typename Allocator2>
inline void SaveJsonValue(rapidjson::GenericValue<Encoding, Allocator>& v, const std::unordered_set<U>& t, Allocator2& alloc) {
v.SetArray();
v.Reserve(t.size(), alloc);
for (typename std::unordered_set<U>::const_iterator it = t.begin(); it != t.end(); ++it) {
rapidjson::GenericValue<Encoding, Allocator> value;
SaveJsonValue(value, *it, alloc);
v.PushBack(value, alloc);
}
}

template<typename U, typename Encoding, typename Allocator, typename Allocator2 >
inline void SaveJsonValue(rapidjson::GenericValue<Encoding, Allocator>& v, const std::unordered_map<std::string, U>& t, Allocator2& alloc) {
v.SetObject();
Expand Down Expand Up @@ -1195,6 +1224,7 @@ inline bool UpdateJsonRecursively(rapidjson::GenericValue<Encoding, Allocator>&
return hasChanged;
}


} // namespace mujinjson

#endif