-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to deal with extra fields which we aren't handling #9
Comments
It could be implemented through a class/struct level attribute.
The above would enforce on any json keys that do not map to a member, and that enforce may eventually be replaced by a custom exception. |
I'm not sure whether that is overloading |
Agreed, I think a mixin parameter would make more sense. |
This is turning out to be non-trivial. |
Try doing it like this: int fieldsFound = 0;
foreach(field; objectFields) {
if(field in json) {
this.field = json[field];
++fieldsFound;
} else if(!field.isOptional()) {
throw new JsonParsingException(getExtraAndMissingFields(json));
}
}
if(!ignoreExtraFields && fieldsFound != json.length) {
throw new JsonParsingException(getExtraAndMissingFields(json));
} That's just pseudo-code for what I was thinking of. |
That looks right. For some reason I assumed it would be more complicated dealing with optional fields but I guess not. |
We probably want the default to be strict. The reason for this is, if someone forgets to not ignore extra keys, no error message will be shown. It will not do what they want silently. If the default was to be strict, they get an error and then go "oh yes, I wanted to ignore extra fields". Make sense? |
Thats a valid argument, but I could also see the case where, during testing, the user never encounters extra fields and doesn't bother to pass an argument to It would be possible to change the behavior based on debug/release flags, but I'm not sure if that would feel too unpredictable. |
Yes, I tend to split it up into two parts:
For first two cases above, ETE tests should be used to test the full system. If no ETE tests are written, then the developer should at least be testing the endpoint that they are hitting manually before deploying to Prod. Tell me if you don't agree :) |
I guess the question here is:
My main use case up to this point was deserializing map data expored by Tiled. These json files were huge and contained a lot of data that I didn't care about; I just picked out the members I needed. I was assuming that a breaking API change would usually cause failues due to missing/renamed members (which are non-optional by default).
Yeah, it would be bad not to catch something like that. I'm re-opening, as I'm leaning towards the more strict approach being the default. |
Ok, looking forward to the change :) |
Hey,
If I have:
and:
I have no idea that I am not converting "i". Is it possible to make it so there's an option to disallow unknown fields?
The text was updated successfully, but these errors were encountered: