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

Allow JsonIgnore on classes and structs #54269

Closed
TonyValenti opened this issue Jun 16, 2021 · 4 comments
Closed

Allow JsonIgnore on classes and structs #54269

TonyValenti opened this issue Jun 16, 2021 · 4 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

@TonyValenti
Copy link

TonyValenti commented Jun 16, 2021

Background and Motivation

Today, JsonIgnore can only be specified on properties and fields. This creates a needlessly redundant scenario when JsonIgnore should be applied to all members of a type.

For example, today one must write:

    public class Person {
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<long> Id { get; set; }

        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<string> FirstName { get; set; }

        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<string> LastName { get; set; }
        
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<int> Age { get; set; }
    }

    [JsonConverter(typeof(OptionalJsonConverter))]
    public struct Optional<T>  {
      /* ... */
    }

When it would be much nicer to be able to write:

    public class Person {
        public Optional<long> Id { get; set; }
        public Optional<string> FirstName { get; set; }
        public Optional<string> LastName { get; set; }
        public Optional<int> Age { get; set; }
    }

    [JsonConverter(typeof(OptionalJsonConverter))]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
    public struct Optional<T>  {
      /* ... */
    }

Proposed API

[JsonIgnore(Condition = ...)] should be specifiable on a class/struct to indicate that all properties/fields of that type have the specified default JsonIgnore condition.

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
    public sealed class JsonIgnoreAttribute : JsonAttribute

Usage Examples

See Above.

Alternative Designs

Today you can specify a global JsonIgnore value using JsonSerializerOptions.DefaultIgnoreCondition however, this is undesirable because it will impact other types you are (de)serializing and not just your target type.

Risks

None. Today JsonIgnore cannot be applied to classes so enabling it will not be a breaking change.

@TonyValenti TonyValenti added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 16, 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 Jun 16, 2021
@ghost
Copy link

ghost commented Jun 16, 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

Today, JsonIgnore can only be specified on properties and fields. This creates a needlessly redundant scenario when JsonIgnore should be applied to all members of a type.

For example, today one must write:

    public record Person {
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<long> Id { get; set; }

        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<string> FirstName { get; set; }

        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<string> LastName { get; set; }
        
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public Optional<int> Age { get; set; }
    }

    [JsonConverter(typeof(OptionalJsonConverter))]
    public struct Optional<T>  {
      /* ... */
    }

When it would be much nicer to be able to write:

    public record Person {
        public Optional<long> Id { get; set; }
        public Optional<string> FirstName { get; set; }
        public Optional<string> LastName { get; set; }
        public Optional<int> Age { get; set; }
    }

    [JsonConverter(typeof(OptionalJsonConverter))]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
    public struct Optional<T>  {
      /* ... */
    }

Proposed API

[JsonIgnore(Condition = ...)] should be specifiable on a class to indicate that all properties/fields of that class have the specified default JsonIgnore condition.

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = false)]
    public sealed class JsonIgnoreAttribute : JsonAttribute

Usage Examples

See Above.

Alternative Designs

Today you can specify a global JsonIgnore value using JsonSerializerOptions.DefaultIgnoreCondition however, this is undesirable because it will impact other types you are (de)serializing and not just your target type.

Risks

None. Today JsonIgnore cannot be applied to classes so enabling it will not be a breaking change.

Author: TonyValenti
Assignees: -
Labels:

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

Milestone: -

@TonyValenti TonyValenti changed the title Allow JsonIgnore on classes Allow JsonIgnore on classes and structs Jun 16, 2021
@TonyValenti
Copy link
Author

@steveharter

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Jul 16, 2021

Related to #50294 and #55781. In #55781 (comment) I sketched a potential API for customizing ignored values (and not just null/default values) on the converter level. Would such an alternative suit your use case?

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

Closing in favor of #55781 (comment)

@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