[성현모] Core JsonUtil 추가

This commit is contained in:
SHM
2025-04-18 10:05:45 +09:00
parent babdb4732b
commit 1bfc56f437
3 changed files with 50 additions and 0 deletions

View 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;
}
}