Skip to content

[System.Text.Json] JsonConverter lacks ability to specify converter for items in a collection #54189

Open
@RamjotSingh

Description

@RamjotSingh

JsonConverter allows specifying the conveter to be used for a specific property, however if a collection is in use and you want to use the converter to serialize a items in collection it doesn't work.

Example

public enum Day
{
   Sunday,
   Monday,
}

public class Model
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public IEnumerable<Day> Days { get; set; }
}

The above code will throw. The only way to work around is to write a custom converter which understands collections and uses a different converter for each item in collection.

I propose a new attribute which allows the caller to specify converter to use for items in collection should be added. Something like

[JsonCollectionItemConverter(typeof(JsonStringEnumConverter))]

Newtonsoft.Json allows this by allowing specifying the type in JsonProperty attribute. That option has it's own shortcomings as well. I believe the above option will solve that too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Team:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updated

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions