Table of Contents

Class JsonUtils

Namespace
VpnHood.Net.Toolkit.Utils
Assembly
VpnHood.Net.Toolkit.dll

Provides JSON helpers: comparison, cloning, reading text and files, and redaction.

public static class JsonUtils
Inheritance
JsonUtils
Inherited Members

Methods

DeserializeFile<T>(string, JsonSerializerOptions?)

Reads an object from a JSON file; a null is refused.

public static T DeserializeFile<T>(string filePath, JsonSerializerOptions? options = null)

Parameters

filePath string

The file's path.

options JsonSerializerOptions

The JSON settings, or null for the defaults.

Returns

T

The object.

Type Parameters

T

The object's type.

Exceptions

IOException

The file cannot be read.

UnauthorizedAccessException

The process may not read the file.

InvalidDataException

The result is null.

JsonException

The text is not JSON of the type.

Deserialize<T>(string, JsonSerializerOptions?)

Reads an object from JSON; a null result is refused.

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

Parameters

json string

The JSON.

options JsonSerializerOptions

The JSON settings, or null for the defaults.

Returns

T

The object.

Type Parameters

T

The object's type.

Exceptions

InvalidDataException

The result is null.

JsonException

The text is not JSON of the type.

Deserialize<T>(string, JsonTypeInfo<T>)

Reads an object from JSON with the type's metadata; a null result is refused.

public static T Deserialize<T>(string json, JsonTypeInfo<T> jsonTypeInfo)

Parameters

json string

The JSON.

jsonTypeInfo JsonTypeInfo<T>

The type's metadata.

Returns

T

The object.

Type Parameters

T

The object's type.

Exceptions

InvalidDataException

The result is null.

JsonException

The text is not JSON of the type.

JsonClone<T>(T, JsonSerializerOptions?)

Copies an object by writing it as JSON and reading it back.

public static T JsonClone<T>(T obj, JsonSerializerOptions? options = null)

Parameters

obj T

The object.

options JsonSerializerOptions

The JSON settings, or null for the defaults.

Returns

T

The copy.

Type Parameters

T

The object's type.

Exceptions

InvalidDataException

The result is null.

JsonException

The text is not JSON of the type.

JsonEquals(object?, object?)

Determines whether two objects write the same JSON, with the default settings.

public static bool JsonEquals(object? obj1, object? obj2)

Parameters

obj1 object

The first object, or null.

obj2 object

The second object, or null.

Returns

bool

true if both are null or their JSON is the same; otherwise, false.

RedactValue(string, string[])

Replaces the values of some keys in JSON text with "", an array's with [""], at any depth.

public static string RedactValue(string json, string[] keys)

Parameters

json string

The JSON text; comments and trailing commas are accepted, and not written again.

keys string[]

The keys whose values are hidden, compared as they are.

Returns

string

The text with those values hidden.

Remarks

The text is read as JSON and written again, indented when it came on several lines, so a value is hidden whole, whatever it holds: a string with commas or braces, an object, an array of arrays.

Exceptions

JsonException

The text is not JSON; nothing of it is returned.

TryDeserializeFileAsync<T>(string, CancellationToken)

Reads an object from a JSON file asynchronously, with the default settings, or gives the default: a missing file goes unlogged, a read failure is logged as a warning and a parse failure as an error, to VhLogger.

public static Task<T?> TryDeserializeFileAsync<T>(string filePath, CancellationToken cancellationToken)

Parameters

filePath string

The file's path.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<T>

A task whose result is the object, or the default.

Type Parameters

T

The object's type.

Exceptions

OperationCanceledException

cancellationToken was canceled.

TryDeserializeFile<T>(string, JsonSerializerOptions?, ILogger?)

Reads an object from a JSON file, or gives the default: a missing file, as on a first run, goes unlogged, and any other failure, a file gone after the check included, is logged as an error when there is a logger.

public static T? TryDeserializeFile<T>(string filePath, JsonSerializerOptions? options = null, ILogger? logger = null)

Parameters

filePath string

The file's path.

options JsonSerializerOptions

The JSON settings, or null for the defaults.

logger ILogger

The logger a failure goes to, or null for none.

Returns

T

The object, or the default.

Type Parameters

T

The object's type.