Override Json deserializing a number with a leading zero as a decimal and not an octal value(覆盖 Json 反序列化带有前导零的数字作为十进制而不是八进制值)
问题描述
我正在生成一个 json 对象,
I am generating a json object,
{
"number":0100
}
当这个对象在 C# 中使用 Newtonsoft.Json 反序列化时,0100 被视为八进制数,因为前导零.有没有办法覆盖此功能并使其将数字视为十进制整数?
When this object is deserialized in C# using Newtonsoft.Json, 0100 is treated as an octal number because of the leading zero. Is there a way to override this functionality and make it consider the number as a decimal integer?
推荐答案
我看过 JsonTextReader.ParseNumber()
(数字读取的魔法"发生的方法).我会说这是不可行的.八进制大小写特别处理
I've looked at JsonTextReader.ParseNumber()
(the method where the "magic" of number reading happen). I'll say that it isn't doable. The octal case is especially handled
bool flag2 = c == '0' ...
然后
long value2 = text2.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ?
Convert.ToInt64(text2, 16) :
Convert.ToInt64(text2, 8); // Here OCTAL!!!
我还没有找到任何方法来覆盖这个方法(除了重写所有在 Json 解析中执行 everything 的 Read()
方法)
I haven't found any way to override this method (other than re-writing all the Read()
method that does everything in Json parsing)
这篇关于覆盖 Json 反序列化带有前导零的数字作为十进制而不是八进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:覆盖 Json 反序列化带有前导零的数字作为十进制而不是八进制值


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01