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

333
Visualizações
System.Text.Json won't deserialize to my type even though my constructor implements every property

I can't figure out why System.Text.Json won't deserialize this simple JSON string to my specified .NET type. I'm getting the Each parameter in the deserialization constructor on type 'MenuItem' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive error message, even though I have mapped every property — name, url, and permissions — in my MenuItem constructor.

Dotnet fiddle demo

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

public class Program
{
    public static void Main()
    {
        try
        {
            var json = @"
                [
                    {
                        ""name"": ""General Info"",
                        ""url"": ""/Info"",
                        ""permissions"": []
                    },
                    {
                        ""name"": ""Settings"",
                        ""url"": ""/Settings"",
                        ""permissions"": [
                            ""Admin""
                        ]
                    }
                ]";

            Console.WriteLine(json);
            var menu = new Menu(json);
            Console.WriteLine("Deserialization success");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

public class Menu
{
    public Menu(string json)
    {
        var items = JsonSerializer.Deserialize<List<MenuItem>>(json);
        MenuItems = items.AsReadOnly();
    }

    public IReadOnlyList<MenuItem> MenuItems { get; }
}

public class MenuItem
{
    [JsonConstructor]
    public MenuItem
        (
            string name, 
            string url, 
            List<Permission> permissions
        )
    {
        this.Name = name;
        this.Url = url;
        this.Permissions = (permissions ?? new List<Permission>()).AsReadOnly();
    }

    public string Name { get; }
    public string Url { get; }
    public IReadOnlyList<Permission> Permissions { get; }
}

public enum Permission
{
    Admin,
    FinancialReporting
}

Can anyone tell me what I'm doing wrong?

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

0

I've had a look at your fiddle and spotted a couple of problems. Working fiddle here

  1. System.Text.Json is case-sensitive by default (except for web apps). You can resolve this by using either PropertyNamingPolicy = JsonNamingPolicy.CamelCase or PropertyNameCaseInsensitive = true in the serializer options.

  2. The second issue is outlined in Enums as strings

    By default, enums are serialized as numbers. To serialize enum names as strings, use the JsonStringEnumConverter.

    You should add JsonSerializerOptions to resolve (1) and (2):

    var options = new JsonSerializerOptions 
    {
        Converters =
        {
            new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
        },
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    };
    var items = JsonSerializer.Deserialize<List<MenuItem>>(json, options);
    
  3. The third issue appears to be with the binding in the constructor for the list of Permissions. In the constructor you define a List<Permission> for the permissions parameter. I receive the error in your question unless the constructor argument type matches the model property type exactly. So, I updated the constructor to take a IReadOnlyList<Permission> and it deserializes successfully:

    [JsonConstructor]
    public MenuItem
        (
            string name, 
            string url,
            IReadOnlyList<Permission> permissions
        )
    {
        this.Name = name;
        this.Url = url;
        this.Permissions = permissions ?? new List<Permission>().AsReadOnly();
    }
    

    Alternatively, you could change the Permissions property to List<Permission>.

This answer to a question with a similar problem explains that this is actually a limitation of System.Text.Json and there is currently an open github issue.

A working fork of your fiddle is demoed here.

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