Skip to content

Deserialize

Deserializes a JSON string into a nullable value of type T. Use this extension when the complete JSON payload is already available as a string and the caller wants a compact wrapper around JsonSerializer.Deserialize<T>.

There are two overloads. Pass JsonSerializerOptions when the caller needs exact control over naming, converters, or other serializer behavior. Use the bool overload when the caller wants the package default options, which currently use camel-case naming.

Usage

csharp
using AlmightyShogun.Utils;

string json = """
{
    "name": "Akari",
    "enabled": true
}
""";

ExampleSettings? settings = json.Deserialize<ExampleSettings>();
csharp
using System.Text.Json;
using AlmightyShogun.Utils;

JsonSerializerOptions options = new()
{
    PropertyNameCaseInsensitive = true
};

ExampleSettings? settings = json.Deserialize<ExampleSettings>(options);
csharp
public sealed record ExampleSettings(string Name, bool Enabled);

Parameters

json: string
JSON string to deserialize.

options: JsonSerializerOptions?
Optional System.Text.Json serializer options. When omitted, the runtime serializer defaults are used.
Default: null

useDefaultOptions: bool
Whether to use the package default serializer options with camel-case naming.
Default: true

Returns

A deserialized T instance, or null when the JSON contains null for a nullable target.

Type signature

csharp
public static T? Deserialize<T>(
    this string json,
    JsonSerializerOptions? options = null
);

public static T? Deserialize<T>(
    this string json,
    bool useDefaultOptions = true
);

All packages are released under the MIT License.