Class JsonHelper
- Namespace
- OpenSettings.Helpers
- Assembly
- OpenSettings.dll
Provides helper methods for JSON manipulation, including options for reading and writing JSON documents and merging JSON objects.
public static class JsonHelper
- Inheritance
-
JsonHelper
- Inherited Members
Properties
DefaultJsonDocumentOptions
Gets the default options for parsing a JSON document, which allows trailing commas and skips comments.
public static JsonDocumentOptions DefaultJsonDocumentOptions { get; }
Property Value
DefaultJsonSerializerOptions
Gets the default options for serializing JSON, which allows trailing commas, uses relaxed escaping, and skips comments.
public static JsonSerializerOptions DefaultJsonSerializerOptions { get; }
Property Value
DefaultJsonWriterOptions
Gets the default options for writing a JSON document, which does not indent and uses relaxed escaping.
public static JsonWriterOptions DefaultJsonWriterOptions { get; }
Property Value
Methods
GetJsonFileAsync(string, CancellationToken)
Reads a JSON file asynchronously and returns it as a dictionary.
public static Task<Dictionary<string, object>> GetJsonFileAsync(string filePath, CancellationToken cancellationToken = default)
Parameters
filePath
stringThe file path of the JSON file to read.
cancellationToken
CancellationTokenA cancellation token to support cancellation of the operation.
Returns
- Task<Dictionary<string, object>>
A Task containing a Dictionary<string, object> with the JSON data.
Merge(string, string)
Merges two JSON strings (baseJson and patchJson) into a single JSON object.
public static JsonMergeResult Merge(string baseJson, string patchJson)
Parameters
baseJson
stringThe base JSON string to be merged with the patch.
patchJson
stringThe patch JSON string containing the changes to be applied to the base JSON.
Returns
- JsonMergeResult
A JsonMergeResult containing the result of the merge operation.
Merge(JsonDocument, JsonDocument)
Merges two JSON strings (baseDoc and patchDoc) into a single JSON object.
public static JsonMergeResult Merge(JsonDocument baseDoc, JsonDocument patchDoc)
Parameters
baseDoc
JsonDocumentThe base JsonDocument to be merged with the patch.
patchDoc
JsonDocumentThe patch JsonDocument containing the changes to be applied to the base JSON.
Returns
- JsonMergeResult
A JsonMergeResult containing the result of the merge operation.
Remarks
Both baseDoc
and patchDoc
should be disposed after the merge operation is complete.
MergeFileAsync(string, string, CancellationToken)
Merges two JSON files asynchronously by reading their contents from the specified file paths.
public static Task<JsonMergeResult> MergeFileAsync(string baseFilePath, string patchFilePath, CancellationToken cancellationToken = default)
Parameters
baseFilePath
stringThe file path of the base JSON file.
patchFilePath
stringThe file path of the patch JSON file to apply.
cancellationToken
CancellationTokenA cancellation token to support cancellation of the operation.
Returns
- Task<JsonMergeResult>
A Task representing the asynchronous operation, with the result as a JsonMergeResult.