I am using Json.NET Serialize the class to JSON.
I have classes like this:
class Test1 { [JsonProperty("id")] public string ID { get; set; } [JsonProperty("label")] public string Label { get; set; } [JsonProperty("url")] public string URL { get; set; } [JsonProperty("item")] public List<Test2> Test2List { get; set; } }
I want to add the JsonIgnore() property to the Test2List property only if Test2List is null. If it's not null, then I want to include it in my json.
#1 building
An alternative solution to using the JsonProperty property:
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)] // or [JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)] // or for all properties in a class [JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
Just as Online documents Seen.
#2 building
You can see it in this link on their website (http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size.aspx). I support using [Default()] to specify the Default value
From link
public class Invoice { public string Company { get; set; } public decimal Amount { get; set; } // false is default value of bool public bool Paid { get; set; } // null is default value of nullable public DateTime? PaidDate { get; set; } // customize default values [DefaultValue(30)] public int FollowUpDays { get; set; } [DefaultValue("")] public string FollowUpEmailAddress { get; set; } } Invoice invoice = new Invoice { Company = "Acme Ltd.", Amount = 50.0m, Paid = false, FollowUpDays = 30, FollowUpEmailAddress = string.Empty, PaidDate = null }; string included = JsonConvert.SerializeObject(invoice, Formatting.Indented, new JsonSerializerSettings { }); // { // "Company": "Acme Ltd.", // "Amount": 50.0, // "Paid": false, // "PaidDate": null, // "FollowUpDays": 30, // "FollowUpEmailAddress": "" // } string ignored = JsonConvert.SerializeObject(invoice, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); // { // "Company": "Acme Ltd.", // "Amount": 50.0 // }
#3 building
Similar to @ sirthomas' answer, JSON.NET also respects DataMemberAttribute The EmitDefaultValue property of :
[DataMember(Name="property_name", EmitDefaultValue=false)]
If you have used [DataContract] and [DataMember] in model types and do not want to add JSON.NET specific properties, you may need to do so.
#4 building
You can do this to ignore all empty values in the object you are serializing, and then no empty properties will appear in JSON
JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; var myJson = JsonConvert.SerializeObject(myObject, settings);
#5 building
Adapted to the / @ amit answer of @ mybrief, but suitable for people using VB
Dim JSONOut As String = JsonConvert.SerializeObject( myContainerObject, New JsonSerializerSettings With { .NullValueHandling = NullValueHandling.Ignore } )
Please refer to: Object initializers: named and anonymous types (Visual Basic)