使用Json.Net处理json序列化和反序列化接口或继承类

之前一直没有怎么关注过Newtonsoft的Json.Net这个第三方的.NET Json框架,主要是我之前在开发项目的时候大多数使用的都是.NET自带的Json序列化类JavaScriptSerializer,可是最近在项目中须要序列化和反序列化一个实现接口的类,而若是使用JavaScriptSerializer的话就会出现问题,咱们来看看以下场景。json

 

首先咱们有一个接口IPeople和一个实现了该接口的类Manapp

interface IPeople
{
    string Name { get; set; }
    int Age { get; set; }
}

class Man : IPeople
{
    public string Name { get; set; }

    public int Age { get; set; }
}


咱们使用JavaScriptSerializer直接序列化IPeople接口框架

IPeople poeple = new Man();
poeple.Age = 25;
poeple.Name = "Scott";

JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string textJson = jsSerializer.Serialize(poeple);
poeple = jsSerializer.Deserialize<IPeople>(textJson);

会获得序列化后的json文本textJson以下函数

{"Name":"Scott","Age":25}

咱们能够看到在序列化后的json中没有任何属性说明这段json究竟是由什么类序列化而来的,紧接着在JavaScriptSerializer执行jsSerializer.Deserialize<IPeople>(textJson)作反序列化的时候就抛出了异常提示IPeople没有默认无参构造函数,也就是说JavaScriptSerializer不知道应该把textJson中的json反序列化为类Man。
ui

 

而若是咱们使用的是Json.NET的话,就能够完美的实现接口IPeople的序列化和反序列化,咱们来看看怎么使用Json.NET的序列化和反序列化this

IPeople poeple = new Man();
poeple.Age = 25;
poeple.Name = "Scott";

JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.TypeNameHandling = TypeNameHandling.All;//这一行就是设置Json.NET可以序列化接口或继承类的关键,将TypeNameHandling设置为All后,Json.NET会在序列化后的json文本中附加一个属性说明json究竟是从什么类序列化过来的,也能够设置TypeNameHandling为Auto,表示让Json.NET自动判断是否须要在序列化后的json中添加类型属性,若是序列化的对象类型和声明类型不同的话Json.NET就会在json中添加类型属性,反之就不添加,可是我发现TypeNameHandling.Auto有时候不太好用。。。
string textJson = JsonConvert.SerializeObject(poeple, jsonSerializerSettings);//将JsonSerializerSettings做为参数传入序列化函数,这样序列化后的Json就附带类型属性
poeple = JsonConvert.DeserializeObject<IPeople>(textJson, jsonSerializerSettings);//将JsonSerializerSettings做为参数传入反序列化函数,这样Json.NET就会读取json文本中的类型属性,知道应该反序列化成什么类型

这里IPeople接口能被成功序列化和返序列化的关键就是jsonSerializerSettings.TypeNameHandling = TypeNameHandling.All这行代码,咱们来看看Json.NET序列化后的json文本信息spa

{"$type":"Json.Man, Json","Name":"Scott","Age":25}

能够看到Json.NET在序列化后的json文本中添加了一个属性叫$type来讲明json是从Json.Man类序列化而来的,那么后面再反序列化的时候Json.NET就成功地将上面的json文本反序列化成了类Man.code

 

因此Json.NET在作json的序列化和反序列化的时候比JavaScriptSerializer更全面,固然在使用JavaScriptSerializer的时候自定义Converter也能够作到序列化接口和继承类,可是这要麻烦不少。这一点也会让我之后更多使用Json.NET来实现json的序列化和反序列化。orm

 

给出一个设置TypeNameHandling.Auto的例子说明,是老外写的,我以为将TypeNameHandling.Auto解释得很清楚了。对象

Json.Net has a setting that intelligently adds type information - declare it like this:

new JsonSerializer { TypeNameHandling = TypeNameHandling.Auto };

This will determine whether type embedding is required and add it where necessary. Lets say I had the following classes:

public class Message
{
    public object Body { get; set; }
}

public class Person
{
    public string Name { get; set; }
}

public class Manager : Person
{

}

public class Department
{
    private List<Person> _employees = new List<Person>();
    public List<Person> Employees { get { return _employees; } }
}

 

Notice the Message Body is of type object, and that Manager subclasses Person. If I serialize a Message with a Department Body that has a single Manager I get this:

{
    "Body":
    {
        "$type":"Department, MyAssembly",
        "Employees":[
            {
                "$type":"Manager, MyAssembly",
                "Name":"Tim"
            }]
    }
}


Notice how it's added the $type property to describe the Department and Manager types. If I now add a Person to the Employees list and change the Message Body to be of type Department like this:

public class Message
{
    public Department Body { get; set; }
}

 

then the Body type annotation is no longer needed and the new Person is not annotated - absence of annotation assumes the element instance is of the declared array type. The serialized format becomes:

{
    "Body":
    {
        "Employees":[
            {
                "$type":"Manager, MyAssembly",
                "Name":"Tim"
            },
            {
                "Name":"James"
            }]
    }
}

This is an efficient approach - type annotation is only added where required. While this is .NET specific, the approach is simple enough to handle that deserializers/message types on other platforms should be fairly easily extended to handle this.

I'd be reticent about using this in a public API though, as it is non-standard. In that case you'd want to avoid polymorphism, and make versioning and type information very explicit properties in the message.

 

最后经过我写的一个例子来演示怎么自定义和使用Json.Net的转换器,这个例子还阐述了Json.Net在序列化和反序列化实现了接口IEnumerable的类时所遇到的问题,有兴趣的朋友能够下载。

JsonNetArraySerialization.rar

相关文章
相关标签/搜索