Skip to content
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
2 changes: 1 addition & 1 deletion src/DFApp.Web/DTOs/Lottery/PrizegradesItemDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class PrizegradesItemDto
{

[JsonPropertyName("type")]
[JsonConverter(typeof(IntToStringConverter))]
[JsonConverter(typeof(FlexibleStringConverter))]
public string? Type { get; set; }

[JsonPropertyName("typenum")]
Expand Down
28 changes: 28 additions & 0 deletions src/DFApp.Web/Infrastructure/FlexibleStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DFApp.Web.Infrastructure
{
/// <summary>
/// 灵活的字符串转换器,能将 JSON 中的数字和字符串都转换为 C# string
/// </summary>
public class FlexibleStringConverter : JsonConverter<string?>
{
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
return reader.GetString();
if (reader.TokenType == JsonTokenType.Number)
return reader.GetInt32().ToString();
if (reader.TokenType == JsonTokenType.Null)
return null;
throw new JsonException($"意外的 JSON 令牌类型: {reader.TokenType},期望字符串、数字或null。");
}

public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}
}
Loading