转自:http://www.cnblogs.com/jams742003/
序列化是将对象状态转换为可保持或可传输的格式的过程,反序列化则过程相反。用于存储和传输数据。
(一)asp.net序列化与反序列化
.net提供多种序列化类
(1)BinaryFormatter 类
名字空间:System.Runtime.Serialization.Formatters.Binary
这个类用于以二进制格式将对象或整个连接对象图形序列化和反序列化
构造器两个:
BinaryFormatter()
BinaryFormatter(ISurrogateSelector, StreamingContext)
介绍它的两个主要方法:
1 Serialize方法
将对象或连接对象图形序列化为给定流
它有两个重载:
Serialize(Stream, Object)
Serialize(Stream, Object,Header[])
类Customer
public
class
Customer
{
public
int
Unid {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
string
Call {
get
;
set
; }
}
下边通过示例来测试序列化方法Serialize
public
void
TestSerialize()
{
Customer customer
=
new
Customer {Unid
=
1
,Name
=
"
宋江
"
,Call
=
"
89589689
"
};
FileStream fs
=
new
FileStream(
"
test.dat
"
, FileMode.Create);
BinaryFormatter formatter
=
new
BinaryFormatter();
try
{
formatter.Serialize(fs, customer);
}
catch
{ }
finally
{
fs.Close();
}
}
测试时,这个会抛出异常:类型“SerializeTest.Customer”未标记为可序列化。所以要在Customer类上添加修饰标签
[Serializable]
public
class
Customer
测试,在bin\debug文件夹中可以看到test.dat文件。
2 Deserialize方法
将流反序列化为对象图形,它也有两个重载
Deserialize(Stream)
Deserialize(Stream, HeaderHandler)
通过示例来测试从流进行反序列化
public
void
TestDeserialize()
{
Customer customer
=
new
Customer();
FileStream fs
=
new
FileStream(
"
test.dat
"
, FileMode.Open);
BinaryFormatter formatter
=
new
BinaryFormatter();
customer
=
formatter.Deserialize(fs)
as
Customer;
fs.Close();
Console.WriteLine(customer.Name);
}
结果输出:宋江
(2)SoapFormatter类
名字空间:System.Runtime.Serialization.Formatters.Soap
以 SOAP 格式将对象或整个连接对象的图形序列化和反序列化。
SOAP就是simple object access protocol的缩写,简单对象传输协议。是一种轻量的,简单的,基于xml的协议。
这个要添加对System.Runtime.Serialization.Formatters.Soap.dll的引用
构造器:
SoapFormatter xx
=
new
SoapFormatter()
SoapFormatter(ISurrogateSelector, StreamingContext)
主要介绍其中2个方法
1 Serialize方法
Serialize(Stream, Object)
Serialize(Stream, Object, Header[]
public
void
TestSoapSerialize()
{
Customer customer
=
new
Customer { Unid
=
1
, Name
=
"
宋江
"
, Call
=
"
89589689
"
};
FileStream fs
=
new
FileStream(
"
soaptest.dat
"
, FileMode.Create);
SoapFormatter formatter
=
new
SoapFormatter();
try
{
formatter.Serialize(fs, customer);
}
catch
{ }
finally
{
fs.Close();
}
}
打开bin\debug中的soaptest.dat文件,这就是soap格式。
2 Deserialize方法
反序列化soap格式
Deserialize(Stream)
Deserialize(Stream, HeaderHandler)
其它的不多说了。
public
void
TestSoapDeserialize()
{
Customer customer
=
new
Customer();
FileStream fs
=
new
FileStream(
"
soaptest.dat
"
, FileMode.Open);
SoapFormatter formatter
=
new
SoapFormatter();
customer
=
formatter.Deserialize(fs)
as
Customer;
fs.Close();
Console.WriteLine(customer.Name);
}
(3)XmlSerializer类
将对象序列化到 XML 文档中和从 XML 文档中反序列化对象
名字空间:System.Xml.Serialization
构造方法太多,不列举了,可以参考帮助
1 Serialize方法
拿出一个来说:
public
void
Serialize(XmlWriter xmlWriter,Object o)
public
void
TestXmlSerialize()
{
Customer customer
=
new
Customer { Unid
=
1
, Name
=
"
宋江
"
, Call
=
"
89589689
"
};
FileStream fs
=
new
FileStream(
"
xmltest.xml
"
, FileMode.Create);
XmlSerializer formatter
=
new
XmlSerializer(
typeof
(Customer));
formatter.Serialize(fs, customer);
fs.Close();
}
结果可以到debug\bin里查看。
2 Deserialize方法
public
void
TestXmlDeserialize()
{
Customer customer
=
new
Customer();
FileStream fs
=
new
FileStream(
"
xmltest.xml
"
, FileMode.Open);
XmlSerializer formatter
=
new
XmlSerializer(
typeof
(Customer));
customer
=
formatter.Deserialize(fs)
as
Customer;
fs.Close();
Console.WriteLine(customer.Name);
}
(二)Json序列化与反序列化
Json相关可见:
http://www.cnblogs.com/jams742003/category/225387.html
Json序列化和反序列化指的是:对象序列化为JSON,并可用于从 JSON 反序列化对象
在.net 3.5中
名字空间:System.Runtime.Serialization.Json
但程序集是: System.ServiceModel.Web.dll
(1)DataContractJsonSerializer类
构造方法很多。
介绍这个类其中2个方法:
1 WriteObject方法
有好种重载,这里测试一个:
public
override
void
WriteObject(Stream stream,Object graph)
测试:
public
void
TestJsonSerialize()
{
Customer customer
=
new
Customer { Unid
=
1
, Name
=
"
宋江
"
, Call
=
"
89589689
"
};
DataContractJsonSerializer ds
=
new
DataContractJsonSerializer(
typeof
(Customer));
FileStream fs
=
new
FileStream(
"
json.txt
"
, FileMode.Create);
ds.WriteObject(fs, customer);
fs.Close();
}
进行Json序列化时,类型不必添加修饰标签
在bin\debug里可以看到json串。
有关json串请见:
http://www.cnblogs.com/jams742003/archive/2009/12/29/1634764.html
2 ReadObject方法
重载很多。
public
override
Object ReadObject(Stream stream)
测试:
public
void
TestJsonDeserialize()
{
DataContractJsonSerializer ds
=
new
DataContractJsonSerializer(
typeof
(Customer));
FileStream fs
=
new
FileStream(
"
json.txt
"
, FileMode.Open);
var cc
=
ds.ReadObject(fs);
fs.Close();
}
(2)JavaScriptSerializer类
为启用 AFAX 的应用程序提供序列化和反序列化功能
名字空间:System.Web.Script.Serialization
程序集:System.Web.Extensions(在 System.Web.Extensions.dll 中)
说其中2个方法
1 Serialize方法
Serialize(Object)
Serialize(Object, StringBuilder)
public
void
TestJsSerialize()
{
Customer customer
=
new
Customer { Unid
=
1
, Name
=
"
宋江
"
, Call
=
"
89589689
"
};
JavaScriptSerializer js
=
new
JavaScriptSerializer();
Console.WriteLine(js.Serialize(customer));
}
输出串:{"Unid":1,"Name":"宋江","Call":"89589689"}
2 Deserialize方法
public
void
TestJsDeserialize()
{
string
str
=
File.ReadAllText(
"
jsjson.txt
"
);
JavaScriptSerializer js
=
new
JavaScriptSerializer();
Customer customer
=
js.Deserialize
<
Customer
>
(str);
Console.WriteLine(customer.Name);
}
(3)Json.net
这是一个第三方工具
请见:http://www.cnblogs.com/jams742003/archive/2009/11/04/1595737.html
名字空间:Newtonsoft.Json
添加Newtonsoft.Json.dll程序集
其中2个方法:
1 SerializeObject方法
public
void
TestJsonnetSerialize()
{
Customer customer
=
new
Customer { Unid
=
1
, Name
=
"
宋江
"
, Call
=
"
89589689
"
};
string
strJson
=
JsonConvert.SerializeObject(customer);
StreamWriter sw
=
File.CreateText(
"
jsonnet.txt
"
);
sw.Write(strJson);
sw.Close();
}
2 DeserializeObject方法
public
void
TestJsonnetDeserialize()
{
string
str
=
File.ReadAllText(
"
jsonnet.txt
"
);
Customer customer
=
JsonConvert.DeserializeObject
<
Customer
>
(str);
Console.WriteLine(customer.Name);
}
更多关于json.net的请见:
http://www.cnblogs.com/jams742003/archive/2009/11/04/1595737.html