天天看點

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]特性

轉載于:https://www.cnblogs.com/fuhai/p/5607934.html