Closed
Description
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