You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once from_json and to_json functions overloading appeared in the lib it became easy to build and serialize custom classes from and to json. But it can be more easy if lib provide ability to create a map for each type with json keys strings and member pointers as values.
For example right now one have to write a code like this
Regions.h
#include<string>
#include<vector>
#include<json/json.hpp>
#include"City.hpp"namespaceDataModel {
structRegion {
std::string name;
std::vector<City> cities;
};
using nlohmann::json;
voidto_json(json &j, const Region &o);
voidfrom_json(const json &j, Region &o);
}
Now keys are stored in datamodel class implementation file. It's ok but it can be implemented even shorter and more comfortable if lib could store an std::map<std::string, F T::*> for T class (Region in the example above). Of course one cannot create such a map in C++ cause value may have different types. But this kind of mapping can be implemented with variadic templates just like sqlite_orm works (look at make_column function). To implement this functionality developer can create template specialization like this:
Also if value needs extra transformation (enum for example) make_mapping function can take transformation lambda as third and fourth arguments like this (example uses BETTER_ENUM lib but it can be implemented without it or with any other lib):
The benefit is to combine from_json and to_json at one place. I mean replace two functions with one specialization (and no need to use same keys in two places)
Once
from_json
andto_json
functions overloading appeared in the lib it became easy to build and serialize custom classes from and to json. But it can be more easy if lib provide ability to create a map for each type with json keys strings and member pointers as values.For example right now one have to write a code like this
Regions.h
Region.cpp
Now keys are stored in datamodel class implementation file. It's ok but it can be implemented even shorter and more comfortable if lib could store an
std::map<std::string, F T::*>
for T class (Region
in the example above). Of course one cannot create such a map in C++ cause value may have different types. But this kind of mapping can be implemented with variadic templates just like sqlite_orm works (look atmake_column
function). To implement this functionality developer can create template specialization like this:Region.hpp
City
is omitted in the example but it also hasfrom_json
andto_json
overriden and has the only fieldstd::string name
.The text was updated successfully, but these errors were encountered: