Skip to content
Merged
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
31 changes: 6 additions & 25 deletions src/BootstrapBlazor/Converter/JsonEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,20 @@ namespace BootstrapBlazor.Core.Converter;
/// <summary>
/// JsonEnumConverter 枚举转换器
/// </summary>
public class JsonEnumConverter : JsonConverterAttribute
/// <param name="camelCase">Optional naming policy for writing enum values.</param>
/// <param name="allowIntegerValues">True to allow undefined enum values. When true, if an enum value isn't defined it will output as a number rather than a string.</param>
public class JsonEnumConverter(bool camelCase = false, bool allowIntegerValues = true) : JsonConverterAttribute
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="camelCase">Optional naming policy for writing enum values.</param>
/// <param name="allowIntegerValues">True to allow undefined enum values. When true, if an enum value isn't defined it will output as a number rather than a string.</param>
public JsonEnumConverter(bool camelCase = false, bool allowIntegerValues = true)
{
_camelCase = camelCase;
_allowIntegerValues = allowIntegerValues;
}

/// <summary>
/// naming policy for writing enum values
/// </summary>
private readonly bool _camelCase;

/// <summary>
/// True to allow undefined enum values. When true, if an enum value isn't defined it will output as a number rather than a string
/// </summary>
private readonly bool _allowIntegerValues;

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="typeToConvert"></param>
/// <returns></returns>
public override JsonConverter? CreateConverter(Type typeToConvert)
{
var converter = _camelCase
? new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, _allowIntegerValues)
: new JsonStringEnumConverter(null, _allowIntegerValues);
var converter = camelCase
? new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, allowIntegerValues)
: new JsonStringEnumConverter(null, allowIntegerValues);
return converter;
}
}