using System.Text.Json;
using System.Threading.Tasks;
namespace AggregateConfigBuildTask.FileHandlers
{
///
public class JsonFileHandler : IFileHandler
{
private readonly IFileSystem fileSystem;
private readonly JsonSerializerOptions jsonOptions = new JsonSerializerOptions { WriteIndented = true };
internal JsonFileHandler(IFileSystem fileSystem)
{
this.fileSystem = fileSystem;
}
///
public async ValueTask ReadInput(string inputPath)
{
using (var json = fileSystem.OpenRead(inputPath))
{
return await JsonSerializer.DeserializeAsync(json).ConfigureAwait(false);
}
}
///
public void WriteOutput(JsonElement? mergedData, string outputPath)
{
var jsonContent = JsonSerializer.Serialize(mergedData, jsonOptions);
fileSystem.WriteAllText(outputPath, jsonContent);
}
}
}