Skip to content

[API Proposal]: JsonSourceGenerationOptions should support ReferenceHandler #107597

Closed
@petro2050

Description

@petro2050

Background and motivation

Provide a way to specify a reference handler using compile-time configuration.

API Proposal

The ReferenceHandler type is a class and therefore it cannot be specified in literal annotations. If we wanted to add support for that it would require introducing a proxy enum type, in the style of JsonKnownEnumPolicy. However that would leave out support for user-defined reference handlers in case that matters:

namespace System.Text.Json.Serialization;

public enum KnownReferenceHandler // Naming follows the ReferenceHandler type which misses a Json prefix
{ 
     IgnoreCycles = 1,
     Preserve = 2,
     Unspecified = 0,
}

public partial class JsonSourceGenerationOptionsAttribute
{
    public JsonKnownReferenceHandler ReferenceHandler { get; set; }
}

API Usage

[JsonSourceGenerationOptions(WriteIndented = true, ReferenceHandler = KnownReferenceHandler.IgnoreCycles)]
[JsonSerializable(typeof(WeatherForecast))]
partial class SourceGenerationContext : JsonSerializerContext;

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsonhelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions