using System; using System.Collections.Generic; using XUtliPoolLib; public class JsonUtil { public static float ParseFloat(object val, float defVal = 0f) { return (val == null) ? defVal : float.Parse(val.ToString()); } public static int ParseInt(object val, int defVal = 0) { return (val == null) ? defVal : int.Parse(val.ToString()); } public static bool ParseBool(object val, bool defVal = false) { bool flag = val == null; bool result; if (flag) { result = defVal; } else { try { result = (JsonUtil.ParseInt(val, defVal ? 1 : 0) != 0); } catch (Exception) { try { result = bool.Parse(val.ToString()); } catch (Exception) { result = defVal; } } } return result; } public static DateTime ParseDateTime(object val) { return DateTime.Parse(val.ToString()); } public static string ReadString(IDictionary dic, string key, string defVal = "") { bool flag = dic != null && dic.ContainsKey(key); string result; if (flag) { result = ((dic[key] == null) ? defVal : dic[key].ToString()); } else { result = defVal; } return result; } public static int ReadInt(IDictionary dic, string key, int defVal = 0) { bool flag = dic != null && dic.ContainsKey(key); int result; if (flag) { result = JsonUtil.ParseInt(dic[key], defVal); } else { result = defVal; } return result; } public static bool ReadBool(IDictionary dic, string key, bool defVal = false) { bool flag = dic != null && dic.ContainsKey(key); bool result; if (flag) { result = JsonUtil.ParseBool(dic[key], defVal); } else { result = defVal; } return result; } public static float ReadFloat(IDictionary dic, string key, float defVal = 0f) { bool flag = dic != null && dic.ContainsKey(key); float result; if (flag) { result = JsonUtil.ParseFloat(dic[key], defVal); } else { result = defVal; } return result; } public static DateTime ReadDateTime(IDictionary dic, string key) { return JsonUtil.ReadDateTime(dic, key, new DateTime(1, 1, 1, 0, 0, 0)); } public static DateTime ReadDateTime(IDictionary dic, string key, DateTime defVal) { bool flag = dic != null && dic.ContainsKey(key); if (flag) { try { return JsonUtil.ParseDateTime(dic[key]); } catch (Exception ex) { XSingleton.singleton.AddErrorLog(ex.Message, null, null, null, null, null); return defVal; } } return defVal; } public static string ListToJsonStr(List list) { bool flag = list == null || list.Count < 1; string result; if (flag) { result = "[]"; } else { bool flag2 = true; string text = "["; foreach (string str in list) { bool flag3 = flag2; if (flag3) { flag2 = false; } else { text += ","; } text += str; } text += "]"; result = text; } return result; } }