|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +namespace AStar.Utilities; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// The <see cref="StringExtensions" /> class contains some useful methods to enable checks to be |
| 7 | +/// performed in a more fluid, English sentence, style. |
| 8 | +/// </summary> |
| 9 | +public static class StringExtensions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// The IsNull method, as you might expect, checks whether the string is, in fact, null. |
| 13 | + /// </summary> |
| 14 | + /// <param name="value">The string to check for being null.</param> |
| 15 | + /// <returns>True if the string is null, False otherwise.</returns> |
| 16 | + public static bool IsNull(this string? value) => value is null; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// The IsNotNull method, as you might expect, checks whether the string is not null. |
| 20 | + /// </summary> |
| 21 | + /// <param name="value">The string to check for being not null.</param> |
| 22 | + /// <returns>True if the string is not null, False otherwise.</returns> |
| 23 | + public static bool IsNotNull(this string? value) => !value.IsNull(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The IsNullOrWhiteSpace method, as you might expect, checks whether the string is, in fact, null, empty or whitespace. |
| 27 | + /// </summary> |
| 28 | + /// <param name="value">The string to check for being null, empty or whitespace.</param> |
| 29 | + /// <returns>True if the string is null, empty or whitespace, False otherwise.</returns> |
| 30 | + public static bool IsNullOrWhiteSpace(this string? value) => string.IsNullOrWhiteSpace(value); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The IsNotNullOrWhiteSpace method, as you might expect, checks whether the string is not null, empty or whitespace. |
| 34 | + /// </summary> |
| 35 | + /// <param name="value">The string to check for being not null, empty or whitespace.</param> |
| 36 | + /// <returns>True if the string is not null, empty or whitespace, False otherwise.</returns> |
| 37 | + public static bool IsNotNullOrWhiteSpace(this string? value) => !value.IsNullOrWhiteSpace(); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The FromJson method, as you might expect, converts the supplied JSON to the specified object. |
| 41 | + /// </summary> |
| 42 | + /// <typeparam name="T">The required type of the object to deserialise to.</typeparam> |
| 43 | + /// <param name="json">The JSON representation of the object.</param> |
| 44 | + /// <returns>A deserialised object based on the original JSON.</returns> |
| 45 | + public static T FromJson<T>(this string json) => JsonSerializer.Deserialize<T>(json)!; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// The FromJson method, as you might expect, converts the supplied JSON to the specified object. |
| 49 | + /// </summary> |
| 50 | + /// <typeparam name="T">The required type of the object to deserialise to.</typeparam> |
| 51 | + /// <param name="json">The JSON representation of the object.</param> |
| 52 | + /// <param name="options">Allows the specific <see href="JsonSerializerOptions">options</see> to be set to control deserialisation.</param> |
| 53 | + /// <returns>A deserialised object based on the original JSON.</returns> |
| 54 | + public static T FromJson<T>(this string json, JsonSerializerOptions options) => JsonSerializer.Deserialize<T>(json, options)!; |
| 55 | +} |
0 commit comments