44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using System.Text.Json;
|
|
|
|
namespace MaxInventory.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Default implementation of IEntitySerializer using System.Text.Json.
|
|
/// Configured to respsect [JsonPropertyName] attribute on model classes.
|
|
/// </summary>
|
|
public sealed class EntitySerializer : IEntitySerializer
|
|
{
|
|
private readonly JsonSerializerOptions _options;
|
|
|
|
public EntitySerializer()
|
|
{
|
|
_options = new JsonSerializerOptions
|
|
{
|
|
//Do not apply naming policy
|
|
PropertyNamingPolicy = null,
|
|
//Allow case-insensitive matching
|
|
PropertyNameCaseInsensitive = true,
|
|
WriteIndented = false,
|
|
};
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public string Serialize<T>(T entity)
|
|
{
|
|
return JsonSerializer.Serialize(entity, _options);
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public T? Deserialize<T>(string json)
|
|
{
|
|
return JsonSerializer.Deserialize<T>(json, _options);
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public List<T> DeserializeList<T>(string json)
|
|
{
|
|
return JsonSerializer.Deserialize<List<T>>(json, _options) ?? new List<T>();
|
|
}
|
|
}
|
|
}
|