使用.NET Core System.Text.Json序列化/反序列化类层次结构

问题描述 投票:1回答:1

我有一个简单的类层次结构,我想使用System.Text.Json进行序列化。

共有3个班级。底数是Shape。继承的是BoxCircle

我计划在前端应用程序上将这些类用作标记的联合,因此我刚刚引入了一个鉴别属性Tag

我写了一个类型转换器,支持此层次结构的序列化/反序列化。

我想理解的是,这是否是实现此类功能的最佳方法。确实,序列化的输出结果很丑陋(我在下面的示例中做了评论)。无论如何,我不确定它是否已达到最佳状态。

这是我实现序列化/反序列化的示例:

using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Serialization.Theory
{
    public abstract class Shape
    {
        public abstract String Tag { get; }
    }

    public class Box : Shape
    {
        public override String Tag { get; } = nameof(Box);

        public Single Width { get; set; }

        public Single Height { get; set; }

        public override String ToString()
        {
            return $"{Tag}: Width={Width}, Height={Height}";
        }
    }

    public class Circle : Shape
    {
        public override String Tag { get; } = nameof(Circle);

        public Single Radius { get; set; }

        public override String ToString()
        {
            return $"{Tag}: Radius={Radius}";
        }
    }

    public class ShapeConverter : JsonConverter<Shape>
    {
        public override Boolean CanConvert(Type typeToConvert)
        {
            return typeToConvert == typeof(Circle) || typeToConvert == typeof(Shape);
        }

        public override Shape Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var raw = reader.GetString();
            var doc = JsonDocument.Parse(raw);
            var prop = doc.RootElement.EnumerateObject().Where(x => x.Name == "Tag").First();
            var value = prop.Value.GetString();

            switch (value)
            {
                case nameof(Circle): 
                    return JsonSerializer.Deserialize<Circle>(raw);
                case nameof(Box):
                    return JsonSerializer.Deserialize<Box>(raw);
                default:
                    throw new NotSupportedException();
            }
        }

        public override void Write(Utf8JsonWriter writer, Shape value, JsonSerializerOptions options)
        {
            if (value is Circle circle)
            {
                writer.WriteStringValue(JsonSerializer.SerializeToUtf8Bytes(circle));
            }
            else if (value is Box box)
            {
                writer.WriteStringValue(JsonSerializer.SerializeToUtf8Bytes(box));
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Keep in base class references like it's a property on another object.
            Shape origin1 = new Box { Width = 10, Height = 20 };
            Shape origin2 = new Circle { Radius = 30 };

            var settings = new JsonSerializerOptions();
            settings.Converters.Add(new ShapeConverter());

            var raw1 = JsonSerializer.Serialize(origin1, settings);
            var raw2 = JsonSerializer.Serialize(origin2, settings);

            Console.WriteLine(raw1); // "{\u0022Tag\u0022:\u0022Box\u0022,\u0022Width\u0022:10,\u0022Height\u0022:20}"
            Console.WriteLine(raw2); // "{\u0022Tag\u0022:\u0022Circle\u0022,\u0022Radius\u0022:30}"

            var restored1 = JsonSerializer.Deserialize<Shape>(raw1, settings);
            var restored2 = JsonSerializer.Deserialize<Shape>(raw2, settings);

            Console.WriteLine(restored1); // Box: Width=10, Height=20
            Console.WriteLine(restored2); // Circle: Radius=30
        }
    }
}

c# json serialization deserialization .net-core-3.0
1个回答
0
投票

我也需要答案。似乎another article在讨论它,但没有给出很好的答案。另请参见this discussion

© www.soinside.com 2019 - 2024. All rights reserved.