-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json Overflow throw exception #48350
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsAccording to https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-handle-overflow one can capture object-unknown fields inside the deserialized object by using JsonExtensionData Without adding such an extension to all my objects, how can I make the deserializer throw an exception when an unknown field is encountered? I find it strange that upon an extra comma an exception is/can be thrown, but not on a completely unknown field. I have not found any parameter where I can set this up, or somehow an efficient extension that I could add for this. For me this feature (throw exception, or in some other way capture these errors without changing all my objects) is relevant to be able to move away from hand-crafted json-Schemas and instead rely in DataAnnotations completely. In light of that, I have also not found a way to validate the annotations while deserializing. This seems the right spot to do so however, as the json deserializer is already handling each individual object in the object-tree. Something that System.ComponentModel.DataAnnotations.Validator.TryValidateObject will not do (it only does 1st level properties) Any guidance is appreciated.
|
This is describing a feature to throw an exception when a JSON property doesn't match with a CLR property. It is technically doable, but I'd like to understand the motivation here a bit more. Do you have a real-world scenario where this is useful? Is there existing functionality in other JSON serializers in or outside the .NET ecosystem that provides similar functionality? |
Right, fair enough question. I am working on an api (ordering, money, you probably understand the restriction there on error margin), where certain fields are optional. However, this optionality completely changes the result of the order. As such, a typo where a fieldname is mistyped or whatever will go unnoticed in code, but not in the end result. I can not return an error because the code simply never knows that 'something' was not as intended. I am currently able to do this with Json.Net like so:
In System.Text.Json it could be as simple as:
I don't even care to find all errors.. single error => BAM incorrect format and I return the error message to the customer. Having this extra validation won't do a complete data annotations validation of course, but at least it will make sure that the format in the Json corresponds to the format in the CLR object. This feature would make me get one step closer to phasing out json.net. At least one step.. |
MissingMemberHandling
This would then be solved :) |
Duplicate of #37483 |
According to https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-handle-overflow one can capture object-unknown fields inside the deserialized object by using JsonExtensionData
Without adding such an extension to all my objects, how can I make the deserializer throw an exception when an unknown field is encountered? I find it strange that upon an extra comma an exception is/can be thrown, but not on a completely unknown field.
I have not found any parameter where I can set this up, or somehow an efficient extension that I could add for this. For me this feature (throw exception, or in some other way capture these errors without changing all my objects) is relevant to be able to move away from hand-crafted json-Schemas and instead rely in DataAnnotations completely.
In light of that, I have also not found a way to validate the annotations while deserializing. This seems the right spot to do so however, as the json deserializer is already handling each individual object in the object-tree. Something that System.ComponentModel.DataAnnotations.Validator.TryValidateObject will not do (it only does 1st level properties)
Any guidance is appreciated.
The text was updated successfully, but these errors were encountered: