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
Describe the feature in as much detail as possible.
At present, in order to provide the ability to round-trip a c++ object to JSON and back, I must write both to_json and from_json, repeating the names of the elements of the corresponding JSON object.
In order to improve this situation I have quickly built a prototype scheme, with an interface loosely modeled on the boost.serialization idea of an Archive and overloading the operator &.
structsome_settings
: json_archivable< some_settings > // this is the important bit
{
// implement json_archivabletemplate < classArchive, classSelf >
staticvoidjson_serialise(Archive &ar, Self &self)
{
ar & nvp("rateLimit", self.rate_limit); // the nvp template function ties the name to the value
ar & nvp("optionA", self.option_a);
ar & nvp("optionB", self.option_b);
ar & nvp("optionC", self.option_c);
}
byte_count rate_limit { 100_gb }; // another JSON-enabled classdouble option_a { 1.0 };
double option_b { 3.0 };
double option_c { 100.0 };
};
my particular implementation of set_if_present is as follows:
template < classT >
voidset_if_present(T &target, json const &source, std::string_view key)
try
{
auto i = source.find(key);
if (i != source.end())
{
target = i->get< T >(); // i->get_to(target);
}
}
catch (...)
{
std::throw_with_nested(std::invalid_argument("key"));
}
This allows a permissive interpretation of JSON data. This could be a customisation point if people need a different behaviour.
The text was updated successfully, but these errors were encountered:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
stalebot
added
the
state: stale
the issue has not been updated in a while and will be closed automatically soon unless it is updated
label
Feb 26, 2019
At present, in order to provide the ability to round-trip a c++ object to JSON and back, I must write both to_json and from_json, repeating the names of the elements of the corresponding JSON object.
In order to improve this situation I have quickly built a prototype scheme, with an interface loosely modeled on the boost.serialization idea of an Archive and overloading the operator
&
.Code follows:
example of use:
my particular implementation of
set_if_present
is as follows:This allows a permissive interpretation of JSON data. This could be a customisation point if people need a different behaviour.
The text was updated successfully, but these errors were encountered: