1

Closed

ignore EmitDefaultValue=false automatically

description

Hi,

I set EmitDefaultValue=false to my model properties, due to reduce the size when transferring from WCF to my UI project. I should ignore the datamember(EmitDefaultValue=false) attributes automatically when de-serializing an object to JSON (all the properties should be deserialized eventhough I mentioned EmitDefaultValue=false). because already I defined my models (more than 250 models) and its very hard to define the defaultvalue for all the properties. Is there any possible way to do this?

Thanks in advance
Closed Oct 3, 2013 at 11:00 AM by JamesNK
I'm happy with people using a contract resolver. Not everything can be built in.

comments

nandhu wrote Aug 23, 2013 at 8:38 AM

I resolved this issue to create new contract resolver.
public class CustomContractResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty property = base.CreateProperty(member, memberSerialization);
            property.DefaultValueHandling = null;
            property.HasMemberAttribute = false;
            return property;
        }
    }
and use it as
storeResponse.Data = JsonConvert.SerializeObject(this.Data, new JsonSerializerSettings { ContractResolver = new CustomContractResolver() });
But better to have a inbuilt functionality to handle this.

Thanks