-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 option to ignore default values during serialization #779
Comments
Currently there is no support for Ignoring Default Values while Serialization in Proposed API
/// <summary>
/// Determines whether default values are ignored during serialization and deserialization.
/// The default value is false.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown if this property is set after serialization or deserialization has occurred.
/// </exception>
public bool IgnoreDefaultValues
{
get
{
return _ignoreDefaultValues;
}
set
{
VerifyMutable();
_ignoreDefaultValues = value;
}
} bool includeProperty = true;
// Check default value property.
if (options.IgnoreDefaultValues)
{
DefaultValueAttribute defaultValueAttribute = JsonPropertyInfo.GetAttribute<DefaultValueAttribute>(jsonPropertyInfo.PropertyInfo);
currentValue = jsonPropertyInfo.GetValueAsObject(state.Current.CurrentValue);
includeProperty = (defaultValueAttribute != null && object.Equals(currentValue, defaultValueAttribute.Value)) == false;
}
if (includeProperty)
{
jsonPropertyInfo.Write(ref state, writer);
} Details
Pull RequestA PR with the proposed changes is available: #42288 |
@layomia following on from https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/api-review-process.md, who is the owner of this suggestion? Is there anything we can we do to help move this through discussion? |
From @SanderSade in #681:
|
@nickevansuk we are planning for 5.0 and this issue is being discussed. The next step is to create a formal API proposal. The proposal should include API modifications, rationale, usage scenarios and code examples, and raise any open questions. This would be a good way to help move this along. Here is an example of a good one: https://github.com/dotnet/corefx/issues/4547#issue-117426449. This proposal should be added to this thread, and we'll merge it into the description of this issue for greater visibility. I'll own the feature and drive it through framework core API review. Some things to consider for this issue:
|
Regarding: If the granularity is per-property, this could probably be used to solve: |
Is there an ETA for this feature in an upcoming release? My JSON class has many bools and it would be nice to default them all to false which would greatly reduce the serialized size. |
Don't want to point out obvious but for the time being one can make value type fields nullable and use |
Yes I did consider this. My models are used with a Razor engine application and bool? will not bind to checkboxes unfortunately. |
in progress ... woot ! |
Currently, with |
I ran into the same issue as @rubo when updating my app to .NET 5. The current For context, my app implements the NuGet protocol. This protocol requires some integer and boolean properties even if they have default values (for example, the search API must return a |
@rubo, @loic-sharma this will be addressed with #39152. |
Updated by @layomia. I'm pasting the API approved in #35649, along with API review notes. An option to ignore defaults on deserialization was not added, as we couldn't identify a reasonable scenario that requires this. This proposal does not include a way to specify a custom default. This is discussed in #36236.
Video
WhenWritingDefaultValues
toWhenWritingDefault
.JsonSerializerOptions.DefaultIgnoreCondition
should throwArgumentException
rather thanInvalidOperationException
Original proposal (click to view)
There is no option to ignore the
DefaultValues
for the properties inSystem.Text.Json
.The
JSON.NET
contains this options to ignore default values.https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DefaultValueHandling.htm
Usecase scenario:
For updating the Blazor component model, need to send the values which has only changes to component and refresh the UI, even for initial rendering of component itself, we will the default values.
The text was updated successfully, but these errors were encountered: