using System.Reflection;
using adas_core.Domain.Models;
using MongoDB.Bson.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace adas_core.Infrastructure.Utils;
///
/// Converts instances to and from JSON representation.
///
///
/// Implements to provide BSON serialization and deserialization support for objects.
///
public class PatientObservationAlarmConverter : JsonConverter, IBsonSerializer
{
///
/// Deserializes a BSON value into a .NET object using the supplied deserialization context and arguments.
///
/// The BSON deserialization context that provides access to the reader and configuration.
/// The deserialization arguments that influence how the value is read and converted.
/// The deserialized .NET object produced from the BSON input.
/// Always thrown because the deserialization logic has not been implemented yet.
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
throw new NotImplementedException();
}
///
/// Serializes the specified value to BSON format using the provided serialization context and arguments.
///
/// The BSON serialization context that holds the writer and configuration for the serialization operation.
/// The BSON serialization arguments containing additional information that influences the serialization behavior.
/// The object to be serialized into BSON.
/// Thrown because the method has not yet been implemented.
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
throw new NotImplementedException();
}
public Type ValueType => typeof(PatientObservationAlarm);
///
/// Deserializes a JSON token into a instance. Returns null when the JSON token is , otherwise extracts the value property from the JSON object and constructs a new alarm, falling back to a default when the value is missing.
///
/// The positioned at the JSON token to read.
/// The type of the object being deserialized.
/// The existing value of the object being deserialized, or null if none exists.
/// Indicates whether contains a value to be used during deserialization.
/// The used for nested object deserialization.
/// A populated from the JSON, or null if the JSON token is .
public override PatientObservationAlarm? ReadJson(JsonReader reader, Type objectType,
PatientObservationAlarm? existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
// Implement your custom deserialization logic here
var jsonObject = JObject.Load(reader);
// Deserialize properties from jsonObject to PatientObservationAlarm object
// Example: Deserialize 'Value' property
var value = jsonObject.GetValue("value")?.ToObject