Table of Contents

Class JsonNodeExtensions

Namespace
Songhay.Extensions
Assembly
SonghayCore.dll

Extensions of JsonNode.

public static class JsonNodeExtensions
Inheritance
JsonNodeExtensions
Inherited Members

Remarks

To prevent passing null instances of ILogger into these methods, use AsInstanceOrNullLogger(ILogger?).

Methods

DisplayTopProperties(JsonObject?, int)

Displays top-level JsonObject properties without recursion.

public static string DisplayTopProperties(this JsonObject? jObject, int truncationLength = 16)

Parameters

jObject JsonObject

the JsonObject

truncationLength int

the number of characters to display for each property

Returns

string

GetJsonValueKind(JsonNode?)

Gets the JsonValueKind of the specified JsonNode.

[Obsolete("For .NET 8 and beyond use `JsonNode.GetValueKind()` instead.")]
public static JsonValueKind GetJsonValueKind(this JsonNode? node)

Parameters

node JsonNode

The node.

Returns

JsonValueKind

Remarks

This member is needed for .NET 6.0 and earlier.

GetPropertyJsonArrayOrNull(JsonNode?, string)

Gets the JsonValue of the specified JsonNode of Array or defaults to null.

public static JsonArray? GetPropertyJsonArrayOrNull(this JsonNode? node, string propertyName)

Parameters

node JsonNode

The JsonNode.

propertyName string

Name of the property.

Returns

JsonArray

GetPropertyJsonObjectOrNull(JsonNode?, string)

Gets the JsonValue of the specified JsonNode of Object or defaults to null.

public static JsonObject? GetPropertyJsonObjectOrNull(this JsonNode? node, string propertyName)

Parameters

node JsonNode

The JsonNode.

propertyName string

Name of the property.

Returns

JsonObject

GetPropertyJsonValueOrNull(JsonNode?, string)

Gets the JsonValue of the specified JsonNode or defaults to null.

public static JsonValue? GetPropertyJsonValueOrNull(this JsonNode? node, string propertyName)

Parameters

node JsonNode

The JsonNode.

propertyName string

Name of the property.

Returns

JsonValue

Remarks

This member will return null when the specified JsonNode is not of:

GetPropertyValue<T>(JsonNode?, string)

Gets the property value of the specified JsonNode.

[Obsolete("Use `GetPropertyJsonArrayOrNull`, `GetPropertyJsonObjectOrNull` or `GetPropertyJsonValueOrNull` instead.")]
public static (T? value, bool success) GetPropertyValue<T>(this JsonNode? node, string propertyName)

Parameters

node JsonNode

The node.

propertyName string

Name of the property.

Returns

(T value, bool success)

Type Parameters

T

HasProperty(JsonNode?, string?)

Returns false when the specified property name does not exist.

public static bool HasProperty(this JsonNode? node, string? propertyName)

Parameters

node JsonNode

the JsonNode

propertyName string

the property name

Returns

bool

IsExpectedObject(JsonNode?, ILogger, params string[])

Determines whether the specified JsonNode is the expected JsonObject.

public static bool IsExpectedObject(this JsonNode? node, ILogger logger, params string[] properties)

Parameters

node JsonNode

The node.

logger ILogger

The logger.

properties string[]

The properties.

Returns

bool

RemoveFromParent(JsonNode?, ILogger)

Removes the specified JsonNode when its parent is JsonArray or JsonObject.

public static void RemoveFromParent(this JsonNode? node, ILogger logger)

Parameters

node JsonNode

the JsonNode

logger ILogger

the ILogger

RemoveFromParent(JsonNode?, string?, ILogger)

Removes the specified JsonNode when its parent is JsonArray or JsonObject.

public static void RemoveFromParent(this JsonNode? node, string? propertyName, ILogger logger)

Parameters

node JsonNode

the JsonNode

propertyName string

the name of the property when the specified node is Object

logger ILogger

the ILogger

RemoveProperty(JsonNode?, ILogger)

Removes the specified JsonNode when its parent is JsonArray or JsonObject.

public static void RemoveProperty(this JsonNode? node, ILogger logger)

Parameters

node JsonNode

the JsonNode

logger ILogger

the ILogger

RemoveProperty(JsonNode?, string?, ILogger)

Removes the specified JsonNode with the specified property name when the node is JsonObject or JsonArray (array of JsonObject with a property to remove).

public static void RemoveProperty(this JsonNode? node, string? propertyName, ILogger logger)

Parameters

node JsonNode

the JsonNode

propertyName string

the name of the property when the specified node is Object

logger ILogger

the ILogger

ToJsonArray(JsonNode?, ILogger)

Converts to the specified JsonNode to JsonArray or logs failure and returns null.

public static JsonArray? ToJsonArray(this JsonNode? node, ILogger logger)

Parameters

node JsonNode

The node.

logger ILogger

The logger.

Returns

JsonArray

ToJsonObject(JsonNode?, ILogger)

Converts to the specified JsonNode to JsonObject or logs failure and returns null.

public static JsonObject? ToJsonObject(this JsonNode? node, ILogger logger)

Parameters

node JsonNode

The node.

logger ILogger

The logger.

Returns

JsonObject

WithPropertiesRenamed(JsonObject?, ILogger, params (string oldName, string newName)[])

Returns the specified JsonObject with its properties renamed.

public static JsonObject? WithPropertiesRenamed(this JsonObject? documentData, ILogger logger, params (string oldName, string newName)[] operations)

Parameters

documentData JsonObject

the JsonObject

logger ILogger

the ILogger

operations (string oldName, string newName)[]

specifies which JsonObject properties to rename

Returns

JsonObject