使用 System.Text.Json 将科学记数法反序列化为 long
Deserialize scientific notation to long with System.Text.Json
我 JSON 有一个科学记数法的数字,例如1.83E+2
。用 Json.NET 将其反序列化为 long
对我来说效果很好,但是当我用 System.Text.Json 中的新反序列化器替换反序列化器时,它会抛出 JsonException
:
System.Text.Json.JsonException: 'The JSON value could not be converted to System.Int64. ...'
这是一个可重现的例子:
static void Main()
{
// test1 is 183
var test1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Foo>(@"{""Bar"": 1.83E+2}");
// throws JsonException
var test2 = System.Text.Json.JsonSerializer.Deserialize<Foo>(@"{""Bar"": 1.83E+2}");
}
public class Foo
{
public long Bar { get; set; }
}
System.Text.Json.JsonSerializer
不支持将科学计数法转换为 long
。反而。您需要序列化为 double
或 decimal
并改为处理该数字:
public class Foo
{
public double Bar { get; set; }
}
在询问 .NET Core 团队后,他们提供了 this solution 问题:
static void Main()
{
var options = new JsonSerializerOptions
{
Converters = { new CustomInt64Converter() }
};
// test1 is 183
var test1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Foo>(@"{""Bar"": 1.83E+2}");
// test2 is 183
var test2 = System.Text.Json.JsonSerializer.Deserialize<Foo>(@"{""Bar"": 1.83E+2}", options);
}
public class Foo
{
public long Bar { get; set; }
}
public class CustomInt64Converter : JsonConverter<long>
{
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TryGetInt64(out var result) ? result : Convert.ToInt64(reader.GetDecimal());
}
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}
我 JSON 有一个科学记数法的数字,例如1.83E+2
。用 Json.NET 将其反序列化为 long
对我来说效果很好,但是当我用 System.Text.Json 中的新反序列化器替换反序列化器时,它会抛出 JsonException
:
System.Text.Json.JsonException: 'The JSON value could not be converted to System.Int64. ...'
这是一个可重现的例子:
static void Main()
{
// test1 is 183
var test1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Foo>(@"{""Bar"": 1.83E+2}");
// throws JsonException
var test2 = System.Text.Json.JsonSerializer.Deserialize<Foo>(@"{""Bar"": 1.83E+2}");
}
public class Foo
{
public long Bar { get; set; }
}
System.Text.Json.JsonSerializer
不支持将科学计数法转换为 long
。反而。您需要序列化为 double
或 decimal
并改为处理该数字:
public class Foo
{
public double Bar { get; set; }
}
在询问 .NET Core 团队后,他们提供了 this solution 问题:
static void Main()
{
var options = new JsonSerializerOptions
{
Converters = { new CustomInt64Converter() }
};
// test1 is 183
var test1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Foo>(@"{""Bar"": 1.83E+2}");
// test2 is 183
var test2 = System.Text.Json.JsonSerializer.Deserialize<Foo>(@"{""Bar"": 1.83E+2}", options);
}
public class Foo
{
public long Bar { get; set; }
}
public class CustomInt64Converter : JsonConverter<long>
{
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TryGetInt64(out var result) ? result : Convert.ToInt64(reader.GetDecimal());
}
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}