使用System.Text.Json序列化BigInteger

编程入门 行业动态 更新时间:2024-10-26 14:29:20
本文介绍了使用System.Text.Json序列化BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 System.Text.Json 将 BigInteger 序列化为JSON:

I'm serialising a BigInteger to JSON using System.Text.Json:

JsonSerializer.Serialize(new {foo = new BigInteger(ulong.MaxValue) + 1})

这将导致以下输出:

{"foo":{"IsPowerOfTwo":true,"IsZero":false,"IsOne":false,"IsEven":true,"Sign":1}}

如果我添加一个将 BigInteger 值转换为 ulong 的转换器,则它当然会失败,因为 BigInteger 值太大:

If I add a converter that casts the BigInteger value to a ulong, it of course fails because the BigInteger value is too big:

var options = new JsonSerializerOptions(); options.Converters.Add(new BigIntegerConverter()); JsonSerializer.Serialize(new {foo = new BigInteger(ulong.MaxValue) + 1}, options);

这是转换器:

public class BigIntegerConverter : JsonConverter<BigInteger> { public override BigInteger Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException(); public override void Write(Utf8JsonWriter writer, BigInteger value, JsonSerializerOptions options) => writer.WriteNumberValue((ulong)value); }

我想要的输出是:

{"foo":18446744073709551616}

我知道可以通过Json.NET中的 JsonWriter.WriteRawValue 来实现,但是我仅限于使用 System.Text.Json .

I know this can be achieved with JsonWriter.WriteRawValue in Json.NET, but I am restricted to using System.Text.Json.

有什么方法可以做到,而无需手动破解序列化的字符串?

Is there any way to do this without manually hacking the serialised string?

推荐答案

为 BigInteger 编写转换器有点尴尬,因为如您所述, Utf8JsonReader 和 Utf8JsonWriter

Writing a converter for BigInteger is a little awkward because, as you note, Utf8JsonReader and Utf8JsonWriter do not provide the ability to read and write raw JSON as of .NET 5.

JsonDocument ,但是,确实通过 RootElement.GetRawText() ,因此您可以通过阅读来编写转换器像这样从中间文件中写入并写入中间文件:

JsonDocument, however, does provide access to the raw JSON via RootElement.GetRawText(), so you can write your converter by reading from and writing to an intermediate document like so:

public class BigIntegerConverter : JsonConverter<BigInteger> { public override BigInteger Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.Number) throw new JsonException(string.Format("Found token {0} but expected token {1}", reader.TokenType, JsonTokenType.Number )); using var doc = JsonDocument.ParseValue(ref reader); return BigInteger.Parse(doc.RootElement.GetRawText(), NumberFormatInfo.InvariantInfo); } public override void Write(Utf8JsonWriter writer, BigInteger value, JsonSerializerOptions options) { var s = value.ToString(NumberFormatInfo.InvariantInfo); using var doc = JsonDocument.Parse(s); doc.WriteTo(writer); } }

演示小提琴此处.

更多推荐

使用System.Text.Json序列化BigInteger

本文发布于:2023-11-03 13:05:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555204.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   Text   System   BigInteger   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!