ASP.NETCore7没有返回多态序列化鉴别器
创始人
2024-09-17 17:01:00
0
  1. 在需要进行序列化和反序列化的类中,添加JsonConverterAttribute,指定JsonConverter类型为JsonSubtypes。例如:
[JsonConverter(typeof(JsonSubtypes))]
public abstract class Shape
{
    public int Id { get; set; }
    public abstract double Area { get; }
}

public class Circle : Shape
{
    public double Radius { get; set; }
    public override double Area => Math.PI * Radius * Radius;
}

public class Square : Shape
{
    public double SideLength { get; set; }
    public override double Area => SideLength * SideLength;
}
  1. 在JsonSubtypes中添加子类型和对应的类型鉴别器值。例如:
public class JsonSubtypes : JsonConverter
{
    private readonly Type[] _subtypes;
    private readonly string _discriminatorProperty;

    public JsonSubtypes()
    {
        _subtypes = new[]
        {
            typeof(Circle),
            typeof(Square),
        };
        _discriminatorProperty = "shapeType";
    }

    public override void WriteJson(JsonWriter writer, Shape value, JsonSerializer serializer)
    {
        writer.WriteStartObject();
        writer.WritePropertyName(_discriminatorProperty);
        writer.WriteValue(GetDiscriminatorValue(value));
        writer.WritePropertyName("id");
        writer.WriteValue(value.Id);
        writer.WritePropertyName("area");
        writer.WriteValue(value.Area);
        writer.WriteEndObject();
    }

    public override Shape ReadJson(JsonReader reader, Type objectType, Shape existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        JObject jObject = JObject.Load(reader);
        JToken discriminatorValue = jObject.GetValue(_discriminatorProperty, StringComparison.OrdinalIgnoreCase);
        Type type = _subtypes.FirstOrDefault(t => GetDiscriminatorValue((Shape)Activator.CreateInstance(t)) == discriminatorValue.ToString());
        if (type == null)
        {
            throw new InvalidOperationException($"Unknown {nameof(Shape)} type: {discriminatorValue}");
        }
        return (Shape)jObject.ToObject(type);
    }

    private string GetDiscriminatorValue(Shape shape)
    {
        return shape switch
        {
            Circle _ => "circle",
            Square _ => "square",
            _ => throw new InvalidOperationException($"Unknown {nameof(Shape)} type: {shape.GetType().Name}"),
        };
    }
}
  1. 在需要序列化的对象上使用JsonSerializerSettings,指定Converters为一个JsonConverter的列表,将JsonSubtypes添加到列表中。例如:
var circle = new Circle { Id = 1, Radius = 5 };
var square = new Square { Id = 2, SideLength = 10

相关内容

热门资讯

科普攻略!德州真人透视脚本,h... 科普攻略!德州真人透视脚本,hhpoker可以控制吗,果然有挂(有挂教学)1、实时透视辅助更新:用户...
专业讨论!来玩app 德州 辅... 专业讨论!来玩app 德州 辅助,wepoker新号好一点吗,一直真的有挂(有挂头条)1)免费钻石:...
一分钟快速了解!aapoker... 一分钟快速了解!aapoker透视方法,wejoker透视方法,真是是有挂(有挂教程);1、超多福利...
解密关于!wepoker有人用... 解密关于!wepoker有人用过吗,wepoker怎么开辅助,一贯存在有挂(今日头条)1、首先打开辅...
分享开挂内幕!wepoker新... 分享开挂内幕!wepoker新号好一点吗,德普之星辅助工具如何打开,一贯真的是有挂(有挂详细)1)辅...
终于知道!fishpoker透... 终于知道!fishpoker透视,德扑圈透视,竟然真的有挂(有挂功能)1、完成辅助器v3.3的残局,...
大神推荐!wepoker有没有... 大神推荐!wepoker有没有透视方法,wepoker一直输的号能继续打吗,一贯真的是有挂(有挂总结...
玩家必看科普!aapoker安... 玩家必看科普!aapoker安装包怎么使用,hhpoker软件可以玩吗,确实存在有挂(有挂攻略)1、...
我来向大家传授!德普之星透视,... 我来向大家传授!德普之星透视,wepoker钻石怎么看底牌,都是是真的有挂(有挂方法)进入游戏-大厅...
热点推荐!wepoker有没有... 热点推荐!wepoker有没有插件,hhpoker德州挂真的有吗,切实是有挂(果真有挂)运辅助工具,...