Skip to main content

JsonObjectExtensions Class

Abblix.Jwt

Abblix.Jwt

JsonObjectExtensions Class

Provides extension methods for the System.Text.Json.Nodes.JsonObject class, enhancing its usability by simplifying the process of accessing and manipulating JSON properties.

public static class JsonObjectExtensions

Inheritance System.Object → JsonObjectExtensions

Remarks

The extension methods in this class aim to streamline common tasks associated with JSON objects, such as retrieving and setting properties with type safety and minimal boilerplate code. These methods abstract away some of the complexities of working directly with System.Text.Json.Nodes.JsonObject and System.Text.Json.Nodes.JsonNode, offering a more fluent and intuitive interface for developers.

Methods

JsonObjectExtensions.GetProperty<T>(this JsonObject, string) Method

Retrieves the value of the specified property from a System.Text.Json.Nodes.JsonObject.

public static T? GetProperty<T>(this System.Text.Json.Nodes.JsonObject json, string name);

Type parameters

T

The expected type of the property value.

Parameters

json System.Text.Json.Nodes.JsonObject

The System.Text.Json.Nodes.JsonObject instance to extract the property value from.

name System.String

The name of the property whose value is to be retrieved.

Returns

T
The value of the specified property if it exists and can be successfully converted to the specified type; otherwise, the default value for the type T.

Remarks

This method facilitates the retrieval of typed values from a JSON object, abstracting away the need for manual type checking and conversion.

JsonObjectExtensions.SetProperty(this JsonObject, string, JsonNode) Method

Sets or updates the value of a specified property in a System.Text.Json.Nodes.JsonObject.

public static System.Text.Json.Nodes.JsonObject SetProperty(this System.Text.Json.Nodes.JsonObject json, string name, System.Text.Json.Nodes.JsonNode? value);

Parameters

json System.Text.Json.Nodes.JsonObject

The System.Text.Json.Nodes.JsonObject instance to modify.

name System.String

The name of the property to set or update.

value System.Text.Json.Nodes.JsonNode

The new value for the property. If null, the property is removed from the System.Text.Json.Nodes.JsonObject.

Returns

System.Text.Json.Nodes.JsonObject

Remarks

This method provides a convenient way to update the properties of a JSON object, allowing for the addition of new properties or the removal of existing ones by providing a null value. It ensures that the JSON object remains in a consistent state by avoiding the presence of null property values.