-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonFileHandler.cs
More file actions
36 lines (31 loc) · 1.13 KB
/
JsonFileHandler.cs
File metadata and controls
36 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Text.Json;
using System.Threading.Tasks;
namespace AggregateConfigBuildTask.FileHandlers
{
/// <summary>
/// Handles reading and writing JSON files.
/// </summary>
public class JsonFileHandler : IFileHandler
{
readonly IFileSystem fileSystem;
private readonly JsonSerializerOptions jsonOptions = new JsonSerializerOptions { WriteIndented = true };
internal JsonFileHandler(IFileSystem fileSystem)
{
this.fileSystem = fileSystem;
}
/// <inheritdoc/>
public async ValueTask<JsonElement> ReadInput(string inputPath)
{
using (var json = fileSystem.OpenRead(inputPath))
{
return await JsonSerializer.DeserializeAsync<JsonElement>(json).ConfigureAwait(false);
}
}
/// <inheritdoc/>
public Task WriteOutput(JsonElement? mergedData, string outputPath)
{
var jsonContent = JsonSerializer.Serialize(mergedData, jsonOptions);
return fileSystem.WriteAllTextAsync(outputPath, jsonContent);
}
}
}