一个 ASP.NET Core 6.0 Web API 项目运行时日志中报错
System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON.
at System.Text.Json.ThrowHelper.ThrowArgumentException_ValueNotSupported()
at System.Text.Json.Utf8JsonWriter.WriteNumberValue(Single value)
请问如何解决?
通过添加 [JsonNumberHandling]
attribute 解决了
public class ResultDocument
{
[JsonNumberHandling(JsonNumberHandling.AllowNamedFloatingPointLiterals)]
public float Score { get; set; }
}
https://stackoverflow.com/questions/7814247/serialize-nan-values-into-json-as-nulls-in-json-net
用的是 System.Text.Json