123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- using Newtonsoft.Json;
- using System;
- using System.IO;
- using System.IO.Compression;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization.Json;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
- namespace Venus_Unity
- {
- public class SerializeHelper
- {
- private static SerializeHelper m_Instance;
- public static SerializeHelper Instance
- {
- get
- {
- if (m_Instance == null)
- {
- m_Instance = new SerializeHelper();
- }
- return m_Instance;
- }
- }
- public SerializeHelper()
- { }
- #region XML序列化
- /// <summary>
- /// 文件化XML序列化
- /// </summary>
- /// <param name="obj">对象</param>
- /// <param name="filename">文件路径</param>
- public void Save(object obj, string filename)
- {
- FileStream fs = null;
- try
- {
- fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
- //Create our own namespaces for the output
- XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
- //Add an empty namespace and empty value
- ns.Add("", "");
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- serializer.Serialize(fs, obj, ns);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (fs != null) fs.Close();
- }
- }
- /// <summary>
- /// 文件化XML反序列化
- /// </summary>
- /// <param name="type">对象类型</param>
- /// <param name="filename">文件路径</param>
- public object Load(Type type, string filename)
- {
- FileStream fs = null;
- try
- {
- fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- XmlSerializer serializer = new XmlSerializer(type);
- return serializer.Deserialize(fs);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (fs != null) fs.Close();
- }
- }
- /// <summary>
- /// 文本化XML序列化
- /// </summary>
- /// <param name="item">对象</param>
- public string ToXml<T>(T item)
- {
- XmlSerializer serializer = new XmlSerializer(item.GetType());
- StringBuilder sb = new StringBuilder();
- using (XmlWriter writer = XmlWriter.Create(sb))
- {
- serializer.Serialize(writer, item);
- return sb.ToString();
- }
- }
- /// <summary>
- /// 文本化XML反序列化
- /// </summary>
- /// <param name="str">字符串序列</param>
- public T FromXml<T>(string str)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- using (XmlReader reader = new XmlTextReader(new StringReader(str)))
- {
- return (T)serializer.Deserialize(reader);
- }
- }
- #endregion
- #region BinaryFormatter序列化
- /// <summary>
- /// BinaryFormatter序列化
- /// </summary>
- /// <param name="item">对象</param>
- public string ToBinary<T>(T item)
- {
- BinaryFormatter formatter = new BinaryFormatter();
- using (MemoryStream ms = new MemoryStream())
- {
- formatter.Serialize(ms, item);
- ms.Position = 0;
- byte[] bytes = ms.ToArray();
- StringBuilder sb = new StringBuilder();
- foreach (byte bt in bytes)
- {
- sb.Append(string.Format("{0:X2}", bt));
- }
- return sb.ToString();
- }
- }
- /// <summary>
- /// BinaryFormatter反序列化
- /// </summary>
- /// <param name="str">字符串序列</param>
- public T FromBinary<T>(string str)
- {
- int intLen = str.Length / 2;
- byte[] bytes = new byte[intLen];
- for (int i = 0; i < intLen; i++)
- {
- int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);
- bytes[i] = (byte)ibyte;
- }
- BinaryFormatter formatter = new BinaryFormatter();
- using (MemoryStream ms = new MemoryStream(bytes))
- {
- return (T)formatter.Deserialize(ms);
- }
- }
- #endregion
- /// <summary>
- /// 将对象序列化为二进制字节
- /// </summary>
- /// <param name="obj">待序列化的对象</param>
- /// <returns></returns>
- public byte[] SerializeToBinary(object obj)
- {
- byte[] bytes = new byte[2500];
- using (MemoryStream memoryStream = new MemoryStream())
- {
- BinaryFormatter bformatter = new BinaryFormatter();
- bformatter.Serialize(memoryStream, obj);
- memoryStream.Seek(0, 0);
- if (memoryStream.Length > bytes.Length)
- {
- bytes = new byte[memoryStream.Length];
- }
- bytes = memoryStream.ToArray();
- }
- return bytes;
- }
- /// <summary>
- /// 从二进制字节中反序列化为对象
- /// </summary>
- /// <param name="type">对象的类型</param>
- /// <param name="bytes">字节数组</param>
- /// <returns>反序列化后得到的对象</returns>
- public object DeserializeFromBinary(Type type, byte[] bytes)
- {
- object result = new object();
- using (MemoryStream memoryStream = new MemoryStream(bytes))
- {
- BinaryFormatter serializer = new BinaryFormatter();
- result = serializer.Deserialize(memoryStream);
- }
- return result;
- }
- /// <summary>
- /// 将文件对象序列化到文件中
- /// </summary>
- /// <param name="obj">待序列化的对象</param>
- /// <param name="path">文件路径</param>
- /// <param name="fileMode">文件打开模式</param>
- public void SerializeToBinary(object obj, string path, FileMode fileMode)
- {
- using (FileStream fs = new FileStream(path, fileMode))
- {
- // Construct a BinaryFormatter and use it to serialize the data to the stream.
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(fs, obj);
- }
- }
- /// <summary>
- /// 将文件对象序列化到文件中
- /// </summary>
- /// <param name="obj">待序列化的对象</param>
- /// <param name="path">文件路径</param>
- public void SerializeToBinary(object obj, string path)
- {
- SerializeToBinary(obj, path, FileMode.Create);
- }
- /// <summary>
- /// 从二进制文件中反序列化为对象
- /// </summary>
- /// <param name="type">对象的类型</param>
- /// <param name="path">二进制文件路径</param>
- /// <returns>反序列化后得到的对象</returns>
- public object DeserializeFromBinary(Type type, string path)
- {
- object result = new object();
- using (FileStream fileStream = new FileStream(path, FileMode.Open))
- {
- BinaryFormatter serializer = new BinaryFormatter();
- result = serializer.Deserialize(fileStream);
- }
- return result;
- }
- /// <summary>
- /// 获取对象的转换为二进制的字节大小
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public long GetByteSize(object obj)
- {
- long result;
- BinaryFormatter bFormatter = new BinaryFormatter();
- using (MemoryStream stream = new MemoryStream())
- {
- bFormatter.Serialize(stream, obj);
- result = stream.Length;
- }
- return result;
- }
- /// <summary>
- /// 克隆一个对象
- /// </summary>
- /// <param name="obj">待克隆的对象</param>
- /// <returns>克隆的一个新的对象</returns>
- public object Clone(object obj)
- {
- object cloned = null;
- BinaryFormatter bFormatter = new BinaryFormatter();
- using (MemoryStream memoryStream = new MemoryStream())
- {
- try
- {
- bFormatter.Serialize(memoryStream, obj);
- memoryStream.Seek(0, SeekOrigin.Begin);
- cloned = bFormatter.Deserialize(memoryStream);
- }
- catch //(Exception e)
- {
- ;
- }
- }
- return cloned;
- }
- /// <summary>
- /// 从文件中读取文本内容
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns>文件的内容</returns>
- public string ReadFile(string path)
- {
- string content = string.Empty;
- using (StreamReader reader = new StreamReader(path))
- {
- content = reader.ReadToEnd();
- }
- return content;
- }
-
- private byte[] compress(byte[] bytes)
- {
- using (MemoryStream ms = new MemoryStream())
- {
- using (DeflateStream gzs = new DeflateStream(ms, CompressionMode.Compress, false))
- {
- gzs.Write(bytes, 0, bytes.Length);
- }
- ms.Close();
- return ms.GetBuffer();
- }
- }
- private byte[] decompress(byte[] bytes)
- {
- using (MemoryStream ms = new MemoryStream(bytes, false))
- {
- using (DeflateStream gzs = new DeflateStream(ms, CompressionMode.Decompress, false))
- {
- using (MemoryStream dest = new MemoryStream())
- {
- byte[] tmp = new byte[bytes.Length];
- int read;
- while ((read = gzs.Read(tmp, 0, tmp.Length)) != 0)
- {
- dest.Write(tmp, 0, read);
- }
- dest.Close();
- return dest.GetBuffer();
- }
- }
- }
- }
- /// <summary>
- /// 序列化成一个字符串
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="entity"></param>
- /// <returns></returns>
- public string XMLSerializeToString<T>(T entity)
- {
- StringBuilder buffer = new StringBuilder();
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- using (TextWriter writer = new StringWriter(buffer))
- {
- serializer.Serialize(writer, entity);
- }
- return buffer.ToString();
- }
- /// <summary>
- /// xml字符串反序列化为一个XML文件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="xmlString"></param>
- /// <returns></returns>
- public T DeXMLSerialize<T>(string xmlString)
- {
- T cloneObject = default(T);
- StringBuilder buffer = new StringBuilder();
- buffer.Append(xmlString);
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- using (TextReader reader = new StringReader(buffer.ToString()))
- {
- Object obj = serializer.Deserialize(reader);
- cloneObject = (T)obj;
- }
- return cloneObject;
- }
- /// <summary>
- /// Json转换成对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="jsonText"></param>
- /// <returns></returns>
- public T JsonStringToObject<T>(string jsonText)
- {
- DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
- MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
- T obj = (T)s.ReadObject(ms);
- ms.Dispose();
- return obj;
- }
- /// <summary>
- /// 对象转换成JSON
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public string ObjectToJsonString<T>(T obj)
- {
- string result = string.Empty;
- try
- {
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
- using (MemoryStream ms = new MemoryStream())
- {
- serializer.WriteObject(ms, obj);
- ms.Position = 0;
- using (StreamReader read = new StreamReader(ms))
- {
- result = read.ReadToEnd();
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception(" 对象转换成JSON失败!" + ex.Message);
- }
- return result;
- }
- /// <summary>
- /// 类对象保存为Json文件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <param name="strFilePath"></param>
- /// <returns></returns>
- public bool WriteToJsonFile<T>(T obj, string strFilePath)
- {
- bool bRes = false;
- try
- {
- string strJson = ObjectToJsonString(obj);
- //File.WriteAllText(strFilePath, strJson, Encoding.UTF8);
- WriteToJsonFile(strJson, strFilePath);
- bRes = true;
- }
- catch (Exception ex)
- {
- throw new Exception("Write Json File Fail!" + ex.Message);
- }
- return bRes;
- }
- /// <summary>
- /// Json文件转换为类对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strFilePath"></param>
- /// <returns></returns>
- public T ReadFromJsonFile<T>(string strFilePath)
- {
- if (File.Exists(strFilePath))
- {
- try
- {
- string strJson = File.ReadAllText(strFilePath);
- return JsonStringToObject<T>(strJson);
- }
- catch (Exception ex)
- {
- throw new Exception("Read Json File Fail!" + ex.Message);
- }
- }
- else
- {
- throw new Exception("File is not Exit!");
- }
- }
- /// <summary>
- /// 对象转换成JSON,并保存文件
- /// </summary>
- /// <param name="obj"></param>
- /// <param name="path"></param>
- public void WriteToJsonFile(string content, string path)
- {
- if (!File.Exists(path)) // 判断是否已有相同文件
- {
- FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
- fs1.Flush();
- fs1.Dispose();
- fs1.Close();
- }
- File.WriteAllText(path, ConvertJsonString(content));
- }
- /// <summary>
- /// 格式化json字符串
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- private string ConvertJsonString(string str)
- {
- JsonSerializer serializer = new JsonSerializer();
- TextReader tr = new StringReader(str);
- JsonTextReader jtr = new JsonTextReader(tr);
- object obj = serializer.Deserialize(jtr);
- if (obj != null)
- {
- StringWriter textWriter = new StringWriter();
- JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
- {
- Formatting = Newtonsoft.Json.Formatting.Indented,
- Indentation = 4,
- IndentChar = ' '
- };
- serializer.Serialize(jsonWriter, obj);
- return textWriter.ToString();
- }
- else
- {
- return str;
- }
- }
- public static T DeepCopyJson<T>(T obj)
- {
- return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(obj));
- }
- }
- }
|