/// <summary> /// 编码 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param name="format"></param> /// <returns></returns> public static string Encode <T>(T t, Formatting format) { IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); BigintConverter bigintConverter = new BigintConverter(); //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式 timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"; return(JsonConvert.SerializeObject(t, format, timeConverter, bigintConverter)); }
/// <summary> /// 解码 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="json">json字符串</param> /// <returns>类型数据</returns> public static T Decode <T>(string json) { try { BigintConverter bigintConverter = new BigintConverter(); return((T)JsonConvert.DeserializeObject(json, typeof(T), bigintConverter)); } catch { return(default(T)); } }