Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
Deserialize Record - how to init missing properties

Consider a simple case of trying to deserialize a record where some properties are missing:

#nullable enable

record Rec(int Age, Employee Employee); // Employee has a default constructor

var rec = JsonConvert.DeserializeObject<Rec>("{\"Age\":30}");

I get rec with Employee set to null without any compiler warning despite nullable enabled and have to manually set it using the default constructor.

Is there a way to tell DeserializeObject to try/use the default constructor for missing properties instead of setting them to null ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There is an overload to DeserializeObject that allows you to pass in an array of JsonConverter which allows you to management the type conversion (from JObject) yourself. Alternatively you could also change the default JsonSerializerSettings to NOT allow nulls and manually handle the exception that is raised.

All of that would require a lot of boilerplate code but for simple default type instantiation, there is a CustomCreationConverter you can inherit from

class RecConverter : CustomCreationConverter<Rec>
{
    public override Rec Create(Type objectType)
    {
        return new Rec ( 0, new Employee() { Name = "Custom" } );
    }
}

Note that the default value of properties is usually defined in the declaration of the type itself.

DeserializeObject will first instantiate the object, and then if MissingMemberHandling is set to "Ignore", it only sets the properties that are available, the others will remain in their default initialized state, that is defined by the default constructor for that type, which may be defined through auto-property initialization syntax.

You could declare your class with default initialization for the properties that you require to be non-null:

public class Rec
{
    public int Age { get;set; }
    public Employee Employee { get;set; } = new Employee();
}

Similar rules apply for C#9 record, however DeserializeObject will call the relevant constructor that matches the arguments that are available from the JSON string, so you can use the verbose declaration syntax to achieve the same thing:

record Rec
{
    public int Age { get;init; }
    public Employee Employee { get;init; } = new Employee();
}

See this fiddle: https://dotnetfiddle.net/Dmpmn5

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda