NewtonSoft Json无效的强制转换异常
这与我的问题 HTTPClient有关缓冲区超过2G;无法将更多字节写入缓冲区,但与IMO的差异足够大,因此它需要一个单独的问题.
This is related to my question HTTPClient Buffer Exceeded 2G; Cannot write more bytes to the buffer but is different enough that IMO it warrants a separate question.
在另一个问题中,我试图找出如何处理打破2G请求缓冲区的问题.这个想法是使用流式传输,但是我需要反序列化.在与Google教授交谈时,我发现我必须使用TextReader进行流/反序列化.所以我的代码是:
In the other question, I'm trying to figure out how to deal with breaking the 2G request buffer. The idea was to use streaming, but I need to deserialize. In talking to Professor Google, I found that I have to use TextReader to stream/deserialize. so my code for that is:
public async Task<API_Json_Special_Feeds.RootObject> walMart_Special_Feed_Lookup(string url)
{
special_Feed_Lookup_Working = true;
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (HttpClient http = new HttpClient(handler))
{
http.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
http.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
url = String.Format(url);
using (var response = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
{
Console.WriteLine(response);
var serializer = new JsonSerializer();
using (StreamReader sr = new StreamReader(await response.Content.ReadAsStreamAsync()))
{
using (var jsonTextReader = new JsonTextReader(sr))
{
API_Json_Special_Feeds.RootObject root = (API_Json_Special_Feeds.RootObject)serializer.Deserialize(jsonTextReader);
return root;
}
}
}
}
}
现在,如您所见,返回类型是强类型的.方法的返回类型匹配.现在,我转到呼叫线路:
Now, as you can see, the return type is strongly typed. The return type of the method matches. Now, I go to the calling line:
API_Json_Special_Feeds.RootObject Items = await net.walMart_Special_Feed_Lookup(specialFeedsURLs[i].Replace("{apiKey}", Properties.Resources.API_Key_Walmart));
因此,我们一直使用匹配类型API_Json_Special_Feeds.RootMethod.
So, we have matching types API_Json_Special_Feeds.RootMethod all the way around.
运行时,调用行将引发InvalidCastException:
When run, the calling line throws an InvalidCastException:
不良结果:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'RootObject'
在返回之前,我已经检查了方法的末尾,结果确实在返回之前已从对象强制转换为API_Json_Special_Feeds.RootMethod.
I've checked at the end of the method before return, and the result is indeed cast from an object to API_Json_Special_Feeds.RootMethod before being returned.
问题:在return语句和调用行之间的某处,正在将要返回的对象从API_Json_Special_Feeds.RootMethod转换为Newtonsoft.Json.Linq.JObject.我无法调试它,因为它们之间没有代码.如果我再次在呼叫线路中投射,则会收到无法投射"错误.如何防止此对象类型的降级/更改?
Question: somewhere between the return statement and the calling line, the object being returned is being converted from an API_Json_Special_Feeds.RootMethod to a Newtonsoft.Json.Linq.JObject. I can't debug it since there's no code in between. If I cast again in the calling line, I get a "Cannot cast" error. How can I prevent the degradation/changing of this object type?
许多人会考虑您的时间,考虑因素以及您可以提供的任何想法或建议!
Many, many thinks for your time, consideration, and for any thoughts or suggestions you can provide!
您需要使用通用重载 JsonSerializer.Deserialize<T>()
You need to use the generic overload JsonSerializer.Deserialize<T>()
var root = serializer.Deserialize<API_Json_Special_Feeds.RootObject>(jsonTextReader);
与BinaryFormatter
生成的文件不同,JSON文件通常不包含c#类型信息,因此接收系统必须指定期望的类型.
Unlike files generated by BinaryFormatter
, JSON files generally do not include c# type information, so it's necessary for the receiving system to specify the expected type.
( JSON标准的扩展名,其中c#类型信息可以包含在JSON文件中- -例如,Json.NET的 TypeNameHandling
-但仍然有必要将JSON反序列化为适当的显式基类.)
(There are extensions to the JSON standard in which c# type information can be included in a JSON file -- e.g. Json.NET's TypeNameHandling
-- but it is still necessary to deserialize the JSON to an appropriate explicit base class.)
请参见从文件反序列化JSON 有关反序列化a的另一个示例流中的强类型c#对象.
See Deserialize JSON from a file for another example of deserializing a strongly typed c# object from a stream.