Override Json deserializing a number with a leading zero as a decimal and not an octal value

前端 未结 3 1037
慢半拍i
慢半拍i 2020-12-07 03:55

I am generating a json object,

{
   \"number\":0100
}

When this object is deserialized in C# using Newtonsoft.Json, 0100 i

3条回答
  •  一整个雨季
    2020-12-07 04:09

    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' ...
    

    and then

    long value2 = text2.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? 
                      Convert.ToInt64(text2, 16) : 
                      Convert.ToInt64(text2, 8); // Here OCTAL!!!
    

    I haven't found any way to override this method (other than re-writing all the Read() method that does everything in Json parsing)

提交回复
热议问题