Files
adas-core/adas-core.Infrastructure/Utils/CustomPointOfCareConverter.cs
T
2026-06-27 15:23:26 -07:00

71 lines
3.8 KiB
C#

using adas_core.Domain.Models.MongoModels;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace adas_core.Infrastructure.Utils;
/// <summary>
/// Provides a custom JSON conversion implementation for point of care data, extending the base <see cref="JsonConverter"/> functionality.
/// </summary>
/// <remarks>
/// This converter is designed to handle the serialization and deserialization logic specific to point of care entities, tailoring the behavior inherited from the <see cref="JsonConverter"/> base class.
/// </remarks>
public class CustomPointOfCareConverter : JsonConverter
{
/// <summary>
/// Determines whether the converter can convert the specified type. Returns <c>true</c> only when the supplied type is <see cref="PointOfCare"/>; otherwise, returns <c>false</c>.
/// </summary>
/// <param name="objectType">The type to evaluate for convertibility.</param>
/// <returns><c>true</c> if <paramref name="objectType"/> equals <see cref="PointOfCare"/>; otherwise, <c>false</c>.</returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(PointOfCare);
}
/// <summary>
/// Serializes an object to JSON, converting all enum-typed properties to their string representation rather than their underlying integer value. If <paramref name="value"/> is <c>null</c>, a JSON null token is written and the method returns immediately.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> that receives the serialized JSON output.</param>
/// <param name="value">The object to serialize. Enum properties on this instance are written as their string names.</param>
/// <param name="serializer">The <see cref="JsonSerializer"/> used to convert the value to a <see cref="JObject"/>.</param>
/// <!-- aidoc:v1 sig=0bafa81 body=109184b -->
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
var jo = JObject.FromObject(value);
// Aquí modificas específicamente las propiedades que necesitas
// Por ejemplo, aplicar StringEnumConverter solo a ciertas propiedades enumeradas
// Esto es un ejemplo, ajusta según tus necesidades
foreach (var prop in value.GetType().GetProperties())
if (prop.PropertyType.IsEnum)
{
var enumValue = prop.GetValue(value);
if (enumValue != null) jo[prop.Name] = JToken.FromObject(enumValue.ToString() ?? string.Empty);
}
jo.WriteTo(writer);
}
/// <summary>
/// Reads and deserializes a JSON value into an object of the target <paramref name="objectType"/>. This override is not implemented and serves as a placeholder.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> used to read the incoming JSON tokens.</param>
/// <param name="objectType">The <see cref="Type"/> of the object to deserialize into.</param>
/// <param name="existingValue">An existing value to reuse during deserialization, or <see langword="null"/> if none is available.</param>
/// <param name="serializer">The <see cref="JsonSerializer"/> controlling the deserialization process.</param>
/// <returns>An <see cref="object"/> instance populated from the JSON data.</returns>
/// <exception cref="NotImplementedException">Thrown in all cases because the method body has not been implemented.</exception>
/// <!-- aidoc:v1 sig=c91c541 body=bfa6f2f -->
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue,
JsonSerializer serializer)
{
// Implementa la lógica de deserialización si es necesario
throw new NotImplementedException();
}
}