它的Json解串器呈现的IList< T>藏品? [英] Which Json deserializer renders IList<T> collections?
问题描述
我想反序列化JSON到集合被表示为的IList℃的对象模型; T>
类型
I'm trying to deserialize json to an object model where the collections are represented as IList<T>
types.
实际的反序列化是在这里:
The actual deserializing is here:
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<IList<Contact>>(
(new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd()));
在我文章中,我得到你应该知道的隐式转换是什么例外。这是联系
类型:
Before i post the exception i'm getting you should know what the implicit conversions are. This is the Contact
type:
public class Contact
{
public int ID { get; set; }
public string Name { get; set; }
public LazyList<ContactDetail> Details { get; set; }
//public List<ContactDetail> Details { get; set; }
}
和这是 ContactDetail
类型:
public class ContactDetail
{
public int ID { get; set; }
public int OrderIndex { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
要知道有重要的事情 LazyList< T>
是它实现的IList< T>
:
The important thing to know with the LazyList<T>
is that it implements IList<T>
:
public class LazyList<T> : IList<T>
{
private IQueryable<T> _query = null;
private IList<T> _inner = null;
private int? _iqueryableCountCache = null;
public LazyList()
{
this._inner = new List<T>();
}
public LazyList(IList<T> inner)
{
this._inner = inner;
}
public LazyList(IQueryable<T> query)
{
if (query == null)
throw new ArgumentNullException();
this._query = query;
}
现在这个 LazyList< $ C>类的定义是罚款,直到我试图反序列化JSON进去。在
System.Web.Script.Serialization.JavaScriptSerializer
似乎要序列名单列表< T>
这是有道理的它的表兄弟姐妹的年龄,但我需要他们在键入的IList< T>
所以他们会扔在我的 LazyList< T>
(至少这就是我想我错了)
Now this LazyList<T>
class definition was fine until i tried deserializing Json into it. The System.Web.Script.Serialization.JavaScriptSerializer
seems to want to serialize lists to List<T>
which makes sense coz of it's age but i need them in the type IList<T>
so they will cast into my LazyList<T>
(at least that's where i think i am going wrong).
我得到这个异常:
System.ArgumentException: Object of type 'System.Collections.Generic.List`1[ContactDetail]' cannot be converted to type 'LazyList`1[ContactDetail]'..
当我尝试使用列表< ContactDetail>
在我的联系
键入(因为你可以看到上面的注释)似乎工作。但我不想使用列表< T>
的。我甚至尝试有我的 LazyList< T>
从列表与LT继承; T>
这似乎执行,但通过了列表< T>的内部
T []
我的实施是一个恶梦,我根本就不想的膨胀列表< T>
在我的模型的任何地方。
When i try using List<ContactDetail>
in my Contact
type (as you can see commented above) it seems to work. But i dont want to use List<T>
's. I even tried having my LazyList<T>
inheriting from List<T>
which seemed to execute but passing the List<T>
's internal T[]
to my implementation was a nightmare and i simply don't want the bloat of List<T>
anywhere in my model.
我也尝试了一些的其他JSON 库无济于事(这是可能的我可能不会使用这些充分发挥其潜力。我或多或少取代了参考和试图重复这个问题上所引用的代码。也许通过设置PARAMS将帮助?)。
I also tried some other json libraries to no avail (it's possible i may not be using these to their full potential. I more or less replaced the references and attempted to repeat the code quoted at the top of this question. Maybe passing settings params will help??).
我不知道现在是什么尝试。我与另一个解串器去?难道我捏捏反序列化本身呢?我需要改变我的类型来取悦解串器?我是否需要更担心的隐式转换或只是实现另一个接口?
I dont know what to try now. Do i go with another deserializer? Do i tweak the deserializing itself? Do i need to change my types to please the deserializer? Do i need to worry more about implicit casting or just implement another interface?
推荐答案
我结束了使用的 Json.NET LIB 具有自定义映射良好的LINQ的支持。这是我的反序列化结束什么看起来像:
I ended up using the Json.NET lib which has good linq support for custom mapping. This is what my deserializing ended up looking like:
JArray json = JArray.Parse(
(new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd()));
IList<Contact> tempContacts = (from c in json
select new Contact
{
ID = (int)c["ID"],
Name = (string)c["Name"],
Details = new LazyList<ContactDetail>(
(
from cd in c["Details"]
select new ContactDetail
{
ID = (int)cd["ID"],
OrderIndex = (int)cd["OrderIndex"],
Name = (string)cd["Name"],
Value = (string)cd["Value"]
}
).AsQueryable()),
Updated = (DateTime)c["Updated"]
}).ToList<Contact>();
return tempContacts;
这篇关于它的Json解串器呈现的IList< T>藏品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!