Json deserialization does not set certain object property

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.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật