我可以选择关闭在运行时JsonIgnore属性? [英] Can I optionally turn off the JsonIgnore attribute at runtime?

查看:300
本文介绍了我可以选择关闭在运行时JsonIgnore属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建从一组类与Newtonsoft.Json一个JSON文件。创建该文件是非常大的,所以我创建 JsonProperty 的的性能,以减少大小,并添加 JsonIgnore 以及自定义格式的一些数据类型

I am creating a JSON file with Newtonsoft.Json from a set of classes. The file created is very large, so I have created JsonProperty's for the properties to reduce the size and added JsonIgnore and custom formatting for some datatypes.

结果是从24MB到1MB,这是伟大的减少;不过,我想,以产生完整的版本,或在运行时减少财产版本的选项。

The result is a reduction from 24MB to 1MB, which is great; however, I'd like the option to produce either the full version or the reduced property version at runtime.

有反正我能得到的序列化选择使用属性?

Is there anyway I can get the serializer to optionally use the attributes?

推荐答案

是的,这可以使用自定义来完成 ContractResolver

Yes, this can be done using a custom ContractResolver.

您没有表现出任何代码,所以我就补一个例子。比方说,我有一个类,如下图所示。我想在编号中的序列化输出名称属性,但是我绝对不感兴趣的 AlternateName 颜色。我已标记那些 [JsonIgnore] 。我想说明出现,但有时也会带来长,所以我用一个自定义的 JsonConverter 来限制它的长度。我也想用一个较短的属性名称的描述,所以我用 [JsonProperty(说明)]

You didn't show any code, so I'll just make up an example. Let's say I have a class Foo as shown below. I want the Id and Name properties in the serialization output, but I'm definitely not interested in the AlternateName and Color. I've marked those with [JsonIgnore]. I want the description to appear, but sometimes this can get really long, so I've used a custom JsonConverter to limit its length. I also want to use a shorter property name for the description, so I've marked it with [JsonProperty("Desc")].

class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
    [JsonIgnore]
    public string AlternateName { get; set; }
    [JsonProperty("Desc")]
    [JsonConverter(typeof(StringTruncatingConverter))]
    public string Description { get; set; }
    [JsonIgnore]
    public string Color { get; set; }
}

当我序列上面的实例...

When I serialize an instance of the above...

Foo foo = new Foo
{
    Id = 1,
    Name = "Thing 1",
    AlternateName = "The First Thing",
    Description = "This is some lengthy text describing Thing 1 which you'll no doubt find very interesting and useful.",
    Color = "Yellow"
};

string json = JsonConvert.SerializeObject(foo, Formatting.Indented);



...我得到这样的输出:

...I get this output:

{
  "Id": 1,
  "Name": "Thing 1",
  "Desc": "This is some lengthy text describing Thing 1 "
}

现在,让我们说,我有时要得到完整的JSON输出,无视我的自定义设置。我可以使用自定义 ContractResolver 来编程不应用从类的属性。下面是解析器的代码:

Now, let's say that I sometimes want to get the full JSON output, ignoring my customizations. I can use a custom ContractResolver to programmatically "unapply" the attributes from the class. Here's the code for the resolver:

class IgnoreJsonAttributesResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
        foreach (var prop in props)
        {
            prop.Ignored = false;   // Ignore [JsonIgnore]
            prop.Converter = null;  // Ignore [JsonConverter]
            prop.PropertyName = prop.UnderlyingName;  // restore original property name
        }
        return props;
    }
}

要使用的解析器,我将它添加到 JsonSerializerSettings 并通过设置这样的串行:

To use the resolver, I add it to the JsonSerializerSettings and pass the settings to the serializer like this:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new IgnoreJsonAttributesResolver();
settings.Formatting = Formatting.Indented;

string json = JsonConvert.SerializeObject(foo, settings);

输出现在包括忽略的属性,和描述是不再被截断:

The output now includes the ignored properties, and the description is no longer truncated:

{
  "Id": 1,
  "Name": "Thing 1",
  "AlternateName": "The First Thing",
  "Description": "This is some lengthy text describing Thing 1 which you'll no doubt find very interesting and useful.",
  "Color": "Yellow"
}

完整的示例在这里:的 https://dotnetfiddle.net/WZpeWt

Full demo here: https://dotnetfiddle.net/WZpeWt

这篇关于我可以选择关闭在运行时JsonIgnore属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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