Wildmeshing Toolkit
json_serialize_enum.hpp
Go to the documentation of this file.
1 #pragma once
2 // copy of nlohmann's json serialize enum (include/nlohmann/detail/macro_scope.hpp), but without inlining
3 #define WMTK_NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
4  void to_json(nlohmann::json& j, const ENUM_TYPE& e) \
5  { \
6  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
7  static const std::pair<ENUM_TYPE, nlohmann::json> m[] = __VA_ARGS__; \
8  auto it = std::find_if(std::begin(m), std::end(m), \
9  [e](const std::pair<ENUM_TYPE, nlohmann::json>& ej_pair) -> bool \
10  { \
11  return ej_pair.first == e; \
12  }); \
13  j = ((it != std::end(m)) ? it : std::begin(m))->second; \
14  } \
15  void from_json(const nlohmann::json& j, ENUM_TYPE& e) \
16  { \
17  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
18  static const std::pair<ENUM_TYPE, nlohmann::json> m[] = __VA_ARGS__; \
19  auto it = std::find_if(std::begin(m), std::end(m), \
20  [&j](const std::pair<ENUM_TYPE, nlohmann::json>& ej_pair) -> bool \
21  { \
22  return ej_pair.second == j; \
23  }); \
24  e = ((it != std::end(m)) ? it : std::begin(m))->first; \
25  }