aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-22 07:33:55 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-07-22 07:33:55 -0500
commit3001b460068e9c84ef2ce4b56735a7fe0e04ea65 (patch)
treeb9b3e4ee20d09fb5af3e018e755ceeb5c0fd496d /src
parentb1e25db8fbdfbef854ddaa7e1ef586bcf33e3f7f (diff)
downloadslskd-3001b460068e9c84ef2ce4b56735a7fe0e04ea65.tar.gz
add ToJson and ToObject extensions
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Extensions.cs16
1 files changed, 16 insertions, 0 deletions
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,6 +22,7 @@ namespace slskd
using System.IO;
using System.Linq;
using System.Reflection;
+ using System.Text.Json;
/// <summary>
/// Extensions.
@@ -29,6 +30,21 @@ namespace slskd
public static class Extensions
{
/// <summary>
+ /// Serializes this object to json.
+ /// </summary>
+ /// <param name="obj">The object to serialize.</param>
+ /// <returns>A string containing the serialized object.</returns>
+ public static string ToJson(this object obj) => JsonSerializer.Serialize(obj);
+
+ /// <summary>
+ /// Deserializes this string from json to an object of type <typeparamref name="T"/>.
+ /// </summary>
+ /// <typeparam name="T">The type to which to deserialize the string.</typeparam>
+ /// <param name="str">The string to deserialize.</param>
+ /// <returns>The new object deserialzied from the string.</returns>
+ public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str);
+
+ /// <summary>
/// Deeply compares this object with the specified object and returns a list of properties that are different.
/// </summary>
/// <param name="left">The left side of the comparison.</param>