C# Json.NET区分大小写的反序列化
是否有反序列化选项来执行区分大小写的反序列化 建议:C# Json.NET区分大小写的反序列化,c#,json.net,deserialization,C#,Json.net,Deserialization,是否有反序列化选项来执行区分大小写的反序列化 建议: public class Account { public string Email { get; set; } public bool Active { get; set; } public DateTime CreatedDate { get; set; } public IList<string> Roles { get; set; } } 不幸的是,不太可能。它似乎是硬编码的,尝试区分大小
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
不幸的是,不太可能。它似乎是硬编码的,尝试区分大小写,然后不区分大小写
/// <summary>
/// Gets the closest matching <see cref="JsonProperty"/> object.
/// First attempts to get an exact case match of propertyName and then
/// a case insensitive match.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>A matching property if found.</returns>
public JsonProperty GetClosestMatchProperty(string propertyName)
{
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
if (property == null)
{
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
}
return property;
}
//
///获取最接近的匹配对象。
///首先尝试获取propertyName的精确大小写匹配,然后
///不区分大小写的匹配。
///
///属性的名称。
///一个匹配的属性(如果找到)。
公共JsonProperty GetClosestMatchProperty(字符串propertyName)
{
JsonProperty=GetProperty(propertyName,StringComparison.Ordinal);
if(属性==null)
{
property=GetProperty(propertyName,StringComparison.OrdinalIgnoreCase);
}
归还财产;
}
这是由内部读取器调用的,因此没有简单的开关可以直接翻转。这应该是可能的,但您必须自己编写转换器以拒绝不区分大小写的匹配。为什么要失败?在变量名中混合使用案例会导致数月后的维护噩梦参见github,它要求能够选择使用Json.NET进行区分大小写的反序列化。
/// <summary>
/// Gets the closest matching <see cref="JsonProperty"/> object.
/// First attempts to get an exact case match of propertyName and then
/// a case insensitive match.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>A matching property if found.</returns>
public JsonProperty GetClosestMatchProperty(string propertyName)
{
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
if (property == null)
{
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
}
return property;
}