Beispiel #1
0
        /// <summary>
        /// 将JSON文件反序列化
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="path">json所存放的路径</param>
        /// <returns>实体</returns>
        private static T DeSerializableJson <T>(string path)
        {
            T t = default(T);

            try
            {
                FileInfo fi = new FileInfo(path);

                if (fi.Exists)
                {
                    StreamReader  sr = new StreamReader(path, Encoding.UTF8);
                    String        line;
                    StringBuilder strcontent = new StringBuilder();
                    while ((line = sr.ReadLine()) != null)
                    {
                        strcontent.Append(line.ToString());
                    }
                    t = SerializationExtend.JsonDeserialize <T>(strcontent.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(t);
        }
Beispiel #2
0
        /// <summary>
        /// 将字符串根据格式(XML/JSON)反序列化成指定类型的对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="s">目标字符串</param>
        /// <param name="format">输入格式</param>
        /// <returns></returns>
        public static T Deserialize <T>(this string s, SerializationFormat format)
        {
            switch (format)
            {
            case SerializationFormat.Xml:
                return(SerializationExtend.XmlDeserialize <T>(s));

            case SerializationFormat.Json:
                return(SerializationExtend.JsonDeserialize <T>(s));

            default:
                throw new ArgumentException();
            }
        }
Beispiel #3
0
        /// <summary>
        /// 将对象根据格式(XML/JSON)序列化成字符串结果
        /// </summary>
        /// <param name="o">目标对象</param>
        /// <param name="format">输出格式</param>
        /// <returns></returns>
        public static string Serialize(this object o, SerializationFormat format)
        {
            switch (format)
            {
            case SerializationFormat.Xml:
                return(SerializationExtend.XmlSerialize(o));

            case SerializationFormat.Json:
                return(SerializationExtend.JsonSerialize(o));

            default:
                throw new ArgumentException();
            }
        }
Beispiel #4
0
        /// <summary>
        /// 将文件反序列化成对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="path">文件路径</param>
        /// <param name="format">格式: json 或 xml</param>
        /// <returns></returns>
        public static T DeserializeFile <T>(this string path, SerializationFormat format)
        {
            switch (format)
            {
            case SerializationFormat.Xml:
                return(SerializationExtend.DeSerializableXML <T>(path));

            case SerializationFormat.Json:
                return(SerializationExtend.DeSerializableJson <T>(path));

            case SerializationFormat.Binary:
                return(SerializationExtend.DeSerializableBinary <T>(path));

            default:
                throw new ArgumentException();
            }
        }
Beispiel #5
0
        /// <summary>
        /// 对象序列化成文件并保存
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象实体</param>
        /// <param name="path">文件路径</param>
        /// <param name="format">格式:json或 xml</param>
        /// <returns></returns>
        public static bool SerializableFile(this object t, string path, SerializationFormat format)
        {
            switch (format)
            {
            case SerializationFormat.Xml:
                return(SerializationExtend.SerializableXML(t, path));

            case SerializationFormat.Json:
                return(SerializationExtend.SerializableJson(t, path));

            case SerializationFormat.Binary:
                return(SerializationExtend.SerializableBinary(t, path));

            default:
                throw new ArgumentException();
            }
        }