zoukankan      html  css  js  c++  java
  • 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]特性

  • 相关阅读:
    Maven2-profile多环境配置
    Maven-setting.xml详解
    Maven-通过命令操作maven项目
    Maven-eclipse运行maven命令
    Eclipse-导入maven项目
    Maven-搭建maven web项目
    Maven-搭建普通maven项目
    Maven-pom.xml详解
    Maven-生命周期
    Maven-常用命令
  • 原文地址:https://www.cnblogs.com/fuhai/p/5607934.html
Copyright © 2011-2022 走看看