Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made JsonTokenUtils methods public #48

Open
wants to merge 3 commits into
base: v1/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(),
JTokenType.Integer => token.Value<int>() switch {
0 => false,
1 => true,
_ => false,
},
JTokenType.String => StringUtils.ParseBoolean(token.Value<string>()),
namespace Skybrud.Essentials.Json.Newtonsoft.Parsing;

static partial class JsonTokenUtils {

/// <summary>
/// Converts the specified <paramref name="token"/> into a boolean value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <returns><see langword="true"/> if <paramref name="token"/> matches a *truthy* value; otherwise, <see langword="false"/>.</returns>
public static bool GetBoolean(JToken? token) {
return token?.Type switch {
JTokenType.Boolean => token.Value<bool>(),
JTokenType.Integer => token.Value<int>() switch {
0 => false,
1 => true,
_ => false,
};
}
},
JTokenType.String => StringUtils.ParseBoolean(token.Value<string>()),
_ => false,
};
}

internal static bool GetBoolean(JToken? token, bool fallback) {
return token?.Type switch {
JTokenType.Boolean => token.Value<bool>(),
JTokenType.Integer => token.Value<int>() switch {
0 => false,
1 => true,
_ => fallback,
},
JTokenType.String => StringUtils.ParseBoolean(token.Value<string>(), fallback),
/// <summary>
/// Converts the specified <paramref name="token"/> into a boolean value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <param name="fallback">The fallback value to be returned if <paramref name="token"/> matches neither a *truthy* nor *falsy* value.</param>
/// <returns><see langword="true"/> if <paramref name="token"/> matches a *truthy* value, <see langword="false"/> if <paramref name="token"/> matches a *falsy* value. If neither, <paramref name="fallback"/> is returned instead.</returns>
public static bool GetBoolean(JToken? token, bool fallback) {
return token?.Type switch {
JTokenType.Boolean => token.Value<bool>(),
JTokenType.Integer => token.Value<int>() switch {
0 => false,
1 => true,
_ => fallback,
};
}
},
JTokenType.String => StringUtils.ParseBoolean(token.Value<string>(), fallback),
_ => fallback,
};
}
/// <summary>
/// Converts the specified <paramref name="token"/> into a boolean value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <returns><see langword="true"/> if <paramref name="token"/> matches a *truthy* value, <see langword="false"/> if <paramref name="token"/> matches a *falsy* value. If neither, <see langword="null"/> is returned instead.</returns>
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;
/// <summary>
/// Attempts to convert the specified <paramref name="token"/> into a boolean value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <param name="result">When this method returns, holds a boolean value matching either a *truthy* or *falsy* value if successful; otherwise, <see langword="false"/>.</param>
/// <returns><see langword="true"/> if <paramref name="token"/> matches either a *truthy* or *falsy* value; otherwise, <see langword="false"/>.</returns>
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) {
/// <summary>
/// Attempts to convert the specified <paramref name="token"/> into a boolean value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <param name="result">When this method returns, holds a boolean value matching either a *truthy* or *falsy* value if successful; otherwise, <see langword="null"/>.</param>
/// <returns><see langword="true"/> if <paramref name="token"/> matches either a *truthy* or *falsy* value; otherwise, <see langword="false"/>.</returns>
public static bool TryGetBoolean(JToken? token, [NotNullWhen(true)] out bool? result) {

case JTokenType.Boolean:
result = token.Value<bool>();
return true;
switch (token?.Type) {

case JTokenType.Integer:
case JTokenType.Boolean:
result = token.Value<bool>();
return true;

switch (token.Value<int>()) {
case JTokenType.Integer:

case 0:
result = false;
return true;
switch (token.Value<int>()) {

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<string>(), out result);
}

default:
result = false;
return false;
case JTokenType.String:
return StringUtils.TryParseBoolean(token.Value<string>(), out result);

}
default:
result = false;
return false;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(JToken? token, Func<double, T> 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;
}
/// <summary>
/// Converts the specified <paramref name="token"/> into a <see cref="double"/> value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <returns>The converted <see cref="double"/> value if successful; otherwise, <c>0</c>.</returns>
public static double GetDouble(JToken? token) {
return GetDouble(token, default);
}

internal static bool TryGetDouble(JToken? token, out double result) {
/// <summary>
/// Converts the specified <paramref name="token"/> into a <see cref="double"/> value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <param name="fallback">A fallback value to be returned if the conversion fails.</param>
/// <returns>The converted <see cref="double"/> value if successful; otherwise, <paramref name="fallback"/>.</returns>
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<T>(JToken? token, Func<double, T> callback) {
return TryGetDouble(token, out double? result) ? callback(result.Value) : default;
}

result = default;
return false;
/// <summary>
/// Converts the specified <paramref name="token"/> into a <see cref="double"/> value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <returns>The converted <see cref="double"/> value if successful; otherwise, <see langword="null"/>.</returns>
public static double? GetDoubleOrNull(JToken? token) {
return TryGetDouble(token, out double? result) ? result : null;
}

/// <summary>
/// Attempts to convert the specified <paramref name="token"/> into a <see cref="double"/> value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <param name="result">When this method returns, holds the converted <see cref="double"/> value if successful; otherwise, <c>0</c>.</param>
/// <returns><see langword="true"/> if the conversion was successful; otherwise, <see langword="false"/>.</returns>
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<bool>() ? 1 : 0;
return true;
/// <summary>
/// Attempts to convert the specified <paramref name="token"/> into a <see cref="double"/> value.
/// </summary>
/// <param name="token">The token to be converted.</param>
/// <param name="result">When this method returns, holds the converted <see cref="double"/> value if successful; otherwise, <see langword="null"/>.</param>
/// <returns><see langword="true"/> if the conversion was successful; otherwise, <see langword="false"/>.</returns>
public static bool TryGetDouble(JToken? token, [NotNullWhen(true)] out double? result) {

case JTokenType.Integer:
case JTokenType.Float:
result = token.Value<double>();
return true;
switch (token?.Type) {

case JTokenType.String:
return StringUtils.TryParseDouble(token.Value<string>(), out result);
case JTokenType.Boolean:
result = token.Value<bool>() ? 1 : 0;
return true;

default:
result = null;
return false;
case JTokenType.Integer:
case JTokenType.Float:
result = token.Value<double>();
return true;

}
case JTokenType.String:
return StringUtils.TryParseDouble(token.Value<string>(), out result);

}
default:
result = null;
return false;

internal static double[] GetDoubleArray(JToken? token) {
return token?.Type switch {
JTokenType.String => token.Value<string>().ToDoubleArray(),
JTokenType.Array => ConvertArrayTokenToDoubleArray(token),
_ => TryGetDouble(token, out double? result) ? new[] { result.Value } : ArrayUtils.Empty<double>()
};
}

private static double[] ConvertArrayTokenToDoubleArray(JToken token) {
}

if (token is not JArray) return ArrayUtils.Empty<double>();
internal static double[] GetDoubleArray(JToken? token) {
return token?.Type switch {
JTokenType.String => token.Value<string>().ToDoubleArray(),
JTokenType.Array => ConvertArrayTokenToDoubleArray(token),
_ => TryGetDouble(token, out double? result) ? [result.Value] : []
};
}

List<double> 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<double> temp = [];

foreach (JToken item in token) {
if (TryGetDouble(item, out double? result)) {
temp.Add(result.Value);
}
}

return [..temp];

}

}
Loading