Home > Mobile >  System.Text.Json won't deserialize to my type even though my constructor implements every prope
System.Text.Json won't deserialize to my type even though my constructor implements every prope

Time:02-20

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?

CodePudding user response:

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.

  • Related