gpt4 book ai didi

c# - JsonConvert.DeserializeObject,索引超出数组范围

转载 作者:行者123 更新时间:2023-11-30 16:09:06 26 4
gpt4 key购买 nike

所有这些都来自https://github.com/JamesNK/Newtonsoft.Json/issues/469

发在这里是因为我先是在SO上看,没有看到任何东西,所以我在项目的GitHub页面上发帖。


我们目前正在使用 JsonConvert.SerializeObjectJsonConvert.DeserializeObject<T>在客户端和服务器之间发送数据。

我创建了一个工具来创建 10 个客户端,向 10 个不同的服务器发送命令,服务器序列化响应并通过网络发回,然后 10 个客户端反序列化本地机器上的对象。

我在线程池中同时运行这 10 个任务,大约 20% 的时间全部 JsonConvert.DeserializeObject调用失败并显示以下堆栈跟踪:

Error: Index was outside the bounds of the array.
at System.Collections.Generic.List1.Add(T item)
at System.Collections.Generic.List1.System.Collections.IList.Add(Object item)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at MyClientCode()

MyClientCode()正在使用 DeserializeObject像这样:

string json = GetServerResponse();
return JsonConvert.DeserializeObject<ResponseObject>(json);

ResponseObject相当大,包括几个复合对象。但是,我可以保存 json 并使用 DeserializeObject 正确反序列化它,所以我认为对象结构不是问题。

对 List 错误进行一些研究表明,在尝试同时修改 List 对象时会发生这种情况。

最佳答案

来自 James Newton-King:

A new JsonSerializerInternalReader is created each time you deserialize an object. Each deserialization happens in its own state. A high volume server that it deserializing incoming JSON will be deserializing many many things at the same time without issue.

My guess is you have multiple deserializers working over the same list.


谢谢詹姆斯。深入挖掘后,我发现你是对的,我们对反序列化类型的多个实例使用了相同的列表对象。具体来说,该对象看起来像这样:

class Obj {
static List<string> _validSelections = new List<string>() { "One", "Two", "Three", "Four" };
public IEnumerable<string> ValidSelections { get { return _validSelections; } }
... more ...
}

当尝试同时向列表添加对象时,在 JsonSerializerInternalReader.cs 的第 1261 行抛出异常。

在看到它是如何在我们的代码中实现后,我将摆脱静态支持,因为它无论如何都没有为我们提供任何东西。

关于c# - JsonConvert.DeserializeObject,索引超出数组范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28095244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com