Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unseal StringEnumConverter #54917

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,12 @@ public enum JsonSourceGenerationMode
Metadata = 1,
Serialization = 2,
}
public sealed partial class JsonStringEnumConverter : System.Text.Json.Serialization.JsonConverterFactory
public partial class JsonStringEnumConverter : System.Text.Json.Serialization.JsonConverterFactory
{
public JsonStringEnumConverter() { }
public JsonStringEnumConverter(System.Text.Json.JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true) { }
public override bool CanConvert(System.Type typeToConvert) { throw null; }
public override System.Text.Json.Serialization.JsonConverter CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
public sealed override bool CanConvert(System.Type typeToConvert) { throw null; }
public sealed override System.Text.Json.Serialization.JsonConverter CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
}
public enum JsonUnknownTypeHandling
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Text.Json.Serialization
/// <remarks>
/// Reading is case insensitive, writing can be customized via a <see cref="JsonNamingPolicy" />.
/// </remarks>
public sealed class JsonStringEnumConverter : JsonConverterFactory
public class JsonStringEnumConverter : JsonConverterFactory
{
private readonly JsonNamingPolicy? _namingPolicy;
private readonly EnumConverterOptions _converterOptions;
Expand Down Expand Up @@ -45,13 +45,13 @@ public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allow
}

/// <inheritdoc />
public override bool CanConvert(Type typeToConvert)
public sealed override bool CanConvert(Type typeToConvert)
{
return typeToConvert.IsEnum;
}

/// <inheritdoc />
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
public sealed override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
EnumConverterFactory.Create(typeToConvert, _converterOptions, _namingPolicy, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,15 @@ public class Week
[JsonConverter(typeof(JsonStringEnumConverter))]
public DayOfWeek WorkStart { get; set; }
public DayOfWeek WorkEnd { get; set; }
[LowerCaseEnum]
[JsonConverter(typeof(LowerCaseEnumConverter))]
public DayOfWeek WeekEnd { get; set; }
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false)]
private class LowerCaseEnumAttribute : JsonConverterAttribute
private class LowerCaseEnumConverter : JsonStringEnumConverter
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
public LowerCaseEnumAttribute() { }

public override JsonConverter CreateConverter(Type typeToConvert)
=> new JsonStringEnumConverter(new ToLowerNamingPolicy());
public LowerCaseEnumConverter() : base(new ToLowerNamingPolicy())
{
}
}

[Fact]
Expand Down