diff --git a/SystemExtensions.NetStandard/Extensions/ObjectExtensions.cs b/SystemExtensions.NetStandard/Extensions/ObjectExtensions.cs index ab0594e..c8f847a 100644 --- a/SystemExtensions.NetStandard/Extensions/ObjectExtensions.cs +++ b/SystemExtensions.NetStandard/Extensions/ObjectExtensions.cs @@ -1,7 +1,25 @@ -namespace System.Extensions +using Newtonsoft.Json; + +namespace System.Extensions { public static class ObjectExtensions { + public static T Deserialize(this string serialized) + where T : class + { + if (serialized.IsNullOrWhiteSpace()) throw new ArgumentException("Provided serialized string cannot be null or whitespace", nameof(serialized)); + + return JsonConvert.DeserializeObject(serialized); + } + + public static string Serialize(this T obj) + where T : class + { + if (obj is null) throw new ArgumentNullException(nameof(obj)); + + return JsonConvert.SerializeObject(obj); + } + public static T Cast(this object obj) { return (T)obj; diff --git a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj index 7d87597..8299450 100644 --- a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj +++ b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj @@ -18,4 +18,8 @@ + + + +