Class JsonElementExtensions
- Namespace
- Songhay.Extensions
- Assembly
- SonghayCore.dll
Extensions of JsonElement
public static class JsonElementExtensions
- Inheritance
-
JsonElementExtensions
- Inherited Members
Methods
DisplayTopProperties(JsonElement, int)
Displays top-level JsonElement properties without recursion.
public static string DisplayTopProperties(this JsonElement element, int truncationLength = 16)
Parameters
elementJsonElementthe JsonElement
truncationLengthintthe number of characters to display for each property
Returns
GetJsonChildElementOrNull(JsonElement?, string?)
Tries to return the JsonElement property of the specified JsonElement object.
public static JsonElement? GetJsonChildElementOrNull(this JsonElement? elementOrNull, string? elementName)
Parameters
elementOrNullJsonElement?The JsonElement.
elementNamestringThe JsonElement name.
Returns
GetJsonChildElementOrNull(JsonElement, string?)
Tries to return the JsonElement property of the specified JsonElement object.
public static JsonElement? GetJsonChildElementOrNull(this JsonElement element, string? elementName)
Parameters
elementJsonElementThe JsonElement.
elementNamestringThe JsonElement name.
Returns
HasProperty(JsonElement?, string?)
Returns false when the specified property name does not exist.
public static bool HasProperty(this JsonElement? elementOrNull, string? propertyName)
Parameters
elementOrNullJsonElement?the JsonElement
propertyNamestringthe property name
Returns
HasProperty(JsonElement, string?)
Returns false when the specified property name does not exist.
public static bool HasProperty(this JsonElement element, string? propertyName)
Parameters
elementJsonElementthe JsonElement
propertyNamestringthe property name
Returns
IsExpectedObject(JsonElement?, ILogger, params string[])
Determines whether the specified JsonElement has the expected properties.
public static bool IsExpectedObject(this JsonElement? elementOrNull, ILogger logger, params string[] properties)
Parameters
elementOrNullJsonElement?The node.
loggerILoggerThe logger.
propertiesstring[]The properties.
Returns
IsExpectedObject(JsonElement, ILogger, params string[])
Determines whether the specified JsonElement has the expected properties.
public static bool IsExpectedObject(this JsonElement element, ILogger logger, params string[] properties)
Parameters
elementJsonElementThe node.
loggerILoggerThe logger.
propertiesstring[]The properties.
Returns
ToBooleanOrNull(JsonElement?, bool)
Converts the specified JsonElement into a nullable bool.
public static bool? ToBooleanOrNull(this JsonElement? elementOrNull, bool supportBitStrings)
Parameters
elementOrNullJsonElement?The JsonElement.
supportBitStringsboolWhen
true, support "1" and "0" as Boolean strings.
Returns
- bool?
ToBooleanOrNull(JsonElement, bool)
Converts the specified JsonElement into a nullable bool.
public static bool? ToBooleanOrNull(this JsonElement element, bool supportBitStrings)
Parameters
elementJsonElementThe JsonElement.
supportBitStringsboolWhen
true, support "1" and "0" as Boolean strings.
Returns
- bool?
ToByteOrNull(JsonElement?)
Converts the specified JsonElement into a nullable byte.
public static byte? ToByteOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
- byte?
ToByteOrNull(JsonElement)
Converts the specified JsonElement into a nullable byte.
public static byte? ToByteOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
- byte?
ToDateTimeOrNull(JsonElement?)
Converts the specified JsonElement into a nullable DateTime.
public static DateTime? ToDateTimeOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToDateTimeOrNull(JsonElement)
Converts the specified JsonElement into a nullable bool.
public static DateTime? ToDateTimeOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
ToDecimalOrNull(JsonElement?)
Converts the specified JsonElement into a nullable bool.
public static decimal? ToDecimalOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToDecimalOrNull(JsonElement)
Converts the specified JsonElement into a nullable decimal.
public static decimal? ToDecimalOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
ToDoubleOrNull(JsonElement?)
Converts the specified JsonElement into a nullable bool.
public static double? ToDoubleOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToDoubleOrNull(JsonElement)
Converts the specified JsonElement into a nullable double.
public static double? ToDoubleOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
ToGuidOrNull(JsonElement?)
Converts the specified JsonElement into a nullable Guid.
public static Guid? ToGuidOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
- Guid?
ToGuidOrNull(JsonElement)
Converts the specified JsonElement into a nullable Guid.
public static Guid? ToGuidOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
- Guid?
ToInstanceOrNull<TObject>(JsonElement?)
Converts the specified JsonElement
to TObject.
public static TObject? ToInstanceOrNull<TObject>(this JsonElement? elementOrNull) where TObject : class
Parameters
elementOrNullJsonElement?the specified JsonElement
Returns
- TObject
Type Parameters
TObjectthe reference type to convert to
Remarks
To clearly express the intent to return value types, use ToValueTypeOrNull<T>(JsonElement?).
ToInstanceOrNull<TObject>(JsonElement)
Converts the specified JsonElement
to TObject.
public static TObject? ToInstanceOrNull<TObject>(this JsonElement element) where TObject : class
Parameters
elementJsonElementthe specified JsonElement
Returns
- TObject
Type Parameters
TObjectthe reference type to convert to
Remarks
To clearly express the intent to return value types, use ToValueTypeOrNull<T>(JsonElement).
ToIntOrNull(JsonElement?)
Converts the specified JsonElement into a nullable int.
public static int? ToIntOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
- int?
ToIntOrNull(JsonElement)
Converts the specified JsonElement into a nullable int.
public static int? ToIntOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
- int?
ToJsonElementArray(JsonElement?)
Enumerates the specified JsonElement array
public static JsonElement[] ToJsonElementArray(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToJsonElementArray(JsonElement)
Enumerates the specified JsonElement array
public static JsonElement[] ToJsonElementArray(this JsonElement elementOrNull)
Parameters
elementOrNullJsonElementThe JsonElement.
Returns
ToLongOrNull(JsonElement?)
Converts the specified JsonElement into a nullable long.
public static long? ToLongOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
- long?
ToLongOrNull(JsonElement)
Converts the specified JsonElement into a nullable long.
public static long? ToLongOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
- long?
ToReadOnlyCollection(JsonElement?)
Converts the specified JsonElement array into a collection of string values.
public static IReadOnlyCollection<string?> ToReadOnlyCollection(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToReadOnlyCollection(JsonElement)
Converts the specified JsonElement array into a collection of string values.
public static IReadOnlyCollection<string?> ToReadOnlyCollection(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
ToReadOnlyCollection<T>(JsonElement?, Func<JsonElement, T?>)
Converts the specified JsonElement array
into a collection of type T
with the specified converter.
public static IReadOnlyCollection<T?> ToReadOnlyCollection<T>(this JsonElement? elementOrNull, Func<JsonElement, T?> converter)
Parameters
elementOrNullJsonElement?The JsonElement.
converterFunc<JsonElement, T>the specified converter
Returns
Type Parameters
T
ToReadOnlyCollection<T>(JsonElement, Func<JsonElement, T?>)
Converts the specified JsonElement array
into a collection of type T
with the specified converter.
public static IReadOnlyCollection<T?> ToReadOnlyCollection<T>(this JsonElement element, Func<JsonElement, T?> converter)
Parameters
elementJsonElementThe JsonElement.
converterFunc<JsonElement, T>the specified converter
Returns
Type Parameters
T
ToShortOrNull(JsonElement?)
Converts the specified JsonElement into a nullable short.
public static short? ToShortOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToShortOrNull(JsonElement)
Converts the specified JsonElement into a nullable short.
public static short? ToShortOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
ToStringOrNull(JsonElement?)
Converts the specified JsonElement into a nullable string.
public static string? ToStringOrNull(this JsonElement? elementOrNull)
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
ToStringOrNull(JsonElement)
Converts the specified JsonElement into a nullable string.
public static string? ToStringOrNull(this JsonElement element)
Parameters
elementJsonElementThe JsonElement.
Returns
ToValueTypeOrNull<T>(JsonElement?)
Converts the specified JsonElement into a nullable string.
public static T? ToValueTypeOrNull<T>(this JsonElement? elementOrNull) where T : struct
Parameters
elementOrNullJsonElement?The JsonElement.
Returns
- T?
Type Parameters
T
Remarks
This member supports structs, including record structs.
To clearly express the intent to return reference types, use ToInstanceOrNull<TObject>(JsonElement?).
ToValueTypeOrNull<T>(JsonElement)
Converts the specified JsonElement into a nullable struct value type.
public static T? ToValueTypeOrNull<T>(this JsonElement element) where T : struct
Parameters
elementJsonElementThe JsonElement.
Returns
- T?
Type Parameters
T
Remarks
This member supports structs, including record structs.
To clearly express the intent to return reference types, use ToInstanceOrNull<TObject>(JsonElement).
BTW: I am 98% certain that Microsoft did not design generics to be used with object boxing which kind of defeats the fundamental purpose of generics. For higher performance, use methods like ToBooleanOrNull(JsonElement?, bool) instead.