我有一个如下所示的自定义类:
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; }
}
我希望在序列化时忽略任何具有默认值的bool?
成员,但保留字符串为空值。例如,如果我有这样的对象
var options = new PartnerLoginOptions
{
Username = null,
Password = "123",
IncludeUrls = null,
ReturnDeviceType = true
};
然后序列化会导致:
{
"username": null,
"password": "123",
"deviceModel": null,
"version": null,
"returnDeviceType": true
}
这是我到目前为止的代码:
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Ignore // this applies to strings too, not just bool?
};
return JsonConvert.SerializeObject(options, settings);
如果没有使用OptionalBool
单独标记每个[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
属性,有没有办法做到这一点?感谢。
答案 0 :(得分:2)
您可以创建一个继承自custom contract resolver的DefaultContractResolver
,将cannot use type []*Concrete as type []*Type in assignment
应用于所需类型的所有属性。但是,您的具体问题有一些限制:
您希望将默认值处理应用于系统类型,即DefaultValueHandling.Ignore
。因此,无法通过将某个自定义属性应用于该类型,然后在CreateProperty()
中检查它来实现合同解析程序。相反,有必要将一组覆盖传递给合同解析器的构造函数。
您正在使用CamelCasePropertyNamesContractResolver
。如果您将其子类化并将不同的覆盖列表传递给不同的实例,您将遇到来自this question的错误,即在每个子类型的所有实例中强制共享契约,即使不同的实例否则会返回不同的契约。因此,有必要从没有此错误的bool?
继承。
因此,您的合约解决方案应如下所示:
DefaultContractResolver
然后使用它:
public class DefaultValueCamelCaseContractResolver : DefaultValueContractResolver
{
public DefaultValueCamelCaseContractResolver(IEnumerable<KeyValuePair<Type, DefaultValueHandling>> overrides) : base(overrides) { }
protected override string ResolvePropertyName(string propertyName)
{
return propertyName.ToCamelCase();
}
}
public class DefaultValueContractResolver : DefaultContractResolver
{
readonly Dictionary<Type, DefaultValueHandling> overrides;
public DefaultValueContractResolver(IEnumerable<KeyValuePair<Type, DefaultValueHandling>> overrides)
: base()
{
if (overrides == null)
throw new ArgumentNullException("overrides");
this.overrides = overrides.ToDictionary(p => p.Key, p => p.Value);
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.DefaultValueHandling == null)
{
DefaultValueHandling handling;
if (overrides.TryGetValue(property.PropertyType, out handling))
property.DefaultValueHandling = handling;
}
return property;
}
}
public static class CamelCaseNameExtensions
{
class CamelCaseNameResolver : CamelCasePropertyNamesContractResolver
{
// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
static CamelCaseNameResolver() { }
internal static readonly CamelCaseNameResolver Instance = new CamelCaseNameResolver();
// Purely to make the protected method public.
public string ToCamelCase(string propertyName)
{
return ResolvePropertyName(propertyName);
}
}
public static string ToCamelCase(this string propertyName)
{
if (propertyName == null)
return null;
return CamelCaseNameResolver.Instance.ToCamelCase(propertyName);
}
}
请注意,如果您有一个标准的类型列表,您始终应用默认值处理覆盖,则应cache an instance of the contract resolver以获得最佳性能。