Table of Contents

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 JsonElement

the JsonElement

truncationLength int

the number of characters to display for each property

Returns

string

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 string

The JsonElement name.

Returns

JsonElement?

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 JsonElement

The JsonElement.

elementName string

The JsonElement name.

Returns

JsonElement?

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 string

the property name

Returns

bool

HasProperty(JsonElement, string?)

Returns false when the specified property name does not exist.

public static bool HasProperty(this JsonElement element, string? propertyName)

Parameters

element JsonElement

the JsonElement

propertyName string

the property name

Returns

bool

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 ILogger

The logger.

properties string[]

The properties.

Returns

bool

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 JsonElement

The node.

logger ILogger

The logger.

properties string[]

The properties.

Returns

bool

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 bool

When 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 JsonElement

The JsonElement.

supportBitStrings bool

When 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 JsonElement

The 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

DateTime?

ToDateTime(JsonElement)

Converts the specified JsonElement into a nullable bool.

public static DateTime? ToDateTime(this JsonElement element)

Parameters

element JsonElement

The JsonElement.

Returns

DateTime?

ToDecimal(JsonElement?)

Converts the specified JsonElement into a nullable bool.

public static decimal? ToDecimal(this JsonElement? elementOrNull)

Parameters

elementOrNull JsonElement?

The JsonElement.

Returns

decimal?

ToDecimal(JsonElement)

Converts the specified JsonElement into a nullable decimal.

public static decimal? ToDecimal(this JsonElement element)

Parameters

element JsonElement

The JsonElement.

Returns

decimal?

ToDouble(JsonElement?)

Converts the specified JsonElement into a nullable bool.

public static double? ToDouble(this JsonElement? elementOrNull)

Parameters

elementOrNull JsonElement?

The JsonElement.

Returns

double?

ToDouble(JsonElement)

Converts the specified JsonElement into a nullable double.

public static double? ToDouble(this JsonElement element)

Parameters

element JsonElement

The JsonElement.

Returns

double?

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 JsonElement

The 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 JsonElement

The JsonElement.

Returns

int?

ToJsonElementArray(JsonElement?)

Enumerates the specified JsonElement array

public static JsonElement[] ToJsonElementArray(this JsonElement? elementOrNull)

Parameters

elementOrNull JsonElement?

The JsonElement.

Returns

JsonElement[]

ToJsonElementArray(JsonElement)

Enumerates the specified JsonElement array

public static JsonElement[] ToJsonElementArray(this JsonElement elementOrNull)

Parameters

elementOrNull JsonElement

The JsonElement.

Returns

JsonElement[]

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 JsonElement

The 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 JsonElement

the 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

IReadOnlyCollection<string>

ToReadOnlyCollection(JsonElement)

Converts the specified JsonElement array into a collection of string values.

public static IReadOnlyCollection<string?> ToReadOnlyCollection(this JsonElement element)

Parameters

element JsonElement

The JsonElement.

Returns

IReadOnlyCollection<string>

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

IReadOnlyCollection<T>

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 JsonElement

The JsonElement.

converter Func<JsonElement, T>

the specified converter

Returns

IReadOnlyCollection<T>

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 JsonElement

The 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

string

ToStringValue(JsonElement)

Converts the specified JsonElement into a nullable string.

public static string? ToStringValue(this JsonElement element)

Parameters

element JsonElement

The JsonElement.

Returns

string