小编Mah*_*yak的帖子

System.Text.Json 默认将 double 类型的值 1.0 序列化为 int 类型的值 1

我正在升级到 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)

这里 …

c# json.net asp.net-core system.text.json

3
推荐指数
1
解决办法
1977
查看次数

标签 统计

asp.net-core ×1

c# ×1

json.net ×1

system.text.json ×1