JsonFileSaver.cs 702 B

1234567891011121314151617181920212223242526
  1. using System.Text.Json;
  2. using Microsoft.Extensions.Configuration;
  3. using System.IO;
  4. namespace gameapi.Library
  5. {
  6. public class JsonFileSaver : IJsonFileSaver
  7. {
  8. private string path;
  9. public JsonFileSaver()
  10. {
  11. path = GlobalConfiguration.Get().GetSection("gameapi").GetValue<string>("DataPath");
  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. public interface IJsonFileSaver
  20. {
  21. void Save(string filename, dynamic data);
  22. }
  23. }