JsonFileSaver.cs 594 B

123456789101112131415161718192021
  1. using System.Text.Json;
  2. using Microsoft.Extensions.Configuration;
  3. using System.IO;
  4. namespace sera_slackbot.Library
  5. {
  6. public class JsonFileSaver
  7. {
  8. private string path;
  9. public JsonFileSaver(IConfiguration config)
  10. {
  11. path = config.GetSection("Persistence").GetValue<string>("Path");
  12. }
  13. public void Save(string filename, dynamic data)
  14. {
  15. var filePath = path + "/" + filename;
  16. File.WriteAllText(filePath, JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true }));
  17. }
  18. }
  19. }