/// <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); }
/// <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(); } }