Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

[FixBug] Error converting value "" to Int64

Open SpringHgui opened this issue 3 years ago • 1 comments

Solve the following problems

When property type is int32, The following code OK

    public class Program
    {
        public class TestModel
        {
            public int from { get; set; }
        }

        public static void Main(string[] args)
        {
            var json = "{ \"from\": \"\"}";
            var ss = JsonConvert.DeserializeObject<TestModel>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            Console.WriteLine(ss);
        }
    }

When property type is int64, The following code throw exception

    public class Program
    {
        public class TestModel
        {
            public long from { get; set; }
        }

        public static void Main(string[] args)
        {
            var json = "{ \"from\": \"\"}";
            var ss = JsonConvert.DeserializeObject<TestModel>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            Console.WriteLine(ss);
        }
    }

set NullValueHandling = NullValueHandling.Ignore they not have the same effect

SpringHgui avatar Dec 15 '21 15:12 SpringHgui