Skip to content

[System.Text.Json] Support constructor parameters in JsonConverter attribute #54187

@RamjotSingh

Description

@RamjotSingh

Background and Motivation

JsonConverter attribute allows customizing the json serialization and deserialization process. However, today there is no way to specify constructor arguments that the converter themselves take.

Best example of this is JsonStringEnumConverter which has following constructor

public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true)

It is pretty common to specify that you want enum members to be camel cased. However, using JsonConveter attribute it is not possible to do this, unless you create another converter which sets that by default.

Newtonsoft.Json supports the following attribute constructor allowing parameters to be specified.

public JsonConverterAttribute(Type converterType, params object[] converterParameters)

Proposed API

public JsonConverterAttribute(Type converterType, params object[] converterParameters)

Usage Examples

[JsonConverter(typeof(JsonStringEnumConverter), JsonNamingPolicy.CamelCase, true)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.JsonuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions