最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why does the Json.NET deserializer accept string values for boolean properties? - Stack Overflow

programmeradmin2浏览0评论

I expected this code to throw an Exception because I am trying to deserialize a string value into a boolean property. However, I was surprised to find that Json.NET happily converts the string value to a boolean. Why? Is there any way to change this behavior, for example a "strict" mode?

using Newtonsoft.Json;

var activeAccount = JsonConvert.DeserializeObject<Account>("{\"active\": \"True\"}");
var inactiveAccount = JsonConvert.DeserializeObject<Account>("{\"active\": \"false\"}");

Console.WriteLine(activeAccount.Active); // True
Console.WriteLine(inactiveAccount.Active); // False

public class Account
{
    public bool Active { get; set; }
}
发布评论

评论列表(0)

  1. 暂无评论