diff options
author | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
commit | 6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch) | |
tree | 7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs |
+scripts
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs')
-rw-r--r-- | Client/Assets/Scripts/XUtliPoolLib/JsonUtil.cs | 164 |
1 files changed, 164 insertions, 0 deletions
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<string, object> 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<string, object> 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<string, object> 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<string, object> 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<string, object> dic, string key)
+ {
+ return JsonUtil.ReadDateTime(dic, key, new DateTime(1, 1, 1, 0, 0, 0));
+ }
+
+ public static DateTime ReadDateTime(IDictionary<string, object> dic, string key, DateTime defVal)
+ {
+ bool flag = dic != null && dic.ContainsKey(key);
+ if (flag)
+ {
+ try
+ {
+ return JsonUtil.ParseDateTime(dic[key]);
+ }
+ catch (Exception ex)
+ {
+ XSingleton<XDebug>.singleton.AddErrorLog(ex.Message, null, null, null, null, null);
+ return defVal;
+ }
+ }
+ return defVal;
+ }
+
+ public static string ListToJsonStr(List<string> 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;
+ }
+}
|