-
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
Support ignoring property only on (de)serialization #52584
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and MotivationSometimes we want to obsolete/replace properties but we don't want existing serialized content to fail. Having the ability to mark something specifically as "include this property but not on (de)serialization" would be nice. Since we already have Proposed APIpublic enum JsonIgnoreCondition
{
//
// Summary:
// Property will always be serialized and deserialized, regardless of System.Text.Json.JsonSerializerOptions.IgnoreNullValues
// configuration.
Never = 0,
//
// Summary:
// Property will always be ignored.
Always = 1,
//
// Summary:
// Property will only be ignored if it is null.
WhenWritingDefault = 2,
//
// Summary:
// If the value is null, the property is ignored during serialization. This is applied
// only to reference-type properties and fields.
WhenWritingNull = 3,
+ //
+ // Summary:
+ // The property is ignored during serialization. This is applied
+ // only to reference-type properties and fields.
+ OnSerialization = 4,
+ //
+ // Summary:
+ // The property is ignored during deserialization. This is applied
+ // only to reference-type properties and fields.
+ OnDeserialization = 5
} Usage ExamplesIn this example we had previously used a property of type string called Number. Now we decided to use a type of INT so we created a second property. We marked the first property to be ignored on serialization. So when we receive "old" serialized content with a string number, it still gets deserialized without any issue and the number is automatically "converted" into our new property. [JsonIgnore(JsonIgnoreCondition.OnSerialization)]
public string Number
{
get => string.Empty;
set => NewNumber = int.Parse(value);
}
int _NewNumber;
public int NewNumber
{
get => _NewNumber;
set
{
if (value == _NewNumber) return;
_NewNumber = value;
OnPropertyChanged(nameof(NewNumber));
}
}
|
Extending the enum has its own limits (especially w.r.t. composability), so we're likely going down the route sketched in #55781 (comment). I will close this in favor of that other issue. |
Background and Motivation
Sometimes we want to obsolete/replace properties but we don't want existing serialized content to fail. Having the ability to mark something specifically as "include this property but not on (de)serialization" would be nice. Since we already have
System.Text.Json.JsonSerializerOptions.JsonIgnoreCondition
it makes the most sense to just add two more enum values to it, instead of relying on something more complex.Proposed API
Usage Examples
In this example we had previously used a property of type string called Number. Now we decided to use a type of INT so we created a second property. We marked the first property to be ignored on serialization. So when we receive "old" serialized content with a string number, it still gets deserialized without any issue and the number is automatically "converted" into our new property. However the old string property is no longer included when we serialize the class again.
The text was updated successfully, but these errors were encountered: