[성현모] Core JsonUtil 추가
This commit is contained in:
BIN
Projects/DLL/Newtonsoft.Json.dll
Normal file
BIN
Projects/DLL/Newtonsoft.Json.dll
Normal file
Binary file not shown.
Binary file not shown.
50
Projects/SystemX.Core/SystemX.Core/Json/JsonUtils.cs
Normal file
50
Projects/SystemX.Core/SystemX.Core/Json/JsonUtils.cs
Normal file
@ -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<T>(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<T>(this string json) where T : class, new()
|
||||||
|
{
|
||||||
|
T? result = default(T);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = JsonConvert.DeserializeObject<T>(json);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Log4net.WriteLine("JsonUtils.ToObject()", LogType.Error);
|
||||||
|
Log4net.WriteLine(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T? DeepCopy<T>(this T obj) where T : class, new()
|
||||||
|
{
|
||||||
|
string originJson = obj.ToJson();
|
||||||
|
T? clone = ToObject<T>(originJson);
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user