仅序列化类的某些特定属性? [英] Only serialize some specific properties of a class?

查看:59
本文介绍了仅序列化类的某些特定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Json.Net的问题.

I ran into a problem with Json.Net.

我的应用程序已连接到数据库并在其中存储了一些数据. 假设我的应用程序具有三个不同的类:实体,组,组织

My application is connected to a database and stores some data in it. Lets assume my application has three different classes: Entity, Group, Organisation

近距离查看Entity类:

Short look into Entity class:

public class Entity
{
    [JsonIgnore]
    public int ID { get; private set; }
    [JsonProperty]
    public string UID { get; private set; }
    [JsonProperty]
    public Gender EntityGender { get; set; }
    [JsonProperty]
    public string Surname { get; set; }
    [JsonProperty]
    public string Forename { get; set; }
    [JsonProperty]
    public Group EntityGroup { get; set; }
    [JsonProperty]
    public Organisation EntityOrganisation { get; set; }

组织和组也具有ID属性.
序列化实体时,我不想完全序列化EntityGroup和EntityOrganisation,而只序列化其ID.

Organisation and Group also have an ID property.
When serializing an Entity I do not want to fully serialize EntityGroup and EntityOrganisation but only their IDs.

一些简单的例子,我希望它看起来像:

Some quick example how I would like it to look like:

(现在的样子)

{
  "UID": "6c5204356b3a1a33",
  "Surname": "Bar",
  "Forename": "Foo",
  "EntityGroup": {
    "Name": "XGroup",
    "GroupOrganisation": {
      "Name": "FooOrg"
    }
  },
  "EntityOrganisation": null,
  "EntityStation": null
}

(外观如何)

{
  "UID": "6c5204356b3a1a33",
  "Surname": "Bar",
  "Forename": "Foo",
  "EntityGroup": {
    "ID": 1,
    "GroupOrganisation": {
      "ID": 1
    }
  },
  "EntityOrganisation": null,
  "EntityStation": null
}

是否可以设置仅在序列化实体时序列化Group和Organization的ID属性的JsonParser?

Is it possible to setup a JsonParser that only serializes the ID property of Group and Organisation when serializing an Entity?

推荐答案

这是怎么回事:

在您的Entity类中,将JsonConverter属性添加到EntityGroup类中:

In your Entity class, add the JsonConverter attribute to the EntityGroup class:

[JsonProperty]
[JsonConverter(typeof(EntityGroupConverter))]
public Group EntityGroup { get; set; }

这将使用EntityGroupConverter来序列化Group.此类如下:

This will use the EntityGroupConverter to serialize the Group. This class is as follows:

class EntityGroupConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Group);
    }

    public override object ReadJson(JsonReader reader, Type objectType,
        JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value,
        JsonSerializer serializer)
    {
        Group group = value as Group;
        serializer.Serialize(writer, new { ID = group.ID,
            GroupOrganisation = new { ID = group.Organisation.ID } });
    }
}

此处的关键是使用serializer编写仅包含所需属性的匿名对象.

The key here is using the serializer to write an anonymous object that includes only the desired properties.

请注意,ReadJson未实现-我假设在此阶段不需要反序列化.

Note that ReadJson is left unimplemented - I am assuming that deserialization is not required at this stage.

修改

正如Skadier所指出的,在最新版本的Json.NET(此问题中使用的版本为3.5.0.0)中,ReadJson的签名将如下所示:

As Skadier pointed out, the signature of the ReadJson will look like this in more recent versions of Json.NET (the version used in this question is 3.5.0.0):

public override object ReadJson(JsonReader reader, Type objectType,
        object existingValue, JsonSerializer serializer)

这篇关于仅序列化类的某些特定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆