-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding ability to predeclare enum serialization with nlohmann json
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
components/utils/wmtk/components/utils/json_serialize_enum.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
// copy of nlohmann's json serialize enum (include/nlohmann/detail/macro_scope.hpp), but without inlining | ||
#define WMTK_NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ | ||
void to_json(nlohmann::json& j, const ENUM_TYPE& e) \ | ||
{ \ | ||
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \ | ||
static const std::pair<ENUM_TYPE, nlohmann::json> m[] = __VA_ARGS__; \ | ||
auto it = std::find_if(std::begin(m), std::end(m), \ | ||
[e](const std::pair<ENUM_TYPE, nlohmann::json>& ej_pair) -> bool \ | ||
{ \ | ||
return ej_pair.first == e; \ | ||
}); \ | ||
j = ((it != std::end(m)) ? it : std::begin(m))->second; \ | ||
} \ | ||
void from_json(const nlohmann::json& j, ENUM_TYPE& e) \ | ||
{ \ | ||
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \ | ||
static const std::pair<ENUM_TYPE, nlohmann::json> m[] = __VA_ARGS__; \ | ||
auto it = std::find_if(std::begin(m), std::end(m), \ | ||
[&j](const std::pair<ENUM_TYPE, nlohmann::json>& ej_pair) -> bool \ | ||
{ \ | ||
return ej_pair.second == j; \ | ||
}); \ | ||
e = ((it != std::end(m)) ? it : std::begin(m))->first; \ | ||
} |