From 3001b460068e9c84ef2ce4b56735a7fe0e04ea65 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 22 Jul 2021 07:33:55 -0500 Subject: add ToJson and ToObject extensions --- src/slskd/Common/Extensions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 3b52c650..3e4f6e68 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -22,12 +22,28 @@ namespace slskd using System.IO; using System.Linq; using System.Reflection; + using System.Text.Json; /// /// Extensions. /// public static class Extensions { + /// + /// Serializes this object to json. + /// + /// The object to serialize. + /// A string containing the serialized object. + public static string ToJson(this object obj) => JsonSerializer.Serialize(obj); + + /// + /// Deserializes this string from json to an object of type . + /// + /// The type to which to deserialize the string. + /// The string to deserialize. + /// The new object deserialzied from the string. + public static T ToObject(this string str) => JsonSerializer.Deserialize(str); + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3