Class JsonUtils
Help methods to work with JSON
Inheritance
object
JsonUtils
Namespace: BuildSystem.Utils.Json
Assembly: BuildSystem.Utils.dll
Syntax
public static class JsonUtils : Object
Methods
Merge(JsonNode, JsonNode?)
Merges simple JsonObject with another simple JsonObject. Analyzes keys top level only
Declaration
public static JsonObject Merge(JsonNode firstJson, JsonNode? priorJson)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Text.Json.Nodes.JsonNode | firstJson | JsonObject with lesser priority |
| System.Text.Json.Nodes.JsonNode | priorJson | JsonObject with higher priority |
Returns
| Type | Description |
|---|---|
| System.Text.Json.Nodes.JsonObject | Merged JsonObject |
ReadEnum<TEnum>(JsonNode?, string, TEnum?)
Read value of node as string and then cast it to the value from enum
Declaration
public static TEnum? ReadEnum<TEnum>(JsonNode? node, string key, TEnum? defValue)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Text.Json.Nodes.JsonNode | node | JSON which must be JsonObject |
| string | key | Key in input json |
| TEnum | defValue | Default value if |
Returns
| Type | Description |
|---|---|
| TEnum |
Type Parameters
| Name | Description |
|---|---|
| TEnum |
ReadEnum<TEnum>(JsonNode?, string)
Read value of node as string and then cast it to the value from enum
Declaration
public static TEnum ReadEnum<TEnum>(JsonNode? node, string key)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Text.Json.Nodes.JsonNode | node | JSON which must be JsonObject |
| string | key | Key in input json |
Returns
| Type | Description |
|---|---|
| TEnum | Value casted to enum |
Type Parameters
| Name | Description |
|---|---|
| TEnum |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | JSON is not JsonObject or does not contain need key |
| System.ArgumentOutOfRangeException | Value cannot be casted to enum |
SaveToFile(JsonNode, string)
Save JSON to text file with formatting
Declaration
public static void SaveToFile(JsonNode json, string resultFile)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Text.Json.Nodes.JsonNode | json | Saving JSON |
| string | resultFile | Creating file |