Reputation:
I was implementing a unit test to invoke a PATCH endpoint, using "System.Text.Json" to serialize the data and send it as content. I was always getting BAD REQUEST, until I found out that the serializer encapsulates the whole patch document inside the "Operations" list and adds a property called "ContractResolver".
My work around was to use Newtonsoft.Json serializer, that, at least by default, doesn't add the extra property and it work properly since the API accepts the JsonDocumentPatch.
Anyone now why "System.Text.Json" serializer works that way and not simply convert to JSON just like newtonsoft does? or if it is possible to config something to prevent that. I don't want to use both serializers inside the project.. Oh, and I'm using .NET6 with latests nuget packages (until the date of this post)
I have been documentation and nothing comes up regarding that.
Thanks for your time in reading this even if you don't have an answer. Cheers..
Some sample code for the serialization:
var patchOperations = new JsonPatchDocument<BodyModel>();
patchOperations.Replace(m => m.IntValue, 123);
patchOperations.Remove(m => m.StringValue);
var systemSerializer = JsonSerializer.Serialize(patchOperations);
var newtonSerializer = JsonConvert.SerializeObject(patchOperations);
The results are:
systemSerializer
{
"Operations":[
{
"value":123,
"OperationType":2,
"path":"/IntValue",
"op":"replace",
"from":null
},
{
"value":null,
"OperationType":1,
"path":"/StringValue",
"op":"remove",
"from":null
}
],
"ContractResolver":{
}
}
newtonSerializer
[
{
"value":123,
"path":"/IntValue",
"op":"replace"
},
{
"path":"/StringValue",
"op":"remove"
}
]
Upvotes: 0
Views: 3803
Reputation: 11851
The document has the content:
Newtonsoft.Json has several ways to conditionally ignore a property on serialization or deserialization:
DefaultContractResolver lets you select properties to include or ignore, based on arbitrary criteria. The NullValueHandling and DefaultValueHandling settings on JsonSerializerSettings let you specify that all null-value or default-value properties should be ignored. The NullValueHandling and DefaultValueHandling settings on the [JsonProperty] attribute let you specify individual properties that should be ignored when set to null or the default value.
System.Text.Json provides the following ways to ignore properties or fields while serializing: The [JsonIgnore] attribute on a property causes the property to be omitted from the JSON during serialization. The IgnoreReadOnlyProperties global option lets you ignore all read-only properties. If you're Including fields, the JsonSerializerOptions.IgnoreReadOnlyFields global option lets you ignore all read-only fields. The DefaultIgnoreCondition global option lets you ignore all value type properties that have default values, or ignore all reference type properties that have null values.
I tested with the codes:
var sometestmodel1 = new Sometestmodel1 { num1 = 0 };
var somemodel = new Somemodel() { Id = 1, Num=2 ,Sometestmodel1= sometestmodel1 };
var systemSerializer = System.Text.Json.JsonSerializer.Serialize(somemodel);
var newtonSerializer = JsonConvert.SerializeObject(somemodel, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
You could see the result:
Without JsonSerializerSettings:
Please check the attributes on your model properties and read the official document more carefully
Upvotes: 0