© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
46 replies
המשורר

JsonSerialization Help

I have this class in C# (functionallity removed)
public class Command
{
    [JsonIgnore]
    public IEnumerable<Flag> Flags => _flags;
    [JsonIgnore]
    public IEnumerable<string> AllowedFlags => _allowedFlags;
    [JsonPropertyName("command")]
    public string Name { get; }

    [JsonPropertyName("allowedFlags")]
    private readonly HashSet<string> _allowedFlags;
    [JsonPropertyName("flags")]
    private readonly List<Flag> _flags = [];
    [JsonPropertyName("arguments")]
    private readonly List<string> _arguments;
public class Command
{
    [JsonIgnore]
    public IEnumerable<Flag> Flags => _flags;
    [JsonIgnore]
    public IEnumerable<string> AllowedFlags => _allowedFlags;
    [JsonPropertyName("command")]
    public string Name { get; }

    [JsonPropertyName("allowedFlags")]
    private readonly HashSet<string> _allowedFlags;
    [JsonPropertyName("flags")]
    private readonly List<Flag> _flags = [];
    [JsonPropertyName("arguments")]
    private readonly List<string> _arguments;


A record that allows parsing of multiple commands:
[JsonSerializable(typeof(CommandConfig))]
public record CommandConfig
{
    [JsonPropertyName("commands")]
    public IList<Command> Commands { get; init; }
}
[JsonSerializable(typeof(CommandConfig))]
public record CommandConfig
{
    [JsonPropertyName("commands")]
    public IList<Command> Commands { get; init; }
}


and this is the json file:
{
    "commands": [
        {
            "command": "log",
            "allowedFlags": ["pretty"],
            "arguments": [],
            "flags": {
                "Name": "pretty",
                "Value": "%h~%cn~%ch~%cI~%s"
            }
        }
    ]
}
{
    "commands": [
        {
            "command": "log",
            "allowedFlags": ["pretty"],
            "arguments": [],
            "flags": {
                "Name": "pretty",
                "Value": "%h~%cn~%ch~%cI~%s"
            }
        }
    ]
}


I get this error:
Unhandled exception. System.InvalidOperationException: Each parameter in the deserialization constructor on type 'BL.Core.Command' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. Fields are only considered when 'JsonSerializerOptions.IncludeFields' is enabled. The match can be case-insensitive.                                                                                                    
Unhandled exception. System.InvalidOperationException: Each parameter in the deserialization constructor on type 'BL.Core.Command' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. Fields are only considered when 'JsonSerializerOptions.IncludeFields' is enabled. The match can be case-insensitive.                                                                                                    


Serialization has the
IncludeFields
IncludeFields
option set to true and all properties has
JsonIgnore
JsonIgnore
on them. Can someone please help?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Linq Where JsonSerialization not working
C#CC# / help
4y ago
✅ HelpMe
C#CC# / help
3y ago
helpp
C#CC# / help
2y ago