diff --git a/Projects/DLL/Newtonsoft.Json.dll b/Projects/DLL/Newtonsoft.Json.dll new file mode 100644 index 0000000..3af21d5 Binary files /dev/null and b/Projects/DLL/Newtonsoft.Json.dll differ diff --git a/Projects/DLL/SystemX.Core.dll b/Projects/DLL/SystemX.Core.dll index 85de55b..135cf62 100644 Binary files a/Projects/DLL/SystemX.Core.dll and b/Projects/DLL/SystemX.Core.dll differ diff --git a/Projects/SystemX.Core/SystemX.Core/Json/JsonUtils.cs b/Projects/SystemX.Core/SystemX.Core/Json/JsonUtils.cs new file mode 100644 index 0000000..fab37aa --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/Json/JsonUtils.cs @@ -0,0 +1,50 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SystemX.Core.Log4net; + +public static class JsonUtils +{ + public static string ToJson(this T obj) + { + string result = string.Empty; + try + { + result = JsonConvert.SerializeObject(obj, Formatting.Indented); + } + catch (Exception e) + { + Log4net.WriteLine("JsonUtils.ToJson()", LogType.Error); + Log4net.WriteLine(e); + } + + return result; + } + + public static T? ToObject(this string json) where T : class, new() + { + T? result = default(T); + try + { + result = JsonConvert.DeserializeObject(json); + } + catch(Exception e) + { + Log4net.WriteLine("JsonUtils.ToObject()", LogType.Error); + Log4net.WriteLine(e); + } + + return result; + } + + public static T? DeepCopy(this T obj) where T : class, new() + { + string originJson = obj.ToJson(); + T? clone = ToObject(originJson); + + return clone; + } +}