Json.net 序列化/反序列化派生类型? [英] Json.net serialize/deserialize derived types?

查看:49
本文介绍了Json.net 序列化/反序列化派生类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json.net (newtonsoft)
我正在浏览文档,但找不到任何关于此的信息或最佳方法.

json.net (newtonsoft)
I am looking through the documentation but I can't find anything on this or the best way to do it.

public class Base
{
    public string Name;
}
public class Derived : Base
{
    public string Something;
}

JsonConvert.Deserialize<List<Base>>(text);

现在我在序列化列表中有派生对象.如何反序列化列表并取回派生类型?

Now I have Derived objects in the serialized list. How do I deserialize the list and get back derived types?

推荐答案

如果你在你的 text 中存储类型(你应该在这个场景中),你可以使用 JsonSerializerSettings.

If you are storing the type in your text (as you should be in this scenario), you can use the JsonSerializerSettings.

参见:如何将 JSON 反序列化为 IEnumerable<基本类型>使用 Newtonsoft JSON.NET

不过要小心.使用除TypeNameHandling = TypeNameHandling.None 以外的任何东西都可能让一个安全漏洞.

Be careful, though. Using anything other than TypeNameHandling = TypeNameHandling.None could open yourself up to a security vulnerability.

这篇关于Json.net 序列化/反序列化派生类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆