覆盖的Json反序列化一批具有前导零的小数,而不是一个八进制值 [英] Override Json deserializing a number with a leading zero as a decimal and not an octal value
问题描述
我生成一个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解析/ EM>)
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反序列化一批具有前导零的小数,而不是一个八进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!