From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs | 164 +++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs (limited to 'Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs') diff --git a/Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs b/Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs new file mode 100644 index 00000000..e940ea78 --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs @@ -0,0 +1,164 @@ +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; + } +} -- cgit v1.1-26-g67d0