#pragma once #include #include #include #include struct MessageSerializerCallbacks { using Registry = Message3Registry; using Handle = Message3Handle; Contact3Registry& cr; // nlohmann // json/msgpack using serialize_json_fn = bool(*)(MessageSerializerCallbacks& msc, const Handle h, nlohmann::json& out); entt::dense_map _serl_json; using deserialize_json_fn = bool(*)(MessageSerializerCallbacks& msc, Handle h, const nlohmann::json& in); entt::dense_map _deserl_json; template static bool component_get_json(MessageSerializerCallbacks&, const Handle h, nlohmann::json& j) { if (h.template all_of()) { if constexpr (!std::is_empty_v) { j = h.template get(); } return true; } return false; } template static bool component_emplace_or_replace_json(MessageSerializerCallbacks&, Handle h, const nlohmann::json& j) { if constexpr (std::is_empty_v) { h.template emplace_or_replace(); // assert empty json? } else { h.template emplace_or_replace(static_cast(j)); } return true; } void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info) { _serl_json[type_info.hash()] = fn; } template void registerSerializerJson( serialize_json_fn fn = component_get_json, const entt::type_info& type_info = entt::type_id() ) { registerSerializerJson(fn, type_info); } void registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info) { _deserl_json[type_info.hash()] = fn; } template void registerDeSerializerJson( deserialize_json_fn fn = component_emplace_or_replace_json, const entt::type_info& type_info = entt::type_id() ) { registerDeSerializerJson(fn, type_info); } }; // fwd namespace Message::Components { struct ContactFrom; struct ContactTo; } // make specializations known template<> bool MessageSerializerCallbacks::component_get_json(MessageSerializerCallbacks& msc, const Handle h, nlohmann::json& j); template<> bool MessageSerializerCallbacks::component_emplace_or_replace_json(MessageSerializerCallbacks& msc, Handle h, const nlohmann::json& j); template<> bool MessageSerializerCallbacks::component_get_json(MessageSerializerCallbacks& msc, const Handle h, nlohmann::json& j); template<> bool MessageSerializerCallbacks::component_emplace_or_replace_json(MessageSerializerCallbacks& msc, Handle h, const nlohmann::json& j);