I have a custom class that looks like this:
public class PartnerLoginOptions
{
public string Username { get; set; }
public string Password { get; set; }
public string DeviceModel { get; set; }
public string Version { get; set; }
public bool? IncludeUrls { get; set; }
public bool? ReturnDeviceType { get; set; }
public bool? ReturnUpdatePromptVersions { get; set; }
}
I'd like to ignore any bool?
members with default values when serializing, but keep strings with null values. For example, if I had an object like this
var options = new PartnerLoginOptions
{
Username = null,
Password = "123",
IncludeUrls = null,
ReturnDeviceType = true
};
Then serializing would result in this:
{
"username": null,
"password": "123",
"deviceModel": null,
"version": null,
"returnDeviceType": true
}
Here is the code I have so far:
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Ignore // this applies to strings too, not just bool?
};
return JsonConvert.SerializeObject(options, settings);
Is there any way to do this without individually tagging each OptionalBool
property with [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
? Thanks.