#pragma once #include #include #include #define ENROLL_COMPONENT(COMPONENT, ...) \ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(COMPONENT, __VA_ARGS__); \ template <> struct NameOf { \ static constexpr const char *name = #COMPONENT; \ }; // partial specialization (full specialization works too) namespace nlohmann { template struct adl_serializer> { static void to_json(json& j, const std::optional& opt) { if (opt == std::nullopt) { j = nullptr; } else { j = *opt; // this will call adl_serializer::to_json which will // find the free function to_json in T's namespace! } } static void from_json(const json& j, std::optional& opt) { if (j.is_null() || j == false) { opt = std::nullopt; } else { opt = std::make_optional(j.template get()); // same as above, but with adl_serializer::from_json } } }; }