I have several configuration structures that can be JSON serialized and deserialized as follows:
namespace config
{
struct Foo
{
std::string hostname {};
std::string ip_address{};
std::uint32_t port {};
};
JSON::Object serialize( config::Foo const& foo )
{
// ...
}
config::Foo deserialize( JSON::Object const& object )
{
// ...
}
} // namespace config
and in the serialization::JSON
namespace I have main serialize
and deserialize
functions that call specific config serialize
and deserialize
functions:
template <typename T>
Object serialize( T const& obj, Allocator& allocator )
{
return serialize( obj, allocator );
}
// ...
Questions:
Is this a good design that follows SOLID principles (specifically Single Responsibility Principle)? Would making
serialize
/deserialize
member functions of configuration structures break SRP or any of other SOLID principles? What in case adding XML serialization/deserialization?One thing that drives me towards making
serialize
/deserialize
member functions is that I would need to use template specializations to makedeserialize
function free function for each configuration structure.If I have
config::App
configuration structure, but I also have mainApp
class, would naming configuration structureconfig::AppConfig
make more sense, even thoughconfig
part repeats?
Thank you very much