I have this class definition:
public class TrabajadorViewModel
{
public int ID { get; set; }
[Display(Name = "Habilitado")]
public bool Habilitado { get; set; }
[Display(Name = "Inicio de contrato")]
public DateTime? InicioContrato { get; set; }
[Display(Name = "Término de contrato")]
[DateGreaterThan(OtherPropertyName = "InicioContrato", ErrorMessage = "La fecha de término de contrato debe ser mayor que la fecha de inicio.")]
public DateTime? TerminoContrato { get; set; }
[Display(Name = "Envía comprobante por mail")]
public bool ComprobanteMail { get; set; }
[Display(Name = "Permite remoto")]
public bool PermiteRemoto { get; set; }
[Display(Name = "Asistencia activada")]
public bool AsistenciaActivada { get; set; }
[Display(Name = "Feriado")]
public bool Feriado { get; set; }
[Display(Name = "Horas extra")]
public bool HorasExtra { get; set; }
[Display(Name = "Registra hora extra")]
public bool RegistraHoraExtra { get; set; }
[Display(Name = "Horario automático")]
public bool AutoHorario { get; set; }
[Display(Name = "Tipo de marca de entrada")]
public string? TipoMarcaEntrada { get; set; }
[Display(Name = "Tipo de marca de salida")]
public string? TipoMarcaSalida { get; set; }
public int Pin { get; set; }
public string? Tarjeta { get; set; }
[Required]
[Range(1, Int32.MaxValue, ErrorMessage = "Debe indicar la {0}.")]
[Display(Name = "Persona")]
public int PersonaId { get; set; }
private IBaseVIewModel? _objPersona;
public IBaseVIewModel? ObjPersona
{
get
{
return _objPersona;
}
set
{
this.PersonaId = value == null ? 0 : value.ID;
_objPersona = value;
}
}
[Display(Name = "Turnos")]
public int[]? Turnos { get; set; }
public List<TurnoPorTrabajador> ObjTurnos { get; }
public void SetTurnos(IEnumerable<TurnoPorTrabajador> turnos)
{
this.ObjTurnos.Clear();
this.ObjTurnos.AddRange(turnos);
Turnos = turnos.Select(s => s.TurnoId).ToArray();
}
public DateTime[]? FechasInicio { get; set; }
public DateTime[]? FechasTermino { get; set; }
public UbicacionViewModel? Ubicacion { get; set; }
[Display(Name = "Áreas")]
public int[]? Areas { get; set; }
public List<Area> ObjAreas { get; }
public void SetAreas(IEnumerable<Area> areas)
{
this.ObjAreas.Clear();
this.ObjAreas.AddRange(areas);
Areas = areas.Select(a => a.AreaId).ToArray();
}
public TrabajadorViewModel()
{
this.ObjTurnos = [];
this.ObjAreas = [];
}
}
On the other hand, I have the following JSON string:
{
"ID": 10,
"Habilitado": true,
"InicioContrato": null,
"TerminoContrato": null,
"ComprobanteMail": false,
"PermiteRemoto": true,
"AsistenciaActivada": true,
"Feriado": false,
"HorasExtra": false,
"RegistraHoraExtra": false,
"AutoHorario": false,
"TipoMarcaEntrada": "M",
"TipoMarcaSalida": "M",
"Pin": 18228795,
"Tarjeta": null,
"PersonaId": 1391,
"ObjPersona": null,
"Turnos": null,
"ObjTurnos": [],
"FechasInicio": null,
"FechasTermino": null,
"Ubicacion": {
"ID": 0,
"Nombre": null,
"Direccion": null,
"CiudadId": null,
"ObjCiudad": null,
"Latitud": 0,
"Longitud": 0,
"Radio": 30
},
"Areas": null,
"ObjAreas": [],
"Notification": {
"Type": 0,
"Text": null,
"Icon": "check-all",
"Title":"\u00C9xito"
}
}
Finally, I am calling this function:
JsonSerializer.Deserialize<TrabajadorViewModel>(json)
After that, resulting object contains all the right properties, but one property which value is kept with 0. Such a property is PersonaId
:
> JsonSerializer.Deserialize<T>(json)
> {Modules.TimeAttendance.Data.ViewModels.TrabajadorViewModel}
> Areas: null
> AsistenciaActivada: true
> AutoHorario: false
> ComprobanteMail: false
> FechasInicio: null
> FechasTermino: null
> Feriado: false
> Habilitado: true
> HorasExtra: false
> ID: 10
> InicioContrato: null
> Notification: {Modules.Integration.ViewModels.MessageViewModel}
> ObjAreas: Count = 0
> ObjPersona: null
> ObjTurnos: Count = 0
> PermiteRemoto: true
> **PersonaId: 0**
> Pin: 18228795
> RegistraHoraExtra: false
> Tarjeta: null
> TerminoContrato: null
> TipoMarcaEntrada: "M"
> TipoMarcaSalida: "M"
> Turnos: null
> Ubicacion: {Modules.TimeAttendance.Data.ViewModels.UbicacionViewModel}
> _objPersona: null
As you can see, PersonaId
is contained in the json string and also, it belongs to the TrabajadorViewModel
class.
Why that property is not deserialized properly? Other properties are.
How can I fix this?
The setter for ObjPersona
contains this: this.PersonaId = value == null ? 0 : value.ID;
.
The JSON contains ... "ObjPersona":null ...
.
It looks like that setter will run and will set this.PersonaId
to 0.