您的位置:首页 > 其它

序列化、反序列化

2016-06-11 11:57 225 查看
原文链接:http://www.cnblogs.com/lystory/p/5569160.html

序列化和反序列化的作用就是,将对象变成可以存储和传输以及反转的数据(二进制、SOAP、XML、JSON等等),进行保存、传输、和反转的过程。

1、对象:这个对象可以是个类、文件、视频、运行中的游戏等一系列内容

2、序列化:就是将对象转换成数据(二进制、SOAP、XML、JSON等等)的动作

3、存储和传输:可以将序列化之后的数据存起来,也可以传送给别的程序(SOAP、WEBAPI、WCF、以及现有流行的ajax等)的基础都是序列化传输的过程

4、反序列化:就是接收方、或者是使用方把序列化的数据,反转还原的过程

 

目前主流的过程大概有4种:

1、JSON(文件更小、当复杂的时候不容易阅读)

2、XML(容易阅读、文件小)

3、SOAP(文件太大,不容易阅读)

4、Binary(文件小,不能阅读)

 

json只能一次性解析、xml可以分片解析

它们各有优缺点、适用性也不相同,我只知道点简单的。深究需要读者去做

 

代码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace serialize
{
/// <summary>
/// Json序列化助手
/// </summary>
public class JsonSerializeHelper
{
public static T Deserialize<T>(string content) where T : class, new()
{
return JsonConvert.DeserializeObject<T>(content);
}

public static string Serialize<T>(T obj) where T : class, new()
{
return JsonConvert.SerializeObject(obj);
}

public static void Serialize<T, S>(T obj, S stream) where S : Stream where T : class, new()
{
using (stream)
{
byte[] content = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
stream.Write(content, 0, content.Length);
}
}
}
/// <summary>
/// XML序列化助手
/// </summary>
public class XMLSerializeHelper
{

public static T Deserialize<T, S>(S stream) where S : Stream where T : class, new()
{
using (stream)
{
XmlSerializer xmlSearializer = new XmlSerializer(typeof(T));
//    fileStream.Position = 0;
// fileStream.Seek(0, SeekOrigin.Begin);
return (T)xmlSearializer.Deserialize(stream);
}

}

public static void Serialize<T, S>(T obj, S stream) where S : Stream where T : class, new()
{
using (stream)
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
xmlFormat.Serialize(stream, obj);
}
}
}
/// <summary>
/// 二进制序列化助手
/// </summary>
public class BinarySerializeHelper
{

public static T Deserialize<T, S>(S stream) where S : Stream where T : class, new()
{
using (stream)
{
BinaryFormatter formatter = new BinaryFormatter();
return (T)formatter.Deserialize(stream);
}
}

public stati
7ff7
c void Serialize<T, S>(T obj, S stream) where S : Stream where T : class, new()
{
using (stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
}
}
}

/// <summary>
/// SOAP序列化反序列化助手
/// </summary>
public class SOAPSerializeHelper
{

public static T Deserialize<T, S>(S stream) where S : Stream where T : class, new()
{
using (stream)
{
SoapFormatter formatter = new SoapFormatter();
return (T)formatter.Deserialize(stream);
}
}

public static void Serialize<T, S>(T obj, S stream) where S : Stream where T : class, new()
{
using (stream)
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, obj);
}
}
}
}

转载于:https://www.cnblogs.com/lystory/p/5569160.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: