Skip to content

It should be possible to specify JsonIgnoreCondition's on the type level #108231

@eiriktsarpalis

Description

@eiriktsarpalis

Motivation

There's a substantial amount of duplication in the following code:

class MyPoco
{
    [JsonIgnore(JsonIgnoreCondition.WhenWritingNull)]
    public string? PropA { get; set; }

    [JsonIgnore(JsonIgnoreCondition.WhenWritingNull)]
    public string? PropB { get; set; }

    [JsonIgnore(JsonIgnoreCondition.WhenWritingNull)]
    public string? PropC { get; set; }
}

that could be eliminated using a type-level declaration:

[JsonIgnore(JsonIgnoreCondition.WhenWritingNull)]
class MyPoco
{
    public string? PropA { get; set; }

    public string? PropB { get; set; }

    public string? PropC { get; set; }
}

API Proposal

namespace System.Text.Json.Serialization;

-[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public partial class JsonIgnoreAttribute;

cc @stephentoub

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions