如何序列化与反序列化复杂对象

简介:

对象序列化技术在大量数据缓存技术中需要用到,但对于复杂对象,如何实现序列化与反序列化呢?

下面是我写的一个软件中的部分有关序列化的代码,共享之供大家批评,这里展示的是简单的二进制序列化,复杂的还有自定义XML序列化,但由于本人对XML Schem不太熟悉,不知道如何编写高效的XML序列化,例如:Word可以直接存储为XML格式。

 

None.gif using System;
None.gif using System.Runtime.Serialization;
None.gif using System.Runtime.Serialization.Formatters.Binary;
None.gif using System.Collections;
None.gif
None.gif
None.gif namespace SGSoft.Asst
ExpandedBlockStart.gif {
ExpandedSubBlockStart.gif    /// <summary>
InBlock.gif    
/// AsstTable 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Serializable()]
InBlock.gif    public class AsstTable: ISerializable
ExpandedSubBlockStart.gif    {
InBlock.gif
InBlock.gif        public AsstTable()
ExpandedSubBlockStart.gif        {
InBlock.gif            this.mInterval=0;
InBlock.gif            this.mRank=33;
InBlock.gif            mExpre_date=System.DateTime.Now;
InBlock.gif            mLastEdit_time=System.DateTime.Now;
InBlock.gif            mPriority=1.0;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gif        /// <summary>
InBlock.gif        
/// 客户调用时的构造函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mInterval">间隔,1表示相临</param>
ExpandedSubBlockEnd.gif        
/// <param name="mRank">数据表的秩,默认应该为32</param>

InBlock.gif        public AsstTable(int mInterval,uint mRank)
ExpandedSubBlockStart.gif        {
InBlock.gif            this.mInterval=mInterval;
InBlock.gif            this.mRank=mRank;
InBlock.gif            this.mData=new int[mRank,mRank];
InBlock.gif            mExpre_date=System.DateTime.Now.Date.AddDays(30);
InBlock.gif            mLastEdit_time=System.DateTime.Now;
InBlock.gif
InBlock.gif            mTableName=string.Format("matrix{0}",mInterval);
InBlock.gif
InBlock.gif            mDescription=string.Format("粘网矩阵{0},创建间隔为{1};创建时间{2}",mTableName,
InBlock.gif                mInterval,mLastEdit_time.ToShortDateString());
InBlock.gif            mPriority=1/mInterval;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gif        /// <summary>
InBlock.gif        
/// 反序列化构照函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="info"></param>
ExpandedSubBlockEnd.gif        
/// <param name="ctxt"></param>

InBlock.gif        public AsstTable(SerializationInfo info,StreamingContext ctxt)
ExpandedSubBlockStart.gif        {
InBlock.gif            mInterval=info.GetInt32("mInterval");
InBlock.gif            mRank=info.GetUInt32("mRank");
InBlock.gif            mDescription=info.GetString("mDescription");
InBlock.gif            mExpre_date=info.GetDateTime("mExpre_date");
InBlock.gif            mLastEdit_time=info.GetDateTime("mLastEdit_time");
InBlock.gif            mPriority=info.GetDouble("mPriority");
InBlock.gif            mData=new int[mRank,mRank];
InBlock.gif            for(int i=0;i<mRank;i++)
ExpandedSubBlockStart.gif            {
InBlock.gif                for(int j=0;j<mRank;j++)
ExpandedSubBlockStart.gif                {
InBlock.gif                    mData[i,j]=info.GetInt32(string.Format("{0}-{1}",i,j));
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gif        私有成员
InBlock.gif
ContractedSubBlock.gif        //公共属性
ContractedSubBlock.gif        ISerializable 成员
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

下面是测试它的类:

 

None.gif using System;
None.gif using System.Collections;
None.gif using NUnit.Framework;
None.gif using SGSoft.Asst;
None.gif
None.gif using System.IO;
None.gif
None.gif namespace TestProject
ExpandedBlockStart.gif {
ExpandedSubBlockStart.gif    /// <summary>
InBlock.gif    
/// Test 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    public class Test
ExpandedSubBlockStart.gif    {
InBlock.gif        public Test()
ExpandedSubBlockStart.gif        {
InBlock.gif            //
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif        [Test,Ignore("OK")]
InBlock.gif        public void TestAsst()
ExpandedSubBlockStart.gif        {
InBlock.gif                        
InBlock.gif            AsstTable at=new AsstTable(1,33);
InBlock.gif
InBlock.gif            int[,] ss=at.Data;
InBlock.gif            for(int i=0;i<at.Rank;i++)
ExpandedSubBlockStart.gif            {
InBlock.gif                for(int j=0;j<at.Rank;j++)
ExpandedSubBlockStart.gif                {
InBlock.gif                    ss[i,j]=i+j;
InBlock.gif                    Console.WriteLine(ss[i,j]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            at.Description="我爱你";
InBlock.gif            at.Priority=1.1;
InBlock.gif            at.TableName="Tablt1";
InBlock.gif            
InBlock.gif
InBlock.gif            System.IO.Stream sm=File.Open("C:\\tests.txt",FileMode.Create);
InBlock.gif            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
InBlock.gif            bf.Serialize(sm,at);
InBlock.gif            sm.Close();
InBlock.gif
InBlock.gif            
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        public void TestDeS()
ExpandedSubBlockStart.gif        {
InBlock.gif            System.IO.Stream sm=File.Open("C:\\tests.txt",FileMode.Open);
InBlock.gif            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
InBlock.gif            AsstTable at=(AsstTable)bf.Deserialize(sm);
InBlock.gif            sm.Close();
InBlock.gif
InBlock.gif            Console.WriteLine(at.Description);
InBlock.gif            Console.WriteLine(at.Priority);
InBlock.gif
InBlock.gif            Console.WriteLine(at.Data[22,4]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


该程序实现了完整的序列化与反序列化,当然,在流处理过程中,还可以使用Zip技术,直接对序列化流进行压缩。

大家如果有对自定义XML序列化实现的较好的代码,希望能和我交流。



本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/10/10/50534.html,如需转载请自行联系原作者

相关文章
|
1月前
|
存储 C#
C#中的序列化和反序列化
C#中的序列化和反序列化
12 0
|
1月前
|
JSON Java API
GSON 泛型对象反序列化解决方案
GSON 泛型对象反序列化解决方案
|
1月前
|
存储 Java 数据库
|
12天前
|
存储 Java
Java输入输出:解释一下序列化和反序列化。
Java中的序列化和反序列化是将对象转换为字节流和反之的过程。ObjectOutputStream用于序列化,ObjectInputStream则用于反序列化。示例展示了如何创建一个实现Serializable接口的Person类,并将其序列化到文件,然后从文件反序列化回Person对象。
19 5
|
1月前
|
存储 C#
C#中的序列化和反序列化案例
C#中的序列化和反序列化案例
12 0
|
1月前
|
JSON Java Maven
使用Jackson进行 JSON 序列化和反序列化
使用Jackson进行 JSON 序列化和反序列化
25 0
|
1月前
|
JSON Android开发 数据格式
android 使用GSON 序列化对象出现字段被优化问题解决方案
android 使用GSON 序列化对象出现字段被优化问题解决方案
|
1月前
|
存储 JSON 网络协议
【计算机网络】序列化,反序列化和初识协议
【计算机网络】序列化,反序列化和初识协议
|
2月前
|
vr&ar
MFC序列化及反序列化变量
MFC序列化及反序列化变量
16 0
|
2月前
|
vr&ar
MFC序列化及反序列化对象
MFC序列化及反序列化对象
18 0

热门文章

最新文章