3213
This commit is contained in:
parent
7d86f736d9
commit
1e9144b144
|
|
@ -0,0 +1,44 @@
|
|||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace HoneyBox.Model.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// JSON转换器:将字符串或空值转换为int
|
||||
/// </summary>
|
||||
public class StringToIntConverter : JsonConverter<int>
|
||||
{
|
||||
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
var stringValue = reader.GetString();
|
||||
if (string.IsNullOrEmpty(stringValue))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (int.TryParse(stringValue, out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
return reader.GetInt32();
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteNumberValue(value);
|
||||
}
|
||||
}
|
||||
|
|
@ -550,6 +550,7 @@ public class WelfareBuyRequest
|
|||
/// 商品ID
|
||||
/// </summary>
|
||||
[System.Text.Json.Serialization.JsonPropertyName("goods_id")]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(HoneyBox.Model.Converters.StringToIntConverter))]
|
||||
public int GoodsId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -580,6 +581,7 @@ public class WelfareBuyRequest
|
|||
/// 优惠券ID
|
||||
/// </summary>
|
||||
[System.Text.Json.Serialization.JsonPropertyName("coupon_id")]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(HoneyBox.Model.Converters.StringToIntConverter))]
|
||||
public int CouponId { get; set; }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user