Sprint 5 (#28)

This commit was merged in pull request #28.
This commit is contained in:
Caterina Simona Pastore
2025-05-28 00:10:38 +02:00
committed by GitHub
parent ac20664446
commit 79549bea05
53 changed files with 5781 additions and 63 deletions

View File

@@ -0,0 +1,42 @@
using System.Text.Json;
using NLog;
namespace BasicDotnetTemplate.MainProject.Utils;
public static class FileUtils
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private static readonly JsonSerializerOptions jsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
public static T? ConvertFileToObject<T>(string? filePath = "")
{
Logger.Info("[FileUtils][ReadJson] Reading file");
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentException("filePath cannot be null or empty", nameof(filePath));
}
if (!File.Exists(filePath))
{
throw new FileNotFoundException("The specified file does not exists", filePath);
}
try
{
string fileContent = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<T>(fileContent, jsonSerializerOptions);
}
catch (JsonException ex)
{
throw new InvalidOperationException("Error during file deserialization", ex);
}
}
}