using adas_core.Domain.Models.MongoModels;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace adas_core.Infrastructure.Utils;
///
/// Provides a custom JSON conversion implementation for point of care data, extending the base functionality.
///
///
/// This converter is designed to handle the serialization and deserialization logic specific to point of care entities, tailoring the behavior inherited from the base class.
///
///
public class CustomPointOfCareConverter : JsonConverter
{
///
/// Determines whether the converter can convert the specified type. Returns true only when the supplied type is ; otherwise, returns false.
///
/// The type to evaluate for convertibility.
/// true if equals ; otherwise, false.
///
public override bool CanConvert(Type objectType)
{
return objectType == typeof(PointOfCare);
}
///
/// Serializes an object to JSON, converting all enum-typed properties to their string representation rather than their underlying integer value. If is null, a JSON null token is written and the method returns immediately.
///
/// The that receives the serialized JSON output.
/// The object to serialize. Enum properties on this instance are written as their string names.
/// The used to convert the value to a .
///
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);
}
///
/// Reads and deserializes a JSON value into an object of the target . This override is not implemented and serves as a placeholder.
///
/// The used to read the incoming JSON tokens.
/// The of the object to deserialize into.
/// An existing value to reuse during deserialization, or if none is available.
/// The controlling the deserialization process.
/// An instance populated from the JSON data.
/// Thrown in all cases because the method body has not been implemented.
///
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();
}
}