Files
WMS/src/WalkingTec.Mvvm.Core/Json/NullableConverter.cs
T
2023-08-11 15:55:50 +08:00

38 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace WalkingTec.Mvvm.Core.Json
{
public class NullableConverter<T> : JsonConverter<Nullable<T>> where T : struct
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (string.IsNullOrEmpty(reader.GetString()) || string.IsNullOrWhiteSpace(reader.GetString()))
{
return null;
}
else
{
return JsonSerializer.Deserialize<T>(ref reader, options);
}
}
return JsonSerializer.Deserialize<T>(ref reader, options);
}
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value!.Value, options);
}
}
}