Class JsonUtils
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
filePathstringThe file's path.
optionsJsonSerializerOptionsThe JSON settings, or null for the defaults.
Returns
- T
The object.
Type Parameters
TThe 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
jsonstringThe JSON.
optionsJsonSerializerOptionsThe JSON settings, or null for the defaults.
Returns
- T
The object.
Type Parameters
TThe 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
jsonstringThe JSON.
jsonTypeInfoJsonTypeInfo<T>The type's metadata.
Returns
- T
The object.
Type Parameters
TThe 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
objTThe object.
optionsJsonSerializerOptionsThe JSON settings, or null for the defaults.
Returns
- T
The copy.
Type Parameters
TThe 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
Returns
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
jsonstringThe JSON text; comments and trailing commas are accepted, and not written again.
keysstring[]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
filePathstringThe file's path.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<T>
A task whose result is the object, or the default.
Type Parameters
TThe object's type.
Exceptions
- OperationCanceledException
cancellationTokenwas 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
filePathstringThe file's path.
optionsJsonSerializerOptionsThe JSON settings, or null for the defaults.
loggerILoggerThe logger a failure goes to, or null for none.
Returns
- T
The object, or the default.
Type Parameters
TThe object's type.