Skip to content
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

Closed
Symbai opened this issue May 11, 2021 · 3 comments
Closed

Support ignoring property only on (de)serialization #52584

Symbai opened this issue May 11, 2021 · 3 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Milestone

Comments

@Symbai
Copy link

Symbai commented May 11, 2021

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

public 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 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.

[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));
    }
}
@Symbai Symbai added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 11, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels May 11, 2021
@ghost
Copy link

ghost commented May 11, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

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

public 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 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.

[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));
    }
}
Author: Symbai
Assignees: -
Labels:

api-suggestion, area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Related to #50294 and #55781. There are many ignore conditions and permutations thereof that the current APIs do not cover. We need to think carefully about providing a general-purpose solution that covers all use cases.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Jul 21, 2021
@eiriktsarpalis eiriktsarpalis added this to the 7.0.0 milestone Jul 21, 2021
@eiriktsarpalis eiriktsarpalis added the api-needs-work API needs work before it is approved, it is NOT ready for implementation label Jul 21, 2021
@eiriktsarpalis
Copy link
Member

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.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

2 participants