我得(德)问题序列DynamicObject与是不 Newtownsoft.Json其他的JSON库。 (吉尔,NetJSON,ServiceStack.Text ...)(De)用Jil序列化DynamicObject?
这是我的可扩展的对象类:
public class ExpandableObject : DynamicObject
{
private readonly Dictionary<string, object> _fields = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
[JsonIgnore]
public Dictionary<string, object> Extra { get { return _fields; } }
public override IEnumerable<string> GetDynamicMemberNames()
{
var membersNames = GetType().GetProperties().Where(propInfo => propInfo.CustomAttributes
.All(ca => ca.AttributeType != typeof (JsonIgnoreAttribute)))
.Select(propInfo => propInfo.Name);
return Extra.Keys.Union(membersNames);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _fields.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_fields[binder.Name] = value;
return true;
}
}
与其他库(如吉尔)问题不调用覆盖的方法。 随着Newtonsoft.Json它工作得很好,但性能很差。
例如 - 派生类的反序列化测试:
public class Person : ExpandableObject
{
public int Id { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var json = "{ \"Id\": 12 , \"SomeFiled\" : \"hello\" }";
var person = Jil.JSON.Deserialize<Person>(json);
}
}
没有例外的。它只是忽略了“SomeFiled”场(应该是“额外”)
1.有任何解决方案
2.为什么Newtonsoft.Json能够执行操作和JIL不能? (或其他快速库...)。我深信重写的方法应该由DLR调用..我如何使它工作?
谢谢。
编辑:
现在,我使用DeserilizeDynamic,而不是反序列化(T)。现在它起作用了,我的方法由DLR调用。 现在唯一的问题是DeserilizeDynamic返回'动态',并没有泛型覆盖(T)。并且因为Web API无法解析POST动作中的对象类型。 mabye在未来...
嗨Liam,感谢您的回答,但这并不能解决它。我的代码中有一个错误。现在DLR通过使用Jil的DeserializeDynmic来工作并调用这些方法。 –