提问
为什么double会被序列化为NaN
回答
世界上存在Double.NaN这个东西,他被序列化就会成为NaN
example
// See https://aka.ms/new-console-template for more information
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Console.WriteLine("Hello, World!");
var isValueValid = double.TryParse(null, NumberStyles.Any, CultureInfo.InvariantCulture, out var tagValue);
Console.WriteLine($"isValueValid:{isValueValid} tagValue:{tagValue}");
var v = new Example()
{
Name = "12",
Val = Double.NaN
};
Console.WriteLine(JsonConvert.SerializeObject(v));
Console.WriteLine(Double.IsNaN(v.Val.Value));
public class Example
{
public string? Name { get; init; }
public double? Val { get; set; }
}
- 输出:
C:/Repos/tryparse/tryparse/tryparse/bin/Debug/net6.0/tryparse.exe
Hello, World!
isValueValid:False tagValue:0
{"Name":"12","Val":"NaN"}
True
参考
https://learn.microsoft.com/zh-cn/dotnet/api/system.double.nan?view=net-8.0
标签:Console,Val,double,NaN,WriteLine,序列化 From: https://www.cnblogs.com/wuhailong/p/17972608