ARTICLE AD BOX
I want to serialize and deserialize a class with boost::json by using tag_invoke. This is a snippet of my actual code:
#include "GeometricSerializer.hpp" #include "BoundingBoxSerializer.hpp" #include "JSONKeys.hpp" #include <boost/json.hpp> namespace boost { namespace json { void tag_invoke(const value_from_tag&, value& jv, const Geometric& data) { jv = { { Key::BoundingBox, value_from(data.getBoundingBox()) }, { Key::ModelID, data.getModelID() } }; } Geometric tag_invoke(const value_to_tag<Geometric>&, const value& jv) { Geometric geometric; if (!jv.is_object()) { throw std::runtime_error("Invalid JSON format. Internal json parsing error."); } if (jv.as_object().contains(Key::BoundingBox)) { geometric.setBoundingBox(value_to<BoundingBox>(jv.as_object().at(Key::BoundingBox))); } if (!jv.as_object().contains(Key::ModelID)) { geometric.setModelID(jv.as_object().at(Key::ModelID).as_uint64()); } return geometric; } } // namespace json } // namespace boostIt works with both serialization and deserialization. What I want to do is to add a logger, so that, for example, if during the deserialization a field is missing, I can log it, because my deserialization must be permissive: I want to deserialize a json even if some field is missing (I'll use the default value for that class member), but I want to notify this to the user (something like logger->warning("Field x is missing, using default value.");
Is there a way to pass a custom argument (in my case the logger pointer) to tag_invoke?
