Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using the <code>DataContractSerializer</code> (or, in this case, the <code>DataContractJsonSerializer</code>), you can use the <code>DataMember(EmitDefaultValue = false)]</code> decoration in your class. This way, you can set the properties which you <em>don't</em> want serialized to their default values (i.e., <code>null</code> for strings, 0 for ints and so on), and they won't be.</p> <p>If you're using the ASP.NET Web API, then you should be aware that the default JSON serializer isn't the <code>DataContractJsonSerializer</code> (DCJS), but JSON.NET instead. So unless you explicitly configure your <code>JsonMediaTypeFormatter</code> to use DCJS, you need another attribute to get the same behavior (<code>JsonProperty</code>, and its <code>DefaultValueHandling</code> property).</p> <p>The code below only serializes the two members which were assigned in this Car object, using both serializers. Notice that you can remove one of the attributes if you're only going to use one of them.</p> <pre><code>public class StackOverflow_12465285 { [DataContract] public class Car { private int savedId; private string savedYear; private string savedMake; private string savedModel; private string savedColor; [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public int Id { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Year { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Make { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Model { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Color { get; set; } [OnSerializing] void OnSerializing(StreamingContext ctx) { this.savedId = this.Id; this.savedYear = this.Year; this.savedMake = this.Make; this.savedModel = this.Model; this.savedColor = this.Color; // Logic to determine which ones to serialize, let's say I only want Id, Make; so make all others default. this.Color = default(string); this.Model = default(string); this.Year = default(string); } [OnSerialized] void OnSerialized(StreamingContext ctx) { this.Id = this.savedId; this.Year = this.savedYear; this.Make = this.savedMake; this.Model = this.savedModel; this.Color = this.savedColor; } } public static void Test() { Car car = new Car { Id = 12345, Make = "Ford", Model = "Focus", Color = "Red", Year = "2010" }; JsonSerializer js = new JsonSerializer(); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); js.Serialize(sw, car); Console.WriteLine("Using JSON.NET: {0}", sb.ToString()); MemoryStream ms = new MemoryStream(); DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(Car)); dcjs.WriteObject(ms, car); Console.WriteLine("Using DCJS: {0}", Encoding.UTF8.GetString(ms.ToArray())); } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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