none
EmitDefaultValue in DataContractJsonSerializer RRS feed

  • Question

  • I have a class I'm trying to serialise to JSON but one of the properties may or may not be populated. When it hasn't been populated (still null), I want it to be ignored within the serialisation. Reading online, the EmitDefaultValue can control whether the property is written if unchanged from default (which online seems to suggest works fine with null).

    When I add this to my code, I am seeing no change in the output, I still get <property>=null within my JSON string.

    The property of interest is the "partition" below, note that it is a property of a sub-item although would hope that doesn't make a difference, any other suggestion on how to have this ignored when not populated?

        public class refresh
        {
            [DataMember(Name = "type")]
            public String type { get; set; }
            [DataMember(Name = "objects")]
            public List<objects> objects { get; set; }
        }
    
        public class objects
        {
            [DataMember(Name = "database")]
            public String database { get; set; }
            [DataMember(Name = "table")]
            public String table { get; set; }
            [DataMember(Name = "partition", EmitDefaultValue = false)]
            public String partition { get; set; }
        }
    
        [DataContract(Name = "SSAS")]
        public class SSAS
        {
            [DataMember(Name = "refresh")]
            public refresh refresh { get; set; }
        }

    Tuesday, February 14, 2017 11:17 PM

Answers

  • Try adding ‘[DataContract]’ to the objects class.


    • Edited by Viorel_MVP Wednesday, February 15, 2017 7:49 AM
    • Marked as answer by RyanAB Wednesday, February 15, 2017 8:47 PM
    Wednesday, February 15, 2017 7:48 AM

All replies