32 lines
910 B
C#
32 lines
910 B
C#
using System.Text.Json;
|
|
using General_Ledger_Service.Exceptions;
|
|
|
|
namespace General_Ledger_Service.Helpers
|
|
{
|
|
public static class DeserializePayload
|
|
{
|
|
public static Dictionary<string, JsonElement> Deserialize(object payload)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(payload);
|
|
var json = JsonSerializer.Serialize(payload);
|
|
return JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json)
|
|
?? throw new BadRequestException("Failed to deserialize payload.");
|
|
}
|
|
|
|
public static T? DeserializeJson<T>(string? json) where T : class
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<T>(json);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|