一、什么是序列化和反序列化
序列化:序列化是将对象状态转换为可保持或传输的格式的过程,比如转化为二进制、xml、json等的过程。
反序列化:与序列化相对的是反序列化,它将流转换为对象,也就是将在序列化过程中所生成的二进制串、xml、json等转换成数据结构或者对象的过程
二、序列化和反序列化能干什么
1、在不同的AppDomain或进程之间传递数据
比喻说现在有一个java写的应用要向C#写的应用进行通信,传递数据,那么这个时候java要将要传的数据进行序列化,C#应用接收数据后进行反序列化。
2、在进程下次启动时读取上次保存的对象的信息
三、序列化的三个方法
1、BinaryFormatter
用法大致如下:
//BinaryFormatter将对象序列化到文件中
List<string> inputList = new List<string>() { "潮流时尚公子","http://gz168168.taobao.com","欢迎专注技术的程序袁来看看笔者的网店哦"};
using (FileStream fsWriter = new FileStream(@"gz168168.tmp",FileMode.Create,FileAccess.Write))
{
BinaryFormatter bf = new BinaryFormatter();
//序列化
bf.Serialize(fsWriter, inputList);
}
//BinaryFormatter将文件中的数据反序列化出来
List<string> outputList = new List<string>();
using (FileStream fsReader = new FileStream(@"gz168168.tmp",FileMode.Open,FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
//反序列化
outputList = (List<string>)bf.Deserialize(fsReader);
}
2、XML序列化
用法大致如下:
//xml序列化到test.xml文件中
List<string> inputList = new List<string>() { "潮流时尚公子","http://gz168168.taobao.com"};
using (FileStream fsWriter = new FileStream(@"test.xml",FileMode.Create,FileAccess.Write))
{
XmlSerializer xs = new XmlSerializer(typeof(List<string>));
xs.Serialize(fsWriter, inputList);
}
//从test.xml文件中反序列化出来
List<string> outputList = new List<string>();
using (FileStream fsReader = new FileStream(@"test.xml",FileMode.Open,FileAccess.Read))
{
XmlSerializer xs = new XmlSerializer(typeof(List<string>));
outputList = xs.Deserialize(fsReader) as List<string>;
}
3、json序列化
json序列化和反序列化有三种方式:
1).使用JavaScriptSerializer类
Student stu = new Student() 3 { 4 ID = 1, 5 Name = "曹操", 6 Sex = "男", 7 Age = 1000 8 }; 9 //序列化 10 DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Student)); 11 MemoryStream msObj = new MemoryStream(); 12 //将序列化之后的Json格式数据写入流中 13 js.WriteObject(msObj, stu); 14 msObj.Position = 0; 15 //从0这个位置开始读取流中的数据 16 StreamReader sr = new StreamReader(msObj, Encoding.UTF8); 17 string json = sr.ReadToEnd(); 18 sr.Close(); 19 msObj.Close(); 20 Console.WriteLine(json); 21 22 23 //反序列化 24 string toDes = json; 25 //string to = "{"ID":"1","Name":"曹操","Sex":"男","Age":"1230"}"; 26 using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(toDes))) 27 { 28 DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(Student)); 29 Student model = (Student)deseralizer.ReadObject(ms);// //反序列化ReadObject 30 Console.WriteLine("ID=" + model.ID); 31 Console.WriteLine("Name=" + model.Name); 32 Console.WriteLine("Age=" + model.Age); 33 Console.WriteLine("Sex=" + model.Sex); 34 } 35 Console.ReadKey();
2).使用DataContractJsonSerializer类
Student stu = new Student() 3 { 4 ID = 1, 5 Name = "关羽", 6 Age = 2000, 7 Sex = "男" 8 }; 9 10 JavaScriptSerializer js = new JavaScriptSerializer(); 11 string jsonData = js.Serialize(stu);//序列化 12 Console.WriteLine(jsonData); 13 14 15 ////反序列化方式一: 16 string desJson = jsonData; 17 //Student model = js.Deserialize<Student>(desJson);// //反序列化 18 //string message = string.Format("ID={0},Name={1},Age={2},Sex={3}", model.ID, model.Name, model.Age, model.Sex); 19 //Console.WriteLine(message); 20 //Console.ReadKey(); 21 22 23 ////反序列化方式2 24 dynamic modelDy = js.Deserialize<dynamic>(desJson); //反序列化 25 string messageDy = string.Format("动态的反序列化,ID={0},Name={1},Age={2},Sex={3}", 26 modelDy["ID"], modelDy["Name"], modelDy["Age"], modelDy["Sex"]);//这里要使用索引取值,不能使用对象.属性 27 Console.WriteLine(messageDy); 28 Console.ReadKey();
3).使用JSON.NET类库
List<Student> lstStuModel = new List<Student>()
3 {
4
5 new Student(){ID=1,Name="张飞",Age=250,Sex="男"},
6 new Student(){ID=2,Name="潘金莲",Age=300,Sex="女"}
7 };
8
9 //Json.NET序列化
10 string jsonData = JsonConvert.SerializeObject(lstStuModel);
11
12 Console.WriteLine(jsonData);
13 Console.ReadKey();
14
15
16 //Json.NET反序列化
17 string json = @"{ 'Name':'C#','Age':'3000','ID':'1','Sex':'女'}";
18 Student descJsonStu = JsonConvert.DeserializeObject<Student>(json);//反序列化
19 Console.WriteLine(string.Format("反序列化: ID={0},Name={1},Sex={2},Sex={3}", descJsonStu.ID, descJsonStu.Name, descJsonStu.Age, descJsonStu.Sex));
20 Console.ReadKey();