c# - Json.NET区分大小写的反序列化

标签 c# json.net deserialization

是否有反序列化选项来执行区分大小写的反序列化 Json.NET

建议:

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

反序列化时必须失败:

{
  "email": "james@example.com",
  "active": true,
  "createdDate": "2013-01-20T00:00:00Z",
  "roles": [
    "User",
    "Admin"
  ]
}

最佳答案

不幸的是不太可能。尝试区分大小写然后不区分大小写似乎是硬编码的。

/// <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;
}

JsonPropertyCollection

这是由内部阅读器调用的,因此没有您可以翻转的简单开关。这应该是可能的,但您必须自己编写转换器以拒绝不区分大小写的匹配。

关于c# - Json.NET区分大小写的反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568124/

相关文章:

c# - 如何从现有列表创建新列表?

vb.net - 解析 JSON 数组

c# - 如何使用 JsonConvert.DeserializeObject 在动态 JSON 中使用 null 条件运算符

c# - 反序列化 XML Rest WebApi 调用?

c# - 使用同一个类反序列化不同的可能对象

c# - 动态方法,将返回值存储在本地

c# - linkbutton 没有在 asp.net 中回发

java - 如何在 Java 中创建自定义 JsonDeserializer?

c# - Visual Studio c# 编译器未注意到已更改的方法名称

c# - 如何从 JSON 中删除不需要的字符串属性