Json.NET区分大小写的反序列化 [英] Json.NET case-sensitive deserialization

查看:472
本文介绍了Json.NET区分大小写的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 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"
  ]
}

推荐答案

不太可能.似乎很难对大小写敏感然后对大小写不敏感.

Not likely unfortunately. It seems to be hardcoded to try case-sensitive then case-insensitive.

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

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

This is invoked by the internal reader so there's no simple switch that you could just flip. It should be possible, but you would have to write the converter yourself to reject case-insensitive matches.

这篇关于Json.NET区分大小写的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆