Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactor of ShouldSerialize () in class... can I use IContractResolver?
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <pre><code>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 } </code></pre> <p>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?</p> <p>I have been looking into <code>CopyConditional</code> properties with <code>IContractResolver</code> on <a 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</a> and looks like it might be possible to use <code>IContractResolver</code> for such a purpose, but I still seem to end up with lots of code that does not seem to re-factor out</p> <pre><code>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) &amp;&amp; property.PropertyName == "ABS") { property.ShouldSerialize = instance =&gt; { Features e = (Features)instance; return e.ABS != false; }; } if (property.DeclaringType == typeof(Features) &amp;&amp; property.PropertyName == "Immobiliser") { property.ShouldSerialize = instance =&gt; { Features e = (Features)instance; return e.Immobiliser != false; }; } return property; } } </code></pre> <p>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</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload