问题描述
我正在生成一个 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 反序列化带有前导零的数字作为十进制而不是八进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!