From b30c0810b7396e5a4fb84e19f6ac27785d45a48c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 5 Nov 2021 08:52:55 -0500 Subject: add serializer options --- src/slskd/Common/Extensions.cs | 221 +++++++++++++++++++++-------------------- 1 file changed, 116 insertions(+), 105 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 4f2cf29f..30b35d14 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -23,6 +23,7 @@ namespace slskd using System.Linq; using System.Reflection; using System.Text.Json; + using System.Text.Json.Serialization; using System.Text.RegularExpressions; /// @@ -30,88 +31,6 @@ namespace slskd /// 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); - - /// - /// Recursively retrieves all properties. - /// - /// The type from which to retrieve properties. - /// The list of properties. - public static IEnumerable GetPropertiesRecursively(this Type type) - { - var props = new List(); - - foreach (var prop in type.GetProperties()) - { - if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType)) - { - props.Add(prop); - } - else - { - props.AddRange(prop.PropertyType.GetPropertiesRecursively()); - } - } - - return props; - } - - /// - /// Replaces the first occurance of in the string with . - /// - /// The string on which to perform the replacement. - /// The phrase or substring to replace. - /// The replacement string. - /// The string, with the desired phrase replaced. - public static string ReplaceFirst(this string str, string phrase, string replacement) - { - int pos = str.IndexOf(phrase); - - if (pos < 0) - { - return str; - } - - return str.Substring(0, pos) + replacement + str.Substring(pos + phrase.Length); - } - - /// - /// Determines whether the string is a valid regular expression. - /// - /// The string to validate. - /// A value indicating whether the string is a valid regular expression. - public static bool IsValidRegex(this string pattern) - { - if (string.IsNullOrWhiteSpace(pattern)) - { - return false; - } - - try - { - Regex.Match(string.Empty, pattern); - } - catch (ArgumentException) - { - return false; - } - - return true; - } - /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// @@ -171,14 +90,70 @@ namespace slskd } /// - /// Returns a "pretty" string representation of the provided Type; specifically, corrects the naming of generic Types - /// and appends the type parameters for the type to the name as it appears in the code editor. + /// Recursively retrieves all properties. /// - /// The type for which the colloquial name should be created. - /// A "pretty" string representation of the provided Type. - public static string ToColloquialString(this Type type) + /// The type from which to retrieve properties. + /// The list of properties. + public static IEnumerable GetPropertiesRecursively(this Type type) { - return !type.IsGenericType ? type.Name : type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(a => a.ToColloquialString())) + ">"; + var props = new List(); + + foreach (var prop in type.GetProperties()) + { + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType)) + { + props.Add(prop); + } + else + { + props.AddRange(prop.PropertyType.GetPropertiesRecursively()); + } + } + + return props; + } + + /// + /// Determines whether the string is a valid regular expression. + /// + /// The string to validate. + /// A value indicating whether the string is a valid regular expression. + public static bool IsValidRegex(this string pattern) + { + if (string.IsNullOrWhiteSpace(pattern)) + { + return false; + } + + try + { + Regex.Match(string.Empty, pattern); + } + catch (ArgumentException) + { + return false; + } + + return true; + } + + /// + /// Replaces the first occurance of in the string with . + /// + /// The string on which to perform the replacement. + /// The phrase or substring to replace. + /// The replacement string. + /// The string, with the desired phrase replaced. + public static string ReplaceFirst(this string str, string phrase, string replacement) + { + int pos = str.IndexOf(phrase); + + if (pos < 0) + { + return str; + } + + return str.Substring(0, pos) + replacement + str.Substring(pos + phrase.Length); } /// @@ -220,6 +195,48 @@ namespace slskd sizeSuffixes[mag]); } + /// + /// Returns a "pretty" string representation of the provided Type; specifically, corrects the naming of generic Types + /// and appends the type parameters for the type to the name as it appears in the code editor. + /// + /// The type for which the colloquial name should be created. + /// A "pretty" string representation of the provided Type. + public static string ToColloquialString(this Type type) + { + return !type.IsGenericType ? type.Name : type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(a => a.ToColloquialString())) + ">"; + } + + /// + /// 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, GetJsonSerializerOptions()); + + /// + /// Converts a fully qualified remote filename to a local filename based in the provided + /// , swapping directory characters for those specific to the local OS, removing any + /// characters that are invalid for the local OS, and making the path relative to the remote store (including the + /// filename and the parent folder). + /// + /// The fully qualified remote filename to convert. + /// The base directory for the local filename. + /// The converted filename. + public static string ToLocalFilename(this string remoteFilename, string baseDirectory) + { + return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename()); + } + + /// + /// Converts the given path to the local format (normalizes path separators). + /// + /// The path to convert. + /// The converted path. + public static string ToLocalOSPath(this string path) + { + return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); + } + /// /// Converts a fully qualified remote filename to a local filename, swapping directory characters for those specific /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the @@ -243,26 +260,20 @@ namespace slskd } /// - /// Converts a fully qualified remote filename to a local filename based in the provided , swapping directory characters for those specific - /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the - /// remote store (including the filename and the parent folder). + /// Deserializes this string from json to an object of type . /// - /// The fully qualified remote filename to convert. - /// The base directory for the local filename. - /// The converted filename. - public static string ToLocalFilename(this string remoteFilename, string baseDirectory) - { - return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename()); - } + /// 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, GetJsonSerializerOptions()); - /// - /// Converts the given path to the local format (normalizes path separators). - /// - /// The path to convert. - /// The converted path. - public static string ToLocalOSPath(this string path) + private static JsonSerializerOptions GetJsonSerializerOptions() { - return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); + var options = new JsonSerializerOptions(); + options.Converters.Add(new IPAddressConverter()); + options.Converters.Add(new JsonStringEnumConverter()); + options.IgnoreNullValues = true; + return options; } } } \ No newline at end of file -- cgit v1.2.3