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
element
JsonElementthe JsonElement
truncationLength
intthe number of characters to display for each property
Returns
GetJsonPropertyOrNull(JsonElement?, string?)
Tries to return the JsonElement property of the specified JsonElement object.
public static JsonElement? GetJsonPropertyOrNull(this JsonElement? elementOrNull, string? elementName)
Parameters
elementOrNull
JsonElement?The JsonElement.
elementName
stringThe JsonElement name.
Returns
GetJsonPropertyOrNull(JsonElement, string?)
Tries to return the JsonElement property of the specified JsonElement object.
public static JsonElement? GetJsonPropertyOrNull(this JsonElement element, string? elementName)
Parameters
element
JsonElementThe JsonElement.
elementName
stringThe 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
elementOrNull
JsonElement?the JsonElement
propertyName
stringthe 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
element
JsonElementthe JsonElement
propertyName
stringthe 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
elementOrNull
JsonElement?The node.
logger
ILoggerThe logger.
properties
string[]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
element
JsonElementThe node.
logger
ILoggerThe logger.
properties
string[]The properties.
Returns
ToBoolean(JsonElement?, bool)
Converts the specified JsonElement into a nullable bool.
public static bool? ToBoolean(this JsonElement? elementOrNull, bool supportBitStrings)
Parameters
elementOrNull
JsonElement?The JsonElement.
supportBitStrings
boolWhen
true
, support "1" and "0" as Boolean strings.
Returns
- bool?
ToBoolean(JsonElement, bool)
Converts the specified JsonElement into a nullable bool.
public static bool? ToBoolean(this JsonElement element, bool supportBitStrings)
Parameters
element
JsonElementThe JsonElement.
supportBitStrings
boolWhen
true
, support "1" and "0" as Boolean strings.
Returns
- bool?
ToByte(JsonElement?)
Converts the specified JsonElement into a nullable byte.
public static byte? ToByte(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
- byte?
ToByte(JsonElement)
Converts the specified JsonElement into a nullable byte.
public static byte? ToByte(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
- byte?
ToDateTime(JsonElement?)
Converts the specified JsonElement into a nullable DateTime.
public static DateTime? ToDateTime(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
ToDateTime(JsonElement)
Converts the specified JsonElement into a nullable bool.
public static DateTime? ToDateTime(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
ToDecimal(JsonElement?)
Converts the specified JsonElement into a nullable bool.
public static decimal? ToDecimal(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
ToDecimal(JsonElement)
Converts the specified JsonElement into a nullable decimal.
public static decimal? ToDecimal(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
ToDouble(JsonElement?)
Converts the specified JsonElement into a nullable bool.
public static double? ToDouble(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
ToDouble(JsonElement)
Converts the specified JsonElement into a nullable double.
public static double? ToDouble(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
ToGuid(JsonElement?)
Converts the specified JsonElement into a nullable Guid.
public static Guid? ToGuid(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
- Guid?
ToGuid(JsonElement)
Converts the specified JsonElement into a nullable Guid.
public static Guid? ToGuid(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
- Guid?
ToInt(JsonElement?)
Converts the specified JsonElement into a nullable int.
public static int? ToInt(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
- int?
ToInt(JsonElement)
Converts the specified JsonElement into a nullable int.
public static int? ToInt(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
- int?
ToJsonElementArray(JsonElement?)
Enumerates the specified JsonElement array
public static JsonElement[] ToJsonElementArray(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
ToJsonElementArray(JsonElement)
Enumerates the specified JsonElement array
public static JsonElement[] ToJsonElementArray(this JsonElement elementOrNull)
Parameters
elementOrNull
JsonElementThe JsonElement.
Returns
ToLong(JsonElement?)
Converts the specified JsonElement into a nullable long.
public static long? ToLong(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
- long?
ToLong(JsonElement)
Converts the specified JsonElement into a nullable long.
public static long? ToLong(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.
Returns
- long?
ToObject<TObject>(JsonElement?)
Converts the specified JsonElement
to TObject
.
public static TObject? ToObject<TObject>(this JsonElement? elementOrNull) where TObject : class
Parameters
elementOrNull
JsonElement?the specified JsonElement
Returns
- TObject
Type Parameters
TObject
the type to convert to
Remarks
To return value types, use ToScalarValue<T>(JsonElement?).
ToObject<TObject>(JsonElement)
Converts the specified JsonElement
to TObject
.
public static TObject? ToObject<TObject>(this JsonElement element) where TObject : class
Parameters
element
JsonElementthe specified JsonElement
Returns
- TObject
Type Parameters
TObject
the type to convert to
Remarks
To return value types, use ToScalarValue<T>(JsonElement).
ToReadOnlyCollection(JsonElement?)
Converts the specified JsonElement array into a collection of string values.
public static IReadOnlyCollection<string?> ToReadOnlyCollection(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?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
element
JsonElementThe 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
elementOrNull
JsonElement?The JsonElement.
converter
Func<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
element
JsonElementThe JsonElement.
converter
Func<JsonElement, T>the specified converter
Returns
Type Parameters
T
ToScalarValue<T>(JsonElement?)
Converts the specified JsonElement into a nullable string.
public static T? ToScalarValue<T>(this JsonElement? elementOrNull) where T : struct
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
- T?
Type Parameters
T
Remarks
To de-serialize a class, use ToObject<TObject>(JsonElement?).
ToScalarValue<T>(JsonElement)
Converts the specified JsonElement into a nullable string.
public static T? ToScalarValue<T>(this JsonElement element) where T : struct
Parameters
element
JsonElementThe JsonElement.
Returns
- T?
Type Parameters
T
Remarks
To de-serialize a class, use ToObject<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, their use with generic collections. For higher performance, use methods like ToBoolean(JsonElement?, bool) or ToDecimal(JsonElement) instead.
ToStringValue(JsonElement?)
Converts the specified JsonElement into a nullable string.
public static string? ToStringValue(this JsonElement? elementOrNull)
Parameters
elementOrNull
JsonElement?The JsonElement.
Returns
ToStringValue(JsonElement)
Converts the specified JsonElement into a nullable string.
public static string? ToStringValue(this JsonElement element)
Parameters
element
JsonElementThe JsonElement.