You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

2 years ago
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace EC.Util.Common;
public static class JsonUtil
{
public static object ToJson(string Json)
{
object? obj = JsonConvert.DeserializeObject(Json);
return obj ?? new();
}
public static string ToJson(object obj)
{
IsoDateTimeConverter timeConverter = new() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
return JsonConvert.SerializeObject(obj, timeConverter);
}
public static string ToJson(object obj, string datetimeformats)
{
IsoDateTimeConverter timeConverter = new() { DateTimeFormat = datetimeformats };
return JsonConvert.SerializeObject(obj, timeConverter);
}
public static T ToObject<T>(string Json)
{
T? obj = JsonConvert.DeserializeObject<T>(Json);
return obj ?? Activator.CreateInstance<T>();
}
public static List<T> ToList<T>(string Json)
{
List<T>? list = JsonConvert.DeserializeObject<List<T>>(Json);
return list ?? new List<T>();
}
public static JObject ToJObject(string Json)
{
return Json != null ? JObject.Parse(Json.Replace("&nbsp;", "")) : JObject.Parse("{}");
}
}