Json.NET crashes when serializing unsigned integer (ulong) array(序列化无符号整数 (ulong) 数组时 Json.NET 崩溃)
问题描述
尝试序列化 ulong 数组时出现解析器错误,看起来 Json.NET 库没有检查整数是有符号还是无符号;有人知道解决方法吗?或任何其他可以处理 unsigned int 的 .NET Json 库?
Getting a parser error when trying to serialize a ulong array, looks like the Json.NET library isnt checking if the integer is signed or unsigned; any one know of a workaround for this? or any other .NET Json library that can handle unsigned int's?
*代码如下;*它可以很好地序列化,但是在反序列化时会引发错误;从查看堆栈跟踪来看,它似乎不适合 unsigned int;
* code below; * It serializes fine, but when its deserializing it throws an error; Looks like it doesnt cater for the unsigned int from looking at the stack trace;
NewTonsoft.Json.JsonReaderException : {"JSON integer 18446744073709551615 is too large or small for an Int64."}
Value was either too large or too small for an Int64.
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToInt64(String value, IFormatProvider provider)
at Newtonsoft.Json.JsonTextReader.ParseNumber() in d:DevelopmentReleasesJsonWorkingSrcNewtonsoft.JsonJsonTextReader.cs:line 1360
class Program
{
static void Main(string[] args)
{
string output = JsonConvert.SerializeObject(new ulong[] {ulong.MinValue, 20, 21, 22, ulong.MaxValue});
Console.WriteLine(output);
ulong[] array = JsonConvert.DeserializeObject<ulong[]>(output);
Console.WriteLine(array);
Console.ReadLine();
}
}
推荐答案
没错,在这种情况下,JSON.Net 不会处理大于 long.MaxValue
的值.
You're right, JSON.Net doesn't handle values larger than long.MaxValue
in this case.
除了修改库的源代码外,我没有找到任何修改该行为的方法.作为一种解决方法,您可以将其反序列化为 decimal[]
,然后将其转换为 ulong[]
.
I didn't find any way to modify that behavior, except by modifying the source code of the library. As a workaround, you could deserialize it as decimal[]
and then convert that into ulong[]
.
这篇关于序列化无符号整数 (ulong) 数组时 Json.NET 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:序列化无符号整数 (ulong) 数组时 Json.NET 崩溃


基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01