ShouldSerialize()类的重构......我可以使用IContractResolver? [英] Refactor of ShouldSerialize () in class... can I use IContractResolver?

查看:147
本文介绍了ShouldSerialize()类的重构......我可以使用IContractResolver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回的车特点的大名单的API ....所有要么是布尔或整数......基本上我只是想显示返回真值或为整数> 0的人。

I have an API that returns a big list of car features.... all are either bool or ints... and basically I only want to display the ones that return true values or >0 for the ints.

我使用JSON.net让我SAN使用ShouldSerialize()属性来确定我是否应该基于其价值序列的财产,我的code是这样的:

I am using JSON.net so that I san use the ShouldSerialize() property to determine if I should serialize the property based upon its value and my code looks like this:

public class Features
{
    public bool ABS { get; set; }

    public bool ShouldSerializeABS()
    {
        // don't serialize the ABS property if ABS is false
        return (ABS != false);
    }


    public bool Immobiliser { get; set; }

    public bool ShouldSerializeImmobiliser ()
    {
        // don't serialize the Immobiliser property if Immobiliser is false
        return (Immobiliser != false);
    }

    public int BHP { get; set; }

    public bool ShouldSerializeBHP  ()
    {
        // don't serialize the BHP property if BHP is false
        return (BHP != 0);
    }
    //..... etc
}

这个伟大的工程,给我结果我之后,但我只是想知道如果有一种方法来重新考虑此因素,使我的类没有变得混乱与所有ShouldSerialize()属性?

This works great and gives me the results I am after, however I was just wondering if there is a way to re-factor this so that my class does not become cluttered with all the ShouldSerialize() properties?

我已在<一个寻找到 CopyConditional 性能与 IContractResolver href=\"http://james.newtonking.com/projects/json/help/index.html?topic=html/ConditionalProperties.htm\" rel=\"nofollow\">http://james.newtonking.com/projects/json/help/index.html?topic=html/ConditionalProperties.htm看起来像有可能使用 IContractResolver 这样的目的,但是我似乎仍然有很多code的似乎并不重因素落得出

I have been looking into CopyConditional properties with IContractResolver on http://james.newtonking.com/projects/json/help/index.html?topic=html/ConditionalProperties.htm and looks like it might be possible to use IContractResolver for such a purpose, but I still seem to end up with lots of code that does not seem to re-factor out

public class ShouldSerializeContractResolver : DefaultContractResolver
{
   public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();

   protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
   {
     JsonProperty property = base.CreateProperty(member, memberSerialization);

     if (property.DeclaringType == typeof(Features) && property.PropertyName == "ABS")
     {
         property.ShouldSerialize =
           instance =>
           {
               Features e = (Features)instance;
               return e.ABS != false;
           };
     }
     if (property.DeclaringType == typeof(Features) && property.PropertyName == "Immobiliser")
     {
         property.ShouldSerialize =
           instance =>
           {
               Features e = (Features)instance;
               return e.Immobiliser != false;
           };
     }

    return property;
  }
}

和使用ShouldSerializeContractResolver似乎并没有从类中删除属性,如果它是假的这种方法...任何帮助是极大的AP preciated

and this method using the ShouldSerializeContractResolver does not seem to remove the property from the class if it is false... any help is greatly appreciated

推荐答案

这听起来像你正试图通过编写所有这些ShouldSerialize()方法可以通过只是改变了 DefaultValueHandling 的序列化ignore设置。这将导致等于其默认值(假为BOOL,0 INT)不被序列化的任何值。

It sounds like what you are trying to accomplish by writing all these ShouldSerialize() methods can be accomplished by just changing the DefaultValueHandling setting on the serializer to Ignore. This will cause any values that are equal to their default values (false for bool, 0 for int) not to be serialized.

JsonSerializerSettings jsonSettings = new JsonSerializerSettings();
jsonSettings.DefaultValueHandling = DefaultValueHandling.Ignore;

string json = JsonConvert.SerializeObject(yourObject, jsonSettings);

如果您正在使用Web API,那么你可以通过 WebApiConfig <的注册方法访问Json.NET序列化的设置/ code>类(在App_Start文件夹)。

If you're using Web API, then you can access the settings of the Json.NET serializer via the Register method of the WebApiConfig class (in the App_Start folder).

JsonSerializerSettings settings = config.Formatters.JsonFormatter.SerializerSettings;
settings.DefaultValueHandling = DefaultValueHandling.Ignore;

这篇关于ShouldSerialize()类的重构......我可以使用IContractResolver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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