反序列化抽象队列< T>在Json [英] Deserializing abstract Queue<T> in Json
问题描述
我有一个抽象类KVP队列。我排队从KVP继承的2个不同的对象。当我序列化队列时,一切正常,但由于KVP无法构造,所以在反序列化时失败。
I have a Queue of an abstract class KVP. I queue 2 different objects which inherit from KVP. Everything works fine when I serialize the queue, but since KVP cannot be constructed it fails on deserialization.
如果它是一个非通用对象,我可以将其反序列化为动态的,但我不确定如何反序列化可以同时存放事件和ID的队列。
If it was a single non generic object I could deserialize as dynamic, but I'm not sure how to deserialize a queue that could hold both events and IDs.
示例代码:
Sample code:
public virtual async Task<bool> LoadFromFile(string FileName, bool addToExistingQueue,bool DeleteFileAfterLoad = true)
{
try
{
IFile File = await PCLStorage.FileSystem.Current.LocalStorage.GetFileAsync(FileName);
var serializedText = await File.ReadAllTextAsync();
var mQueue = JsonConvert.DeserializeObject<Queue<T>>(serializedText,jss);
if (!addToExistingQueue)
{
_queue = new ConcurrentQueue<T>();
}
while (mQueue.Count > 0)
{
_queue.Enqueue(mQueue.Dequeue());
}
if (DeleteFileAfterLoad)
{
await File.DeleteAsync();
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine("Could not load File. Exception Message: " + ex.Message);
return false;
}
}
public virtual async Task<bool> WriteToFile(string FileName)
{
try
{
Debug.WriteLine("Writing File: " + FileName);
var File = await FileSystem.Current.LocalStorage.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
var serializedText = JsonConvert.SerializeObject(_queue.ToList(),jss);
await File.WriteAllTextAsync(serializedText);
return true;
}
catch (Exception ex)
{
Debug.WriteLine("Could not write File with exception message: " + ex.Message);
return false;
}
}
推荐答案
您可以
You could
-
启用 TypeNameHandling (在序列化和反序列化中):
Enable TypeNameHandling (in both serialization and deserialization):
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
var serializedText= JsonConvert.SerializeObject(mQueue, settings);
再后来
And then later
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
var mQueue = JsonConvert.DeserializeObject<Queue<T>>(serializedText, settings);
这增加了一个额外的$ type
属性到你的多态类,如描述这里。
This adds an extra "$type"
property to your polymorphic classes, as is described here.
在选择此解决方案之前,有关使用 TypeNameHandling
讨论可能的安全问题,请参阅 TypeNameHandling在Newtonsoft Json中谨慎使用和如何配置Json.NET以创建易受攻击的Web API 。
Before choosing this solution, for a discussion of possible security concerns using TypeNameHandling
, see TypeNameHandling caution in Newtonsoft Json and How to configure Json.NET to create a vulnerable web API.
编写一个自定义转换器,查看实际属性并选择使用哪个派生类,如下所述:使用json.net反序列化不带类型信息的多态json类。这避免了额外的$ type
属性的需求。
Write a custom converter that looks at the actual properties and chooses which derived class to use, as is discussed here: Deserializing polymorphic json classes without type information using json.net. This avoids the need for the extra "$type"
property.
这篇关于反序列化抽象队列< T>在Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!