我正在升级到 ASP.NET Core 3.1,在切换到 System.Text.Json 后注意到浮点值的奇怪结果变化。经过调查,我意识到 System.Text.Json 将 double 值转换为 int。例如,考虑一个简单的合约:
public class RowVector
{
[JsonPropertyName("x")]
public double X { get; set; }
[JsonPropertyName("y")]
public double Y { get; set; }
[JsonPropertyName("z")]
public double Z { get; set; }
}
var rowVector = new RowVector { X = 1.0, Y = 213.9, Z = 112.0 };
var serializedData = JsonSerializer.Serialize(rowVector);
Serialized-Data output with System.Text.Json :{"x":1,"y":213.9,"z":112}
Run Code Online (Sandbox Code Playgroud)
这里 x 和 z 是 int 值,而在 Newtonsoft.Json 中,
Serialized-Data output with Newtonsoft.Json :{"X":1.0,"Y":213.9,"Z":112.0}
Run Code Online (Sandbox Code Playgroud)
这里 …