#include "./message_serializer.hpp" #include #include #include #include template<> bool MessageSerializerCallbacks::component_get_json(MessageSerializerCallbacks& msc, const Handle h, nlohmann::json& j) { const Contact3 c = h.get().c; if (!msc.cr.valid(c)) { // while this is invalid registry state, it is valid serialization j = nullptr; std::cerr << "MSC warning: encountered invalid contact\n"; return true; } if (!msc.cr.all_of(c)) { // unlucky, this contact is purely ephemeral j = nullptr; std::cerr << "MSC warning: encountered contact without ID\n"; return true; } j = nlohmann::json::binary(msc.cr.get(c).data); return true; } template<> bool MessageSerializerCallbacks::component_emplace_or_replace_json(MessageSerializerCallbacks& msc, Handle h, const nlohmann::json& j) { if (j.is_null()) { std::cerr << "MSC warning: encountered null contact\n"; h.emplace_or_replace(); return true; } const std::vector id = j.is_binary()?j:j["bytes"]; // TODO: id lookup table, this is very inefficent for (const auto& [c_it, id_it] : msc.cr.view().each()) { if (id == id_it.data) { h.emplace_or_replace(c_it); return true; } } // TODO: should we really return false if the contact is unknown?? return false; } template<> bool MessageSerializerCallbacks::component_get_json(MessageSerializerCallbacks& msc, const Handle h, nlohmann::json& j) { const Contact3 c = h.get().c; if (!msc.cr.valid(c)) { // while this is invalid registry state, it is valid serialization j = nullptr; std::cerr << "MSC warning: encountered invalid contact\n"; return true; } if (!msc.cr.all_of(c)) { // unlucky, this contact is purely ephemeral j = nullptr; std::cerr << "MSC warning: encountered contact without ID\n"; return true; } j = nlohmann::json::binary(msc.cr.get(c).data); return true; } template<> bool MessageSerializerCallbacks::component_emplace_or_replace_json(MessageSerializerCallbacks& msc, Handle h, const nlohmann::json& j) { if (j.is_null()) { std::cerr << "MSC warning: encountered null contact\n"; h.emplace_or_replace(); return true; } const std::vector id = j.is_binary()?j:j["bytes"]; // TODO: id lookup table, this is very inefficent for (const auto& [c_it, id_it] : msc.cr.view().each()) { if (id == id_it.data) { h.emplace_or_replace(c_it); return true; } } // TODO: should we really return false if the contact is unknown?? return false; }