diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Boolean.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Boolean.cs index 83eefeb..a219bf2 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Boolean.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Boolean.cs @@ -2,86 +2,111 @@ using Newtonsoft.Json.Linq; using Skybrud.Essentials.Strings; -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { - - internal static partial class JsonTokenUtils { - - internal static bool GetBoolean(JToken? token) { - return token?.Type switch { - JTokenType.Boolean => token.Value(), - JTokenType.Integer => token.Value() switch { - 0 => false, - 1 => true, - _ => false, - }, - JTokenType.String => StringUtils.ParseBoolean(token.Value()), +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; + +static partial class JsonTokenUtils { + + /// + /// Converts the specified into a boolean value. + /// + /// The token to be converted. + /// if matches a *truthy* value; otherwise, . + public static bool GetBoolean(JToken? token) { + return token?.Type switch { + JTokenType.Boolean => token.Value(), + JTokenType.Integer => token.Value() switch { + 0 => false, + 1 => true, _ => false, - }; - } + }, + JTokenType.String => StringUtils.ParseBoolean(token.Value()), + _ => false, + }; + } - internal static bool GetBoolean(JToken? token, bool fallback) { - return token?.Type switch { - JTokenType.Boolean => token.Value(), - JTokenType.Integer => token.Value() switch { - 0 => false, - 1 => true, - _ => fallback, - }, - JTokenType.String => StringUtils.ParseBoolean(token.Value(), fallback), + /// + /// Converts the specified into a boolean value. + /// + /// The token to be converted. + /// The fallback value to be returned if matches neither a *truthy* nor *falsy* value. + /// if matches a *truthy* value, if matches a *falsy* value. If neither, is returned instead. + public static bool GetBoolean(JToken? token, bool fallback) { + return token?.Type switch { + JTokenType.Boolean => token.Value(), + JTokenType.Integer => token.Value() switch { + 0 => false, + 1 => true, _ => fallback, - }; - } + }, + JTokenType.String => StringUtils.ParseBoolean(token.Value(), fallback), + _ => fallback, + }; + } + /// + /// Converts the specified into a boolean value. + /// + /// The token to be converted. + /// if matches a *truthy* value, if matches a *falsy* value. If neither, is returned instead. + public static bool? GetBooleanOrNull(JToken? token) { + return TryGetBoolean(token, out bool? result) ? result : null; + } - internal static bool? GetBooleanOrNull(JToken? token) { - return TryGetBoolean(token, out bool? result) ? result : null; + /// + /// Attempts to convert the specified into a boolean value. + /// + /// The token to be converted. + /// When this method returns, holds a boolean value matching either a *truthy* or *falsy* value if successful; otherwise, . + /// if matches either a *truthy* or *falsy* value; otherwise, . + public static bool TryGetBoolean(JToken? token, out bool result) { + + if (TryGetBoolean(token, out bool? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetBoolean(JToken? token, out bool result) { - - if (TryGetBoolean(token, out bool? temp)) { - result = temp.Value; - return true; - } - - result = default; - return false; - - } + result = default; + return false; - internal static bool TryGetBoolean(JToken? token, [NotNullWhen(true)] out bool? result) { + } - switch (token?.Type) { + /// + /// Attempts to convert the specified into a boolean value. + /// + /// The token to be converted. + /// When this method returns, holds a boolean value matching either a *truthy* or *falsy* value if successful; otherwise, . + /// if matches either a *truthy* or *falsy* value; otherwise, . + public static bool TryGetBoolean(JToken? token, [NotNullWhen(true)] out bool? result) { - case JTokenType.Boolean: - result = token.Value(); - return true; + switch (token?.Type) { - case JTokenType.Integer: + case JTokenType.Boolean: + result = token.Value(); + return true; - switch (token.Value()) { + case JTokenType.Integer: - case 0: - result = false; - return true; + switch (token.Value()) { - case 1: - result = true; - return true; + case 0: + result = false; + return true; - default: - result = false; - return false; + case 1: + result = true; + return true; - } + default: + result = false; + return false; - case JTokenType.String: - return StringUtils.TryParseBoolean(token.Value(), out result); + } - default: - result = false; - return false; + case JTokenType.String: + return StringUtils.TryParseBoolean(token.Value(), out result); - } + default: + result = false; + return false; } diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Double.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Double.cs index 4c9ff90..6cd7a5d 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Double.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Double.cs @@ -2,92 +2,117 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static double GetDouble(JToken? token) { - return GetDouble(token, default); - } - - internal static double GetDouble(JToken? token, double fallback) { - return TryGetDouble(token, out double? result) ? result.Value : fallback; - } - - internal static T? GetDouble(JToken? token, Func callback) { - return TryGetDouble(token, out double? result) ? callback(result.Value) : default; - } - - internal static double? GetDoubleOrNull(JToken? token) { - return TryGetDouble(token, out double? result) ? result : null; - } + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// The converted value if successful; otherwise, 0. + public static double GetDouble(JToken? token) { + return GetDouble(token, default); + } - internal static bool TryGetDouble(JToken? token, out double result) { + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted value if successful; otherwise, . + public static double GetDouble(JToken? token, double fallback) { + return TryGetDouble(token, out double? result) ? result.Value : fallback; + } - if (TryGetDouble(token, out double? temp)) { - result = temp.Value; - return true; - } + internal static T? GetDouble(JToken? token, Func callback) { + return TryGetDouble(token, out double? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// The converted value if successful; otherwise, . + public static double? GetDoubleOrNull(JToken? token) { + return TryGetDouble(token, out double? result) ? result : null; + } + /// + /// Attempts to convert the specified into a value. + /// + /// The token to be converted. + /// When this method returns, holds the converted value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetDouble(JToken? token, out double result) { + + if (TryGetDouble(token, out double? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetDouble(JToken? token, [NotNullWhen(true)] out double? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.Value() ? 1 : 0; - return true; + /// + /// Attempts to convert the specified into a value. + /// + /// The token to be converted. + /// When this method returns, holds the converted value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetDouble(JToken? token, [NotNullWhen(true)] out double? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.Value(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseDouble(token.Value(), out result); + case JTokenType.Boolean: + result = token.Value() ? 1 : 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.Value(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseDouble(token.Value(), out result); - } + default: + result = null; + return false; - internal static double[] GetDoubleArray(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToDoubleArray(), - JTokenType.Array => ConvertArrayTokenToDoubleArray(token), - _ => TryGetDouble(token, out double? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } - private static double[] ConvertArrayTokenToDoubleArray(JToken token) { + } - if (token is not JArray) return ArrayUtils.Empty(); + internal static double[] GetDoubleArray(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToDoubleArray(), + JTokenType.Array => ConvertArrayTokenToDoubleArray(token), + _ => TryGetDouble(token, out double? result) ? [result.Value] : [] + }; + } - List temp = new(); + private static double[] ConvertArrayTokenToDoubleArray(JToken token) { - foreach (JToken item in token) { - if (TryGetDouble(item, out double? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetDouble(item, out double? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Enum.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Enum.cs index 5c877fa..23dbc37 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Enum.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Enum.cs @@ -4,37 +4,55 @@ // ReSharper disable SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { - - internal static partial class JsonTokenUtils { - - public static T GetEnum(JToken? token) where T : Enum { - return token?.Type switch { - JTokenType.Integer => EnumUtils.FromInt32Internal(token.Value()), - JTokenType.Float => EnumUtils.FromInt32Internal(token.Value()), - JTokenType.String => EnumUtils.ParseEnumInternal(token.Value()), - _ => throw new EnumParseException(typeof(T), token.Value()) - }; - } - - public static T GetEnum(JToken? token, T fallback) where T : Enum { - return token?.Type switch { - JTokenType.Integer => EnumUtils.FromInt32(token.Value(), fallback), - JTokenType.Float => EnumUtils.FromInt32(token.Value(), fallback), - JTokenType.String => EnumUtils.ParseEnumInternal(token.Value(), fallback), - _ => fallback - }; - } +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; + +static partial class JsonTokenUtils { + + /// + /// Converts the specified into a corresponding enum value of type . + /// + /// The type of the enum. + /// The token to be converted. + /// The converted enum value if successful. + /// If the type is not supported, or the doesn't match a valid token value. + public static TEnum GetEnum(JToken? token) where TEnum : Enum { + return token?.Type switch { + JTokenType.Integer => EnumUtils.FromInt32Internal(token.Value()), + JTokenType.Float => EnumUtils.FromInt32Internal(token.Value()), + JTokenType.String => EnumUtils.ParseEnumInternal(token.Value()), + _ => throw new EnumParseException(typeof(TEnum), token.Value()) + }; + } - public static TEnum? GetEnumOrNull(JToken? token) where TEnum : struct, Enum { - return token?.Type switch { - JTokenType.Integer => EnumUtils.ToEnumOrNull(token.Value()), - JTokenType.Float => EnumUtils.ToEnumOrNull(token.Value()), - JTokenType.String => EnumUtils.ParseEnumOrNull(token.Value()), - _ => null - }; - } + /// + /// Converts the specified into a corresponding enum value of type . + /// + /// The type of the enum. + /// The token to be converted. + /// The fallback value to be returned if the conversion fails. + /// The converted enum value if successful; otherwise, . + public static TEnum GetEnum(JToken? token, TEnum fallback) where TEnum : Enum { + return token?.Type switch { + JTokenType.Integer => EnumUtils.FromInt32(token.Value(), fallback), + JTokenType.Float => EnumUtils.FromInt32(token.Value(), fallback), + JTokenType.String => EnumUtils.ParseEnumInternal(token.Value(), fallback), + _ => fallback + }; + } + /// + /// Converts the specified into a corresponding enum value of type . + /// + /// The type of the enum. + /// The token to be converted. + /// The converted enum value if successful; otherwise, . + public static TEnum? GetEnumOrNull(JToken? token) where TEnum : struct, Enum { + return token?.Type switch { + JTokenType.Integer => EnumUtils.ToEnumOrNull(token.Value()), + JTokenType.Float => EnumUtils.ToEnumOrNull(token.Value()), + JTokenType.String => EnumUtils.ParseEnumOrNull(token.Value()), + _ => null + }; } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Float.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Float.cs index 233fc6f..592c90b 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Float.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Float.cs @@ -2,92 +2,129 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static float GetFloat(JToken? token) { - return GetFloat(token, default); - } - - internal static float GetFloat(JToken? token, float fallback) { - return TryGetFloat(token, out float? result) ? result.Value : fallback; - } - - internal static T? GetFloat(JToken? token, Func callback) { - return TryGetFloat(token, out float? result) ? callback(result.Value) : default; - } - - internal static float? GetFloatOrNull(JToken? token) { - return TryGetFloat(token, out float? result) ? result : null; - } + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// The converted value if successful; otherwise, 0. + public static float GetFloat(JToken? token) { + return GetFloat(token, default); + } - internal static bool TryGetFloat(JToken? token, out float result) { + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted value if successful; otherwise, . + public static float GetFloat(JToken? token, float fallback) { + return TryGetFloat(token, out float? result) ? result.Value : fallback; + } - if (TryGetFloat(token, out float? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetFloat(JToken? token, Func callback) { + return TryGetFloat(token, out float? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// The converted value if successful; otherwise, . + public static float? GetFloatOrNull(JToken? token) { + return TryGetFloat(token, out float? result) ? result : null; + } + /// + /// Attempts to convert the specified into a value. + /// + /// The token to be converted. + /// When this method returns, holds the converted value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetFloat(JToken? token, out float result) { + + if (TryGetFloat(token, out float? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetFloat(JToken? token, [NotNullWhen(true)] out float? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.Value() ? 1 : 0; - return true; + /// + /// Attempts to convert the specified into a value. + /// + /// The token to be converted. + /// When this method returns, holds the converted value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetFloat(JToken? token, [NotNullWhen(true)] out float? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.Value(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseFloat(token.Value(), out result); + case JTokenType.Boolean: + result = token.Value() ? 1 : 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.Value(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseFloat(token.Value(), out result); - } + default: + result = null; + return false; - internal static float[] GetFloatArray(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToFloatArray(), - JTokenType.Array => ConvertArrayTokenToFloatArray(token), - _ => TryGetFloat(token, out float? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } - private static float[] ConvertArrayTokenToFloatArray(JToken token) { + } - if (token is not JArray) return ArrayUtils.Empty(); + /// + /// Converts the specified into an array of values. + /// + /// The token to be converted. + /// A array. + public static float[] GetFloatArray(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToFloatArray(), + JTokenType.Array => ConvertArrayTokenToFloatArray(token), + _ => TryGetFloat(token, out float? result) ? [result.Value] : [] + }; + } - List temp = new(); + private static float[] ConvertArrayTokenToFloatArray(JToken token) { - foreach (JToken item in token) { - if (TryGetFloat(item, out float? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetFloat(item, out float? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Guid.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Guid.cs index 5d017e7..5d15dc3 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Guid.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Guid.cs @@ -2,99 +2,136 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { - - internal static partial class JsonTokenUtils { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; + +static partial class JsonTokenUtils { + + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// The converted value if successful; otherwise, . + public static Guid GetGuid(JToken? token) { + return token?.Type switch { + JTokenType.Guid => token.ToObject(), + JTokenType.String => token.ToObject().ToGuid(), + _ => Guid.Empty + }; + } - internal static Guid GetGuid(JToken? token) { - return token?.Type switch { - JTokenType.Guid => token.ToObject(), - JTokenType.String => token.ToObject().ToGuid(), - _ => Guid.Empty - }; - } + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted value if successful; otherwise, . + public static Guid GetGuid(JToken? token, Guid fallback) { + return token?.Type switch { + JTokenType.Guid => token.ToObject(), + JTokenType.String => token.ToObject().ToGuid(fallback), + _ => fallback + }; + } - internal static Guid GetGuid(JToken? token, Guid fallback) { - return token?.Type switch { - JTokenType.Guid => token.ToObject(), - JTokenType.String => token.ToObject().ToGuid(fallback), - _ => fallback - }; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetGuid(JToken? token, Func callback) { + return TryGetGuid(token, out Guid? result) ? callback(result.Value) : default; + } - internal static T? GetGuid(JToken? token, Func callback) { - return TryGetGuid(token, out Guid? result) ? callback(result.Value) : default; - } + /// + /// Converts the specified into a value. + /// + /// The token to be converted. + /// The converted value if successful; otherwise, . + public static Guid? GetGuidOrNull(JToken? token) { + return TryGetGuid(token, out Guid? result) ? result : null; + } - internal static Guid? GetGuidOrNull(JToken? token) { - return TryGetGuid(token, out Guid? result) ? result : null; + /// + /// Attempts to convert the specified into a value. + /// + /// The token to be converted. + /// When this method returns, holds the converted value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetGuid(JToken? token, out Guid result) { + + if (TryGetGuid(token, out Guid? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetGuid(JToken? token, out Guid result) { + result = default; + return false; - if (TryGetGuid(token, out Guid? temp)) { - result = temp.Value; - return true; - } - - result = default; - return false; - - } - - internal static bool TryGetGuid(JToken? token, [NotNullWhen(true)] out Guid? result) { + } - switch (token?.Type) { + /// + /// Attempts to convert the specified into a value. + /// + /// The token to be converted. + /// When this method returns, holds the converted value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetGuid(JToken? token, [NotNullWhen(true)] out Guid? result) { - case JTokenType.Guid: - result = token.ToObject(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseGuid(token.ToObject(), out result); + case JTokenType.Guid: + result = token.ToObject(); + return true; - default: - result = null; - return false; + case JTokenType.String: + return StringUtils.TryParseGuid(token.ToObject(), out result); - } + default: + result = null; + return false; } - internal static Guid[] GetGuidArray(JToken? token) { + } - switch (token) { + /// + /// Converts the specified into a array. + /// + /// The token to be converted. + /// A array. + public static Guid[] GetGuidArray(JToken? token) { - case null: - return ArrayUtils.Empty(); + switch (token) { - case JArray array: + case null: + return []; - List temp = new(); + case JArray array: - foreach (JToken t in array) { + List temp = []; - // Attempt to parse the individual tokens in the array, ensuring invalid values doesn't trigger an exception - if (t != null && Guid.TryParse(t.ToString(), out Guid guid)) temp.Add(guid); + foreach (JToken t in array) { - } + // Attempt to parse the individual tokens in the array, ensuring invalid values doesn't trigger an exception + if (t != null && Guid.TryParse(t.ToString(), out Guid guid)) temp.Add(guid); - return temp.ToArray(); + } - default: + return [..temp]; - // Be friendly to other formats - return token.Type switch { - JTokenType.String => StringUtils.ParseGuidArray(token.Value()), - JTokenType.Guid => new[] { token.Value() }, - _ => ArrayUtils.Empty() - }; + default: - } + // Be friendly to other formats + return token.Type switch { + JTokenType.String => StringUtils.ParseGuidArray(token.Value()), + JTokenType.Guid => [token.Value()], + _ => [] + }; } diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int16.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int16.cs index ff20534..7ea0642 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int16.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int16.cs @@ -2,92 +2,129 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static short GetInt16(JToken? token) { - return GetInt16(token, default); - } - - internal static short GetInt16(JToken? token, short fallback) { - return TryGetInt16(token, out short? result) ? result.Value : fallback; - } - - internal static T? GetInt16(JToken? token, Func callback) { - return TryGetInt16(token, out short? result) ? callback(result.Value) : default; - } - - internal static short? GetInt16OrNull(JToken? token) { - return TryGetInt16(token, out short? result) ? result : null; - } + /// + /// Converts the specified into a 16-bit integer value. + /// + /// The token to be converted. + /// The converted 16-bit integer value if successful; otherwise, 0. + public static short GetInt16(JToken? token) { + return GetInt16(token, default); + } - internal static bool TryGetInt16(JToken? token, out short result) { + /// + /// Converts the specified into a 16-bit integer value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted 16-bit integer value if successful; otherwise, >. + public static short GetInt16(JToken? token, short fallback) { + return TryGetInt16(token, out short? result) ? result.Value : fallback; + } - if (TryGetInt16(token, out short? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetInt16(JToken? token, Func callback) { + return TryGetInt16(token, out short? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into a 16-bit integer value. + /// + /// The token to be converted. + /// The converted 16-bit integer value if successful; otherwise, . + public static short? GetInt16OrNull(JToken? token) { + return TryGetInt16(token, out short? result) ? result : null; + } + /// + /// Attempts to convert the specified into a 16-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted 16-bit integer value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetInt16(JToken? token, out short result) { + + if (TryGetInt16(token, out short? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetInt16(JToken? token, [NotNullWhen(true)] out short? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.Value() ? (short) 1 : (short) 0; - return true; + /// + /// Attempts to convert the specified into a 16-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted 16-bit integer value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetInt16(JToken? token, [NotNullWhen(true)] out short? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.Value(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseInt16(token.Value(), out result); + case JTokenType.Boolean: + result = token.Value() ? (short) 1 : (short) 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.Value(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseInt16(token.Value(), out result); - } + default: + result = null; + return false; - internal static short[] GetInt16Array(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToInt16Array(), - JTokenType.Array => ConvertArrayTokenToInt16Array(token), - _ => TryGetInt16(token, out short? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } - private static short[] ConvertArrayTokenToInt16Array(JToken token) { + } - if (token is not JArray) return ArrayUtils.Empty(); + /// + /// Converts the specified into an array of 16-bit integer values. + /// + /// The token to be converted. + /// A 16-bit integer array. + public static short[] GetInt16Array(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToInt16Array(), + JTokenType.Array => ConvertArrayTokenToInt16Array(token), + _ => TryGetInt16(token, out short? result) ? [result.Value] : [] + }; + } - List temp = new(); + private static short[] ConvertArrayTokenToInt16Array(JToken token) { - foreach (JToken item in token) { - if (TryGetInt16(item, out short? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetInt16(item, out short? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int32.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int32.cs index 1128206..acb0db5 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int32.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int32.cs @@ -2,93 +2,130 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static int GetInt32(JToken? token) { - return GetInt32(token, default); - } - - internal static int GetInt32(JToken? token, int fallback) { - return TryGetInt32(token, out int? result) ? result.Value : fallback; - } - - internal static T? GetInt32(JToken? token, Func callback) { - return TryGetInt32(token, out int? result) ? callback(result.Value) : default; - } - - internal static int? GetInt32OrNull(JToken? token) { - return TryGetInt32(token, out int? result) ? result : null; - } + /// + /// Converts the specified into a 32-bit integer value. + /// + /// The token to be converted. + /// The converted 32-bit integer value if successful; otherwise, 0. + public static int GetInt32(JToken? token) { + return GetInt32(token, default); + } - internal static bool TryGetInt32(JToken? token, out int result) { + /// + /// Converts the specified into a 32-bit integer value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted 32-bit integer value if successful; otherwise, >. + public static int GetInt32(JToken? token, int fallback) { + return TryGetInt32(token, out int? result) ? result.Value : fallback; + } - if (TryGetInt32(token, out int? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetInt32(JToken? token, Func callback) { + return TryGetInt32(token, out int? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into a 32-bit integer value. + /// + /// The token to be converted. + /// The converted 32-bit integer value if successful; otherwise, . + public static int? GetInt32OrNull(JToken? token) { + return TryGetInt32(token, out int? result) ? result : null; + } + /// + /// Attempts to convert the specified into a 32-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted 32-bit integer value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetInt32(JToken? token, out int result) { + + if (TryGetInt32(token, out int? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetInt32(JToken? token, [NotNullWhen(true)] out int? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.ToObject() ? 1 : 0; - return true; + /// + /// Attempts to convert the specified into a 32-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted 32-bit integer value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetInt32(JToken? token, [NotNullWhen(true)] out int? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.ToObject(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseInt32(token.Value(), out result); + case JTokenType.Boolean: + result = token.ToObject() ? 1 : 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.ToObject(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseInt32(token.Value(), out result); - } + default: + result = null; + return false; - internal static int[] GetInt32Array(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToInt32Array(), - JTokenType.Array => ConvertArrayTokenToInt32Array(token), - _ => TryGetInt32(token, out int? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } + } - internal static int[] ConvertArrayTokenToInt32Array(JToken token) { + /// + /// Converts the specified into an array of 32-bit integer values. + /// + /// The token to be converted. + /// A 32-bit integer array. + public static int[] GetInt32Array(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToInt32Array(), + JTokenType.Array => ConvertArrayTokenToInt32Array(token), + _ => TryGetInt32(token, out int? result) ? [result.Value] : [] + }; + } - if (token is not JArray) return ArrayUtils.Empty(); - List temp = new(); + internal static int[] ConvertArrayTokenToInt32Array(JToken token) { - foreach (JToken item in token) { - if (TryGetInt32(item, out int? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetInt32(item, out int? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int64.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int64.cs index c9cc352..738479a 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int64.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.Int64.cs @@ -2,93 +2,130 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static long GetInt64(JToken? token) { - return GetInt64(token, default); - } - - internal static long GetInt64(JToken? token, long fallback) { - return TryGetInt64(token, out long? result) ? result.Value : fallback; - } - - internal static T? GetInt64(JToken? token, Func callback) { - return TryGetInt64(token, out long? result) ? callback(result.Value) : default; - } - - internal static long? GetInt64OrNull(JToken? token) { - return TryGetInt64(token, out long? result) ? result : null; - } + /// + /// Converts the specified into a 64-bit integer value. + /// + /// The token to be converted. + /// The converted 64-bit integer value if successful; otherwise, 0. + public static long GetInt64(JToken? token) { + return GetInt64(token, default); + } - internal static bool TryGetInt64(JToken? token, out long result) { + /// + /// Converts the specified into a 64-bit integer value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted 64-bit integer value if successful; otherwise, >. + public static long GetInt64(JToken? token, long fallback) { + return TryGetInt64(token, out long? result) ? result.Value : fallback; + } - if (TryGetInt64(token, out long? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetInt64(JToken? token, Func callback) { + return TryGetInt64(token, out long? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into a 64-bit integer value. + /// + /// The token to be converted. + /// The converted 64-bit integer value if successful; otherwise, . + public static long? GetInt64OrNull(JToken? token) { + return TryGetInt64(token, out long? result) ? result : null; + } + /// + /// Attempts to convert the specified into a 64-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted 64-bit integer value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetInt64(JToken? token, out long result) { + + if (TryGetInt64(token, out long? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetInt64(JToken? token, [NotNullWhen(true)] out long? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.ToObject() ? 1 : 0; - return true; + /// + /// Attempts to convert the specified into a 64-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted 64-bit integer value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetInt64(JToken? token, [NotNullWhen(true)] out long? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.ToObject(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseInt64(token.Value(), out result); + case JTokenType.Boolean: + result = token.ToObject() ? 1 : 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.ToObject(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseInt64(token.Value(), out result); - } + default: + result = null; + return false; - internal static long[] GetInt64Array(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToInt64Array(), - JTokenType.Array => ConvertArrayTokenToInt64Array(token), - _ => TryGetInt64(token, out long? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } - internal static long[] ConvertArrayTokenToInt64Array(JToken token) { + } - if (token is not JArray) return ArrayUtils.Empty(); + /// + /// Converts the specified into an array of 64-bit integer values. + /// + /// The token to be converted. + /// A 64-bit integer array. + public static long[] GetInt64Array(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToInt64Array(), + JTokenType.Array => ConvertArrayTokenToInt64Array(token), + _ => TryGetInt64(token, out long? result) ? [result.Value] : [] + }; + } - List temp = new(); + private static long[] ConvertArrayTokenToInt64Array(JToken token) { - foreach (JToken item in token) { - if (TryGetInt64(item, out long? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetInt64(item, out long? result)) { + temp.Add(result.Value); + } } + return [..temp]; } + } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.String.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.String.cs index d815292..e34481c 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.String.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.String.cs @@ -3,88 +3,108 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings.Extensions; using Skybrud.Essentials.Time.Iso8601; // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - #region System.String + #region System.String - internal static string? GetString(JToken? token) { - return TryGetString(token, out string? result) ? result : null; - } + /// + /// Converts the specified into a string value. + /// + /// The token to be converted. + /// The converted string value if successful; otherwise, . + public static string? GetString(JToken? token) { + return TryGetString(token, out string? result) ? result : null; + } - internal static T? GetString(JToken? token, Func callback) { - return TryGetString(token, out string? result) ? callback(result) : default; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a string value into an instance of . + /// An instance of if successful; otherwise, . + public static T? GetString(JToken? token, Func callback) { + return TryGetString(token, out string? result) ? callback(result) : default; + } - internal static bool TryGetString(JToken? token, [NotNullWhen(true)] out string? result) { - - switch (token?.Type) { - - case JTokenType.Boolean: - case JTokenType.Integer: - case JTokenType.Float: - case JTokenType.Guid: - result = string.Format(CultureInfo.InvariantCulture, "{0}", token); - return true; - - case JTokenType.Date: - switch (token.ToObject()) { - case DateTime dt: - result = dt.ToString(Iso8601Constants.DateTimeMilliseconds); - return true; - case DateTimeOffset dto: - result = dto.ToString(Iso8601Constants.DateTimeMilliseconds); - return true; - default: - result = null; - return false; - } - - case JTokenType.String: - result = token.Value(); - return true; - - default: - result = null; - return false; + /// + /// Attempts to convert the specified into a string value. + /// + /// The token to be converted. + /// When the method returns, holds the string value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetString(JToken? token, [NotNullWhen(true)] out string? result) { + + switch (token?.Type) { + + case JTokenType.Boolean: + case JTokenType.Integer: + case JTokenType.Float: + case JTokenType.Guid: + result = string.Format(CultureInfo.InvariantCulture, "{0}", token); + return true; + + case JTokenType.Date: + switch (token.ToObject()) { + case DateTime dt: + result = dt.ToString(Iso8601Constants.DateTimeMilliseconds); + return true; + case DateTimeOffset dto: + result = dto.ToString(Iso8601Constants.DateTimeMilliseconds); + return true; + default: + result = null; + return false; + } - } + case JTokenType.String: + result = token.Value(); + return true; - } + default: + result = null; + return false; - internal static string[] GetStringArray(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToStringArray(), - JTokenType.Array => ConvertArrayTokenToStringArray(token), - _ => TryGetString(token, out string? result) ? new[] { result } : ArrayUtils.Empty() - }; } - internal static string[] ConvertArrayTokenToStringArray(JToken token) { + } - if (token is not JArray) return ArrayUtils.Empty(); + /// + /// Converts the specified into a string array. + /// + /// The token to be converted. + /// A string array. + public static string[] GetStringArray(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToStringArray(), + JTokenType.Array => ConvertArrayTokenToStringArray(token), + _ => TryGetString(token, out string? result) ? [result] : [] + }; + } - List temp = new(); + internal static string[] ConvertArrayTokenToStringArray(JToken token) { - foreach (JToken item in token) { - if (TryGetString(item, out string? result)) { - temp.Add(result); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetString(item, out string? result)) { + temp.Add(result); + } } - #endregion + return [..temp]; } + #endregion + } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt16.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt16.cs index 1ce5136..dad59b4 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt16.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt16.cs @@ -2,94 +2,131 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static ushort GetUInt16(JToken? token) { - return GetUInt16(token, default); - } - - internal static ushort GetUInt16(JToken? token, ushort fallback) { - return TryGetUInt16(token, out ushort? result) ? result.Value : fallback; - } - - internal static T? GetUInt16(JToken? token, Func callback) { - return TryGetUInt16(token, out ushort? result) ? callback(result.Value) : default; - } - - internal static ushort? GetUInt16OrNull(JToken? token) { - return TryGetUInt16(token, out ushort? result) ? result : null; - } + /// + /// Converts the specified into an unsigned 16-bit integer value. + /// + /// The token to be converted. + /// The converted unsigned 16-bit integer value if successful; otherwise, 0. + public static ushort GetUInt16(JToken? token) { + return GetUInt16(token, default); + } - internal static bool TryGetUInt16(JToken? token, out ushort result) { + /// + /// Converts the specified into an unsigned 16-bit integer value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted unsigned 16-bit integer value if successful; otherwise, >. + public static ushort GetUInt16(JToken? token, ushort fallback) { + return TryGetUInt16(token, out ushort? result) ? result.Value : fallback; + } - if (TryGetUInt16(token, out ushort? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetUInt16(JToken? token, Func callback) { + return TryGetUInt16(token, out ushort? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into an unsigned 16-bit integer value. + /// + /// The token to be converted. + /// The converted unsigned 16-bit integer value if successful; otherwise, . + public static ushort? GetUInt16OrNull(JToken? token) { + return TryGetUInt16(token, out ushort? result) ? result : null; + } + /// + /// Attempts to convert the specified into an unsigned 16-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted unsigned 16-bit integer value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetUInt16(JToken? token, out ushort result) { + + if (TryGetUInt16(token, out ushort? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetUInt16(JToken? token, [NotNullWhen(true)] out ushort? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.Value() ? (ushort) 1 : (ushort) 0; - return true; + /// + /// Attempts to convert the specified into an unsigned 16-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted unsigned 16-bit integer value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetUInt16(JToken? token, [NotNullWhen(true)] out ushort? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.ToObject(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseUInt16(token.Value(), out result); + case JTokenType.Boolean: + result = token.Value() ? (ushort) 1 : (ushort) 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.ToObject(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseUInt16(token.Value(), out result); - } + default: + result = null; + return false; - internal static ushort[] GetUInt16Array(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToUInt16Array(), - JTokenType.Array => ConvertArrayTokenToUInt16Array(token), - _ => TryGetUInt16(token, out ushort? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } + } - private static ushort[] ConvertArrayTokenToUInt16Array(JToken token) { + /// + /// Converts the specified into an array of unsigned 16-bit integer values. + /// + /// The token to be converted. + /// An unsigned 16-bit integer array. + public static ushort[] GetUInt16Array(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToUInt16Array(), + JTokenType.Array => ConvertArrayTokenToUInt16Array(token), + _ => TryGetUInt16(token, out ushort? result) ? [result.Value] : [] + }; + } - if (token is not JArray) return ArrayUtils.Empty(); - List temp = new(); + private static ushort[] ConvertArrayTokenToUInt16Array(JToken token) { - foreach (JToken item in token) { - if (TryGetUInt16(item, out ushort? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetUInt16(item, out ushort? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt32.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt32.cs index 637fa93..ebd2c75 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt32.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt32.cs @@ -2,94 +2,131 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static uint GetUInt32(JToken? token) { - return GetUInt32(token, default); - } - - internal static uint GetUInt32(JToken? token, uint fallback) { - return TryGetUInt32(token, out uint? result) ? result.Value : fallback; - } - - internal static T? GetUInt32(JToken? token, Func callback) { - return TryGetUInt32(token, out uint? result) ? callback(result.Value) : default; - } - - internal static uint? GetUInt32OrNull(JToken? token) { - return TryGetUInt32(token, out uint? result) ? result : null; - } + /// + /// Converts the specified into an unsigned 32-bit integer value. + /// + /// The token to be converted. + /// The converted unsigned 32-bit integer value if successful; otherwise, 0. + public static uint GetUInt32(JToken? token) { + return GetUInt32(token, default); + } - internal static bool TryGetUInt32(JToken? token, out uint result) { + /// + /// Converts the specified into an unsigned 32-bit integer value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted unsigned 32-bit integer value if successful; otherwise, >. + public static uint GetUInt32(JToken? token, uint fallback) { + return TryGetUInt32(token, out uint? result) ? result.Value : fallback; + } - if (TryGetUInt32(token, out uint? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetUInt32(JToken? token, Func callback) { + return TryGetUInt32(token, out uint? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into an unsigned 32-bit integer value. + /// + /// The token to be converted. + /// The converted unsigned 32-bit integer value if successful; otherwise, . + public static uint? GetUInt32OrNull(JToken? token) { + return TryGetUInt32(token, out uint? result) ? result : null; + } + /// + /// Attempts to convert the specified into an unsigned 32-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted unsigned 32-bit integer value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetUInt32(JToken? token, out uint result) { + + if (TryGetUInt32(token, out uint? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetUInt32(JToken? token, [NotNullWhen(true)] out uint? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.Value() ? 1 : (uint) 0; - return true; + /// + /// Attempts to convert the specified into an unsigned 32-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted unsigned 32-bit integer value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetUInt32(JToken? token, [NotNullWhen(true)] out uint? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.ToObject(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseUInt32(token.Value(), out result); + case JTokenType.Boolean: + result = token.Value() ? 1 : (uint) 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.ToObject(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseUInt32(token.Value(), out result); - } + default: + result = null; + return false; - internal static uint[] GetUInt32Array(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToUInt32Array(), - JTokenType.Array => ConvertArrayTokenToUInt32Array(token), - _ => TryGetUInt32(token, out uint? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } + } - private static uint[] ConvertArrayTokenToUInt32Array(JToken token) { + /// + /// Converts the specified into an array of unsigned 32-bit integer values. + /// + /// The token to be converted. + /// An unsigned 32-bit integer array. + public static uint[] GetUInt32Array(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToUInt32Array(), + JTokenType.Array => ConvertArrayTokenToUInt32Array(token), + _ => TryGetUInt32(token, out uint? result) ? [result.Value] : [] + }; + } - if (token is not JArray) return ArrayUtils.Empty(); - List temp = new(); + private static uint[] ConvertArrayTokenToUInt32Array(JToken token) { - foreach (JToken item in token) { - if (TryGetUInt32(item, out uint? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetUInt32(item, out uint? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt64.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt64.cs index 83ad00e..e291960 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt64.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.UInt64.cs @@ -2,94 +2,131 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; using Skybrud.Essentials.Strings; using Skybrud.Essentials.Strings.Extensions; // ReSharper disable SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +static partial class JsonTokenUtils { - internal static ulong GetUInt64(JToken? token) { - return GetUInt64(token, default); - } - - internal static ulong GetUInt64(JToken? token, ulong fallback) { - return TryGetUInt64(token, out ulong? result) ? result.Value : fallback; - } - - internal static T? GetUInt64(JToken? token, Func callback) { - return TryGetUInt64(token, out ulong? result) ? callback(result.Value) : default; - } - - internal static ulong? GetUInt64OrNull(JToken? token) { - return TryGetUInt64(token, out ulong? result) ? result : null; - } + /// + /// Converts the specified into an unsigned 64-bit integer value. + /// + /// The token to be converted. + /// The converted unsigned 64-bit integer value if successful; otherwise, 0. + public static ulong GetUInt64(JToken? token) { + return GetUInt64(token, default); + } - internal static bool TryGetUInt64(JToken? token, out ulong result) { + /// + /// Converts the specified into an unsigned 64-bit integer value. + /// + /// The token to be converted. + /// A fallback value to be returned if the conversion fails. + /// The converted unsigned 64-bit integer value if successful; otherwise, >. + public static ulong GetUInt64(JToken? token, ulong fallback) { + return TryGetUInt64(token, out ulong? result) ? result.Value : fallback; + } - if (TryGetUInt64(token, out ulong? temp)) { - result = temp.Value; - return true; - } + /// + /// Converts the specified into an instance of . + /// + /// The type of the result of the conversion. + /// The token to be converted. + /// A callback function used for converting a value into an instance of . + /// An instance of if successful; otherwise, the default value of . + public static T? GetUInt64(JToken? token, Func callback) { + return TryGetUInt64(token, out ulong? result) ? callback(result.Value) : default; + } - result = default; - return false; + /// + /// Converts the specified into an unsigned 64-bit integer value. + /// + /// The token to be converted. + /// The converted unsigned 64-bit integer value if successful; otherwise, . + public static ulong? GetUInt64OrNull(JToken? token) { + return TryGetUInt64(token, out ulong? result) ? result : null; + } + /// + /// Attempts to convert the specified into an unsigned 64-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted unsigned 64-bit integer value if successful; otherwise, 0. + /// if the conversion was successful; otherwise, . + public static bool TryGetUInt64(JToken? token, out ulong result) { + + if (TryGetUInt64(token, out ulong? temp)) { + result = temp.Value; + return true; } - internal static bool TryGetUInt64(JToken? token, [NotNullWhen(true)] out ulong? result) { + result = default; + return false; - switch (token?.Type) { + } - case JTokenType.Boolean: - result = token.Value() ? 1 : (ulong) 0; - return true; + /// + /// Attempts to convert the specified into an unsigned 64-bit integer value. + /// + /// The token to be converted. + /// When this method returns, holds the converted unsigned 64-bit integer value if successful; otherwise, . + /// if the conversion was successful; otherwise, . + public static bool TryGetUInt64(JToken? token, [NotNullWhen(true)] out ulong? result) { - case JTokenType.Integer: - case JTokenType.Float: - result = token.ToObject(); - return true; + switch (token?.Type) { - case JTokenType.String: - return StringUtils.TryParseUInt64(token.Value(), out result); + case JTokenType.Boolean: + result = token.Value() ? 1 : (ulong) 0; + return true; - default: - result = null; - return false; + case JTokenType.Integer: + case JTokenType.Float: + result = token.ToObject(); + return true; - } + case JTokenType.String: + return StringUtils.TryParseUInt64(token.Value(), out result); - } + default: + result = null; + return false; - internal static ulong[] GetUInt64Array(JToken? token) { - return token?.Type switch { - JTokenType.String => token.Value().ToUInt64Array(), - JTokenType.Array => ConvertArrayTokenToUInt64Array(token), - _ => TryGetUInt64(token, out ulong? result) ? new[] { result.Value } : ArrayUtils.Empty() - }; } + } - private static ulong[] ConvertArrayTokenToUInt64Array(JToken token) { + /// + /// Converts the specified into an array of unsigned 64-bit integer values. + /// + /// The token to be converted. + /// An unsigned 64-bit integer array. + public static ulong[] GetUInt64Array(JToken? token) { + return token?.Type switch { + JTokenType.String => token.Value().ToUInt64Array(), + JTokenType.Array => ConvertArrayTokenToUInt64Array(token), + _ => TryGetUInt64(token, out ulong? result) ? [result.Value] : [] + }; + } - if (token is not JArray) return ArrayUtils.Empty(); - List temp = new(); + private static ulong[] ConvertArrayTokenToUInt64Array(JToken token) { - foreach (JToken item in token) { - if (TryGetUInt64(item, out ulong? result)) { - temp.Add(result.Value); - } - } + if (token is not JArray) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in token) { + if (TryGetUInt64(item, out ulong? result)) { + temp.Add(result.Value); + } } + return [..temp]; + } } \ No newline at end of file diff --git a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.cs b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.cs index b41a8ca..b98d40b 100644 --- a/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.cs +++ b/src/Skybrud.Essentials/Json/Newtonsoft/Parsing/JsonTokenUtils.cs @@ -1,60 +1,76 @@ using System; using System.Collections.Generic; using Newtonsoft.Json.Linq; -using Skybrud.Essentials.Collections; // ReSharper disable LoopCanBeConvertedToQuery // ReSharper disable SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault -namespace Skybrud.Essentials.Json.Newtonsoft.Parsing { +namespace Skybrud.Essentials.Json.Newtonsoft.Parsing; - internal static partial class JsonTokenUtils { +/// +/// Static class with various methods for parsing instances of . +/// +public static partial class JsonTokenUtils { - internal static T[]? ConvertTokenToArray(JToken? token, Func callback) { + /// + /// Converts the specified into an array of . + /// + /// The item type of the array. + /// The token to be converted. + /// A callback function used for converting each child token into an instance of . + /// An array of if is an array; otherwise, . + public static T[]? ConvertTokenToArray(JToken? token, Func callback) { - if (token is not JArray array) return null; - if (array.Count == 0) return ArrayUtils.Empty(); + if (token is not JArray array) return null; + if (array.Count == 0) return []; - List temp = new(); - - foreach (JToken item in array) { - temp.Add(callback(item)); - } - - return temp.ToArray(); + List temp = []; + foreach (JToken item in array) { + temp.Add(callback(item)); } - internal static T[]? ConvertTokenToArray(JToken? token, Func callback) { + return [..temp]; - if (token is not JArray array) return null; - if (array.Count == 0) return ArrayUtils.Empty(); + } - List temp = new(); + /// + /// Converts the specified into an array of . + /// + /// The item type of the array. + /// The token to be converted. + /// A callback function used for converting each child object into an instance of . + /// An array of if is an array; otherwise, . + public static T[]? ConvertTokenToArray(JToken? token, Func callback) { - foreach (JToken item in array) { - if (item is JObject obj) temp.Add(callback(obj)); - } + if (token is not JArray array) return null; + if (array.Count == 0) return []; - return temp.ToArray(); + List temp = []; + foreach (JToken item in array) { + if (item is JObject obj) temp.Add(callback(obj)); } - internal static IReadOnlyList ConvertTokenToReadOnlyList(JToken? token, Func callback) { + return [..temp]; - if (token is not JArray array || array.Count == 0) return ArrayUtils.Empty(); + } + + internal static IReadOnlyList ConvertTokenToReadOnlyList(JToken? token, Func callback) { - List temp = new(); + // TODO: should this return null if token is not an array? - foreach (JToken item in array) { - if (item is JObject obj) temp.Add(callback(obj)); - } + if (token is not JArray array || array.Count == 0) return []; - return temp; + List temp = []; + foreach (JToken item in array) { + if (item is JObject obj) temp.Add(callback(obj)); } + return temp; + } } \ No newline at end of file