FunGame-Core/Library/Common/Architecture/BaseEntityConverter.cs
milimoe 9631267010
为ReadPropertyName方法添加JsonSerializerOptions (#44)
* 为ReadPropertyName方法添加JsonSerializerOptions

* 添加reader.read();

---------

Co-authored-by: Yezi <53083103+yeziuku@users.noreply.github.com>
2023-07-25 09:16:23 +08:00

31 lines
1.0 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Milimoe.FunGame.Core.Interface.Base;
namespace Milimoe.FunGame.Core.Library.Common.Architecture
{
public abstract class BaseEntityConverter<T> : JsonConverter<T>, IEntityConverter<T>
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
T? result = default;
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject) break;
if (reader.TokenType == JsonTokenType.PropertyName)
{
string propertyName = reader.GetString() ?? "";
reader.Read();
ReadPropertyName(ref reader, propertyName, options, ref result);
}
}
return result;
}
public abstract void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref T? result);
}
}