Json.NET behaviour could be defined by attributes: either use default or just throw an exception if json payload does not contain required property.
Yet System.Text.Json
serializer silently does nothing.
Having class:
public sealed class Foo
{
[Required]
public int Prop {get;set;} = 10;
}
and deserializing empty object:
JsonSerializer.Deserialize<Foo>("{}");
I simply get an instance of Foo
with Prop=10
.
I could not find any setting in JsonSerializerOptions
to force it throw an exception. Is it possible?
To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property.
Json does case-insensitive property name matching by default. The System. Text. Json default is case-sensitive, which gives better performance since it's doing an exact match.
Yet Newtonsoft. Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System.
DeserializeObject<T>(String,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to a .
System.Text.Json doesn't throw an exception if no value is received for one of the properties of the target type. You need to implement a custom converter.
Reference: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#required-properties
System.Text.Json isn't a full replacement for Json.NET. It's built for speed with minimal allocations in mind, not feature completeness. If you want validations you can
TheGeneral showed how to do #3. A custom validator would have to handle all validations explicitly and return some meaningful exception though. Throwing an ArgumentNullException
is enough if there's only a single property to check. Validating multiple properties would require something more complex like a ValidationException to include the validation results.
K. Scott Allen's article Manual Validation with Data Annotations shows how to do #2.
One option is to use the Validator.ValidateObject to validate an object and get a ValidationException
with all the failing validations :
try
{
var validationCtx=new ValidationContexgt(myFoo);
Validator.ValidateObject(myFoo,validationCtx);
}
catch(ValidatinException ex)
{
//Do something with the results.
}
This is OK if invalid objects are rare, as throwing exceptions is expensive. Perhaps a better option is to use Validator.TryValidateObject :
var results = new List<ValidationResult>();
var validationCtx=new ValidationContexgt(myFoo);
if(Validator.TryValidateObject(myFoo,validationCtx,results))
{
//Use the object
}
else
{
//Do something with the failed results
}
All you need to do SetMissingMemberHandling and it will handle every thing for you but you need to install DevBetter.JsonExtensions MissingMemberHandling.Ignore and MissingMemberHandling.Error
var deserializeOptions = new JsonSerializerOptions()
.SetMissingMemberHandling(MissingMemberHandling.Ignore);
var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, deserializeOptions);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With