Repository navigation
Replies: 1 comment
|
Thanks for the idea and the working example. Silently ignoring a misspelled key is a fair complaint, and it isn't limited to the I don't want to add a new macro or macro family at this point, though. The define-type macros already come in 24 variants, and unknown-key checking raises design questions I'd like to settle first. Derived types are one: the check needs the keys of the whole class hierarchy, otherwise the base class's keys are reported as unknown. So I'll leave this discussion open to collect use cases and ideas. In the meantime, your own macro is a good solution for your project. Some suggestions if you keep it:
On the (Written by Claude Code) |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am going to use NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT, but I'm not satisfied with its error checking behavior. When I misspell the name of any field, it will pick the default instead (so far so good) and ignore my field (I think this is bad behavior).
So I want to propose those macros as a variant of _STRICT, for which from_json bails out if there is any field within the json object, which does not exist within the target format.
My working example is:
So my main question here: Are you interested if I try to upstream this? Of course also for the other macros :-)
I've also added a type check and bail out if the json isn't an object. We'd fail later on anyways on the first .value (if there is at least one field). But here we can do by far better error reporting. This IMO should be picked to all of those macros. Maybe with a non-empty check for the target fields to maintain API compatibility.
All reactions