在 .NET 中序列化高度链接的数据(自定义 JSON.NET 引用)

Serializing heavily linked data in .NET (customizing JSON.NET references)(在 .NET 中序列化高度链接的数据(自定义 JSON.NET 引用))

本文介绍了在 .NET 中序列化高度链接的数据(自定义 JSON.NET 引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想避免在序列化数据时重新发明轮子.我知道一些序列化相互链接的对象的方法,但它的范围从编写一些代码到编写大量用于序列化的代码,我想避免这种情况.一定有一些通用的解决方案.

I want to avoid reinventing the wheel when serializing data. I know some ways to serialize objects which are linked to each other, but it ranges from writing some code to writing a lot of code for serialization, and I'd like to avoid that. There must be some generic solutions.

假设我有这样的结构:

Person
    bro = new Person { name = "bro", pos = new Pos { x = 1, y = 5 } },
    sis = new Person { name = "sis", pos = new Pos { x = 2, y = 6 } },
    mom = new Person { name = "mom", pos = new Pos { x = 3, y = 7 }, 
        children = new List<Person> { bro, sis }
    },
    dad = new Person { name = "dad", pos = new Pos { x = 4, y = 8 }, 
        children = new List<Person> { bro, sis }, mate = mom
    };
mom.mate = dad;
Family family = new Family { persons = new List<Person> { mom, dad, bro, sis } };

我想将数据序列化成这样的:

I want to serialize data to something like this:

family: {
    persons: [
        { name: "bro", pos: { x: 1, y: 5 } },
        { name: "sis", pos: { x: 2, y: 6 } },
        { name: "mom", pos: { x: 3, y: 7 }, mate: "dad", children: [ "bro", "sis" ] },
        { name: "dad", pos: { x: 4, y: 8 }, mate: "mom", children: [ "bro", "sis" ] },
    ]
}

在这里,链接被序列化为名称,假设名称是唯一的.链接也可以是family.persons.0"或生成的唯一 ID 等.

Here, links are serialized as just names, with the assumption that names are unique. Links can also be "family.persons.0" or generated unique IDs or whatever.

要求:

  1. 格式必须是人类可读的,最好也是人类可写的.因此,按照优先顺序:JSON、YAML*、XML、自定义.没有二进制格式.

  1. Format must be human-readable and preferably human-writable too. So, in order of preference: JSON, YAML*, XML, custom. No binary formats.

序列化必须支持 .NET 提供的所有好东西.泛型是必须的,包括 IEnumerable<>、IDictionary<> 等类型.动态类型/无类型对象是可取的.

Serialization must support all good stuff .NET offers. Generics are a must, including types like IEnumerable<>, IDictionary<> etc. Dynamic types / untyped objects are desirable.

格式不能执行.没有 Lua、Python 等脚本之类的东西.

Format must not be executable. No Lua, Python etc. scripts and things like that.

如果生成唯一 ID,它们必须是稳定的(通过序列化-反序列化持续存在),因为文件将被放入 版本控制系统.

If unique IDs are generated, they must be stable (persist through serialization-deserialization), as files will be put into a version control system.

* 听说过 YAML,但遗憾的是,它似乎已经死了.

* Heard about YAML, but sadly, it seems to be pretty much dead.

推荐答案

使用 JSON.NET 解决了这个问题(很棒的库!).现在,首先,对象被序列化并准确引用到我想要它们的位置;其次,没有大量的$id"和$ref"字段.在我的解决方案中,对象的第一个属性用作其标识符.

Solved the problem using JSON.NET (fantastic library!). Now objects are, first, serialized and referenced exactly where I want them them to; and second, without numerous "$id" and "$ref" fields. In my solution, the first property of an object is used as its identifier.

我创建了两个 JsonConvertor(用于引用对象和引用对象):

I've created two JsonConvertors (for references to objects and for referenced objects):

interface IJsonLinkable
{
    string Id { get; }
}

class JsonRefConverter : JsonConverter
{
    public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(((IJsonLinkable)value).Id);
    }

    public override object ReadJson (JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType != JsonToken.String)
            throw new Exception("Ref value must be a string.");
        return JsonLinkedContext.GetLinkedValue(serializer, type, reader.Value.ToString());
    }

    public override bool CanConvert (Type type)
    {
        return type.IsAssignableFrom(typeof(IJsonLinkable));
    }
}

class JsonRefedConverter : JsonConverter
{
    public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }

    public override object ReadJson (JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
    {
        var jo = JObject.Load(reader);
        var value = JsonLinkedContext.GetLinkedValue(serializer, type, (string)jo.PropertyValues().First());
        serializer.Populate(jo.CreateReader(), value);
        return value;
    }

    public override bool CanConvert (Type type)
    {
        return type.IsAssignableFrom(typeof(IJsonLinkable));
    }
}

以及保存引用数据的上下文(每种类型都有一个字典,因此 ID 只需要在相同类型的对象中是唯一的):

and a context to hold references data (with a dictionary for each type, so IDs need to be unique only among objects of the same type):

class JsonLinkedContext
{
    private readonly IDictionary<Type, IDictionary<string, object>> links = new Dictionary<Type, IDictionary<string, object>>();

    public static object GetLinkedValue (JsonSerializer serializer, Type type, string reference)
    {
        var context = (JsonLinkedContext)serializer.Context.Context;
        IDictionary<string, object> links;
        if (!context.links.TryGetValue(type, out links))
            context.links[type] = links = new Dictionary<string, object>();
        object value;
        if (!links.TryGetValue(reference, out value))
            links[reference] = value = FormatterServices.GetUninitializedObject(type);
        return value;
    }
}

属性上的一些属性是必需的:

Some attributes on the properties are necessary:

[JsonObject(MemberSerialization.OptIn)]
class Family
{
    [JsonProperty(ItemConverterType = typeof(JsonRefedConverter))]
    public List<Person> persons;
}

[JsonObject(MemberSerialization.OptIn)]
class Person : IJsonLinkable
{
    [JsonProperty]
    public string name;
    [JsonProperty]
    public Pos pos;
    [JsonProperty, JsonConverter(typeof(JsonRefConverter))]
    public Person mate;
    [JsonProperty(ItemConverterType = typeof(JsonRefConverter))]
    public List<Person> children;

    string IJsonLinkable.Id { get { return name; } }
}

[JsonObject(MemberSerialization.OptIn)]
class Pos
{
    [JsonProperty]
    public int x;
    [JsonProperty]
    public int y;
}

所以,当我使用此代码进行序列化和反序列化时:

So, when I serialize and deserialize using this code:

JsonConvert.SerializeObject(family, Formatting.Indented, new JsonSerializerSettings {
    NullValueHandling = NullValueHandling.Ignore,
    Context = new StreamingContext(StreamingContextStates.All, new JsonLinkedContext()),
});

JsonConvert.DeserializeObject<Family>(File.ReadAllText(@"....DataFamily.json"), new JsonSerializerSettings {
    Context = new StreamingContext(StreamingContextStates.All, new JsonLinkedContext()),
});

我得到了这个整洁的 JSON:

I get this neat JSON:

{
  "persons": [
    {
      "name": "mom",
      "pos": {
        "x": 3,
        "y": 7
      },
      "mate": "dad",
      "children": [
        "bro",
        "sis"
      ]
    },
    {
      "name": "dad",
      "pos": {
        "x": 4,
        "y": 8
      },
      "mate": "mom",
      "children": [
        "bro",
        "sis"
      ]
    },
    {
      "name": "bro",
      "pos": {
        "x": 1,
        "y": 5
      }
    },
    {
      "name": "sis",
      "pos": {
        "x": 2,
        "y": 6
      }
    }
  ]
}

我不喜欢我的解决方案是我必须使用 JObject,即使从技术上讲它是不必要的.它可能会创建相当多的对象,因此加载会更慢.但看起来这是自定义对象转换器最广泛使用的方法.无论如何,可以用来避免这种情况的方法都是私有的.

What I don't like in my solution, is that I have to use JObject, even though technically it's unnecessary. It probably creates quite a bit of objects, so loading will be slower. But looks like this is the most widely used approach for customizing convertors of objects. Methods which could be used to avoid this are private anyway.

这篇关于在 .NET 中序列化高度链接的数据(自定义 JSON.NET 引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 .NET 中序列化高度链接的数据(自定义 JSON.NET 引用)

基础教程推荐