JsonFileSaver.cs 801 B

123456789101112131415161718192021222324252627282930313233
  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("server").GetValue<string>("DataPath");
  12. }
  13. public void Save(string filename, dynamic data)
  14. {
  15. var serializedData = JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true });
  16. File.WriteAllText(filename, serializedData);
  17. }
  18. public void SaveGameData(string filename, dynamic data)
  19. {
  20. var filePath = path + "/" + filename;
  21. Save(filePath, data);
  22. }
  23. }
  24. public interface IJsonFileSaver
  25. {
  26. void Save(string filename, dynamic data);
  27. }
  28. }