At the moment reflect-cpp is inconsistent in how strict it is with which data is accepted. Missing fields in the data are treated as an error, but superfluous fields are ignored silently.
There should be modes to either treat both missing and superfluous fields as error (strict) ; as well as to treat neither as an error (lenient). (and i would argue one of these modes should be the default instead of the current mix)
For my current application I want to parse data (that I get from another program) that might be missing some fields. I could of course change all of my structs to define all variables as rfl::Fields, but instead I would prefer to be able to parse this as an argument to the load function.
I have tried to write a Processor to this end to define a default value to every field as the documentation suggested might be possible
struct DefaultIfMissing {
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple.transform([](auto field){
return (field = rfl::default_value);
});
}
};
[...]
rfl::json::load<Map, DefaultIfMissing>(path).value();
[...]
this compiles - but results in a segmentation fault. (though now that I think about it the documentation about Fields only uses these default_values to write a json - can they even be used to default the values when reading a json that is missing these fields?)
And anyway I would prefer the field to be initialized as defined in the struct. So given
struct Map {
int a{1};
int b{2};
};
and a json {"a":123} I would want something like rfl::json::load<Map, rfl::DefaultIfMissing>(path).value(); to return a Map with a==123 && b==2 instead of setting b to a default constructed int.
Is this currently possible with this library? And what do you think about a strict and a lenient parsing mode?
At the moment reflect-cpp is inconsistent in how strict it is with which data is accepted. Missing fields in the data are treated as an error, but superfluous fields are ignored silently.
There should be modes to either treat both missing and superfluous fields as error (strict) ; as well as to treat neither as an error (lenient). (and i would argue one of these modes should be the default instead of the current mix)
For my current application I want to parse data (that I get from another program) that might be missing some fields. I could of course change all of my structs to define all variables as
rfl::Fields, but instead I would prefer to be able to parse this as an argument to theloadfunction.I have tried to write a Processor to this end to define a default value to every field as the documentation suggested might be possible
this compiles - but results in a segmentation fault. (though now that I think about it the documentation about Fields only uses these
default_values to write a json - can they even be used to default the values when reading a json that is missing these fields?)And anyway I would prefer the field to be initialized as defined in the struct. So given
and a json
{"a":123}I would want something likerfl::json::load<Map, rfl::DefaultIfMissing>(path).value();to return aMapwitha==123 && b==2instead of settingbto a default constructedint.Is this currently possible with this library? And what do you think about a strict and a lenient parsing mode?