序列化、反序列化
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
相关文章推荐
- Java基础学习总结——Java对象的序列化和反序列化
- 对象,对象集合的简单Xml序列化与反序列化
- java序列化和反序列化
- java序列化、反序列化示例
- java序列化反序列化深入探究
- ProtoBuf 常用序列化/反序列化API
- 二叉树3:介绍二叉树的序列化和反序列化
- 序列化与反序列化 Socket中处理方法
- Xml与对象之间的序列化、反序列化(转)
- python3 pickle模块 序列化对象层次结构,反序列化数据流
- java序列化和反序列化
- asp.net中序列化和反序列化json的两种常用方式
- java序列化和反序列化
- JSON序列化和反序列化日期时间的处理
- 序列化和反序列化C# [Serializable] 与[Serializable()]
- C#对 Json的序列化和反序列化会出现“k_BackingField”
- 请实现两个函数,分别用来序列化和反序列化二叉树
- Java基础——IO流(字节流、字符流、文件与文件夹的拷贝、缓冲流、转换流、基本数据类型流、序列化和反序列化)
- Java对象的序列化和反序列化