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 反序列化带有前导零的数字作为十进制而不是八进制值
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 将数据集转换为列表 2022-01-01
