不允许系列化,让反序列化的属性 [英] Disallow serialization, allow deserialization on property

查看:310
本文介绍了不允许系列化,让反序列化的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的C#类:

I have a very simple C# class:

[Serializable]
[JsonObject]
public class ObjectBase {
    public string Id { get; set; }
    public string CreatedById { get; set; }
    public DateTime CreatedDate { get; set; }
    public string LastModifiedById { get; set; }
    public DateTime LastModifiedDate { get; set; }
}

属性编号 CreatedDate LastModifiedDate 是我不希望被序列化值(我与第三方的API集,而这些价值观不能被更新)。

The properties Id, CreatedDate and LastModifiedDate are values that I don't want to be serialized (I'm integrating with a third party API, and those values can never be updated).

不过,我想这些属性与数据进行填充时,我有JSON说我反序列化。

However, I would like those properties to be populated with data when I have JSON that I'm deserializing.

我试图用 [JsonIgnore] ,但导致性反序列化过程中被跳过。是否有可能以这种方式使用属性?

I tried using [JsonIgnore], but that results in the property being skipped during deserialization. Is it possible to use the properties in this manner?

修改

我使用的继承已为我所有的对象都需要相同的基本属性。我的总是的德/序列化到子类(如帐户):

I am using inheritance already as all of my objects require the same base properties. I always de/serialize into the child class (e.g. Account):

[Serializable]
[JsonObject]
public class Account : ObjectBase {
    public string AccountNumber { get; set; }
    public string ParentId { get; set; }
}

作为一个例子,我可能具有帐户的一个实例对象和编号 CreatedDate 属性可以有一个值。当我的对象序列化到JSON,我不希望被列入这些属性。但是,当我有JSON和我反序列化,我想这些属性得到一个值。

As an example, I might have an instance of the Account object and the Id and CreatedDate properties could have a value. When I serialize that object into JSON, I don't want those properties to be included. However, when I have JSON and am deserializing, I want those properties to get a value.

推荐答案

而无需修改您的类结构排除系列化某些属性的一种方法是创建一个自定义 ContractResolver 是这样的:

One way to exclude certain properties from serialization without having to modify your class structure is to create a custom ContractResolver like this:

class CustomResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
        if (typeof(ObjectBase).IsAssignableFrom(type))
        {
            string[] excludeThese = new string[] { "Id", "CreatedDate", "LastModifiedDate" };
            props = props.Where(p => !excludeThese.Contains(p.PropertyName)).ToList();
        }
        return props;
    }
}

在序列化,只是解析器添加到串行器设置:

When you serialize, just add the resolver to the serializer settings:

Account account = new Account
{
    Id = "100",
    CreatedById = "2",
    CreatedDate = new DateTime(2014, 3, 12, 14, 52, 18, DateTimeKind.Utc),
    LastModifiedById = "3",
    LastModifiedDate = new DateTime(2014, 3, 17, 16, 3, 34, DateTimeKind.Utc),
    AccountNumber = "1234567",
    ParentId = "99"
};

JsonSerializerSettings settings = new JsonSerializerSettings()
{
    ContractResolver = new CustomResolver(),
    Formatting = Formatting.Indented
};

string json = JsonConvert.SerializeObject(account, settings);
Console.WriteLine(json);

输出:

{
  "AccountNumber": "1234567",
  "ParentId": "99",
  "CreatedById": "2",
  "LastModifiedById": "3"
}

另一种方法

如果你不喜欢的解析器方法,另一种选择是添加布尔 ShouldSerializeX 方法类,其中 X 替换为您要排除的属性的名称:

If you don't like the resolver approach, another alternative is to add boolean ShouldSerializeX methods to your class where X is replaced with names of the properties that you want to exclude:

[Serializable]
[JsonObject]
public class ObjectBase
{
    public string Id { get; set; }
    public string CreatedById { get; set; }
    public DateTime CreatedDate { get; set; }
    public string LastModifiedById { get; set; }
    public DateTime LastModifiedDate { get; set; }

    public bool ShouldSerializeId()
    {
        return false;
    }

    public bool ShouldSerializeCreatedDate()
    {
        return false;
    }

    public bool ShouldSerializeLastModifiedDate()
    {
        return false;
    }
}

这篇关于不允许系列化,让反序列化的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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