c#序列化反序列化工具(json,binary,xml)

using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;
namespace weekReportPlan
{
    public static class  SerializeHelper
    {
        /// <summary>
        /// 將一個對象序列化爲字節(byte)數組
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static byte[] Serialize(this object t)
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter former = new BinaryFormatter();
            former.Serialize(stream, t);
            return stream.GetBuffer();
        }

        /// <summary>
        /// 將字節(byte)數組
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="b"></param>
        /// <returns></returns>
        public static T DeserializeTo<T>(this byte[] b)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            return (T)bFormatter.Deserialize(new MemoryStream(b));
        }

        /// <summary>
        /// Json序列化
        /// </summary>
        public static string ToJsJson(this object item)
        {

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, item);
                StringBuilder sb = new StringBuilder();
                sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
                return sb.ToString();

            }

        }

        /// <summary>
        /// Json反序列化
        /// </summary>
        public static T FromJsonTo<T>(this string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T jsonObject = (T)ser.ReadObject(ms);
            ms.Close();
            return jsonObject;
        }

        /// <summary>
        /// 將一個對象序列化爲字節(byte)數組並保存到指定文件路徑
        /// </summary>
        /// <param name="t"></param>
        /// <param name="path"></param>
        public static void SerializeToFile(this object t,string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, t);
            }
        }

        /// <summary>
        /// 從指定文件路徑中讀取數據並反序列化爲對象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T DeserializeFromFileTo<T>(string path)
        {
            T t;
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                t = (T)bf.Deserialize(fs);
            }
            return t;
        }
    }
}

注意1:序列化爲二進制的時候需要在類中添加[Serializable]特性

注意2:如果類添加了[Serializable]特性,並且屬性都是自動屬性時,將該類對象序列化爲json的時候需要給類再添加[DataContract]特性,給類屬性添加[DataMember]特性

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章