C
C#3mo ago
המשורר

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 option set to true and all properties has JsonIgnore on them. Can someone please help?
19 Replies
sibber
sibber3mo ago
flags isnt a list its a Dict<string, string> but thats a weird api why even make that a dictionary i think in this case youd need an ordered list of key value pairs and manually map it
המשורר
המשורר3mo ago
youre correct i've changed it to a list of dict<string, string>
"flags": [
{
"name": "pretty",
"value": "%h~%cn~%ch~%cI~%s"
}
]
"flags": [
{
"name": "pretty",
"value": "%h~%cn~%ch~%cI~%s"
}
]
still not working though
sibber
sibber3mo ago
right but now the issue is when you have multiple flags youre going to have more than one "name" key and more than one "value" key i assume see what i mean? oh
המשורר
המשורר3mo ago
so flags should be:
"flags": {
"flagname": "flagvalue",
}
"flags": {
"flagname": "flagvalue",
}
sibber
sibber3mo ago
wait so is this the correct json? because this is now an array of objects
המשורר
המשורר3mo ago
still incorrect
sibber
sibber3mo ago
im lost is this your api? what do you want to do exactly?
המשורר
המשורר3mo ago
flag is a simple record
public record Flag(string Name, string Value);
public record Flag(string Name, string Value);
maybe I should've done this one different? it says the problem is in the Command class
sibber
sibber3mo ago
in this case it would serialize to
[
{ "pretty": "blabla" },
...
]
[
{ "pretty": "blabla" },
...
]
but what are you trying to do? if you don't want the serializer to use the constructor there's probably an attribute for that
המשורר
המשורר3mo ago
There is but it doesn't matter ig
sibber
sibber3mo ago
wdym? that's why you're getting the exception
המשורר
המשורר3mo ago
I created this
[JsonConstructor]
public Command(
string name,
HashSet<string> allowedFlags,
List<Flag> flags,
List<string> arguments)
{
Name = name;
_allowedFlags = allowedFlags;
_flags = flags;
_arguments = arguments;
}
[JsonConstructor]
public Command(
string name,
HashSet<string> allowedFlags,
List<Flag> flags,
List<string> arguments)
{
Name = name;
_allowedFlags = allowedFlags;
_flags = flags;
_arguments = arguments;
}
is has everything even names are the same between json and variables like it should still error
sibber
sibber3mo ago
and whats the entire json youre deserializing?
המשורר
המשורר3mo ago
{
"commands": [
{
"name": "log",
"allowedFlags": [ "pretty" ],
"arguments": [],
"flags": [
{
"pretty": "%h~%cn~%ch~%cI~%s"
}
]
}
]
}
{
"commands": [
{
"name": "log",
"allowedFlags": [ "pretty" ],
"arguments": [],
"flags": [
{
"pretty": "%h~%cn~%ch~%cI~%s"
}
]
}
]
}
commands is a list of commands
sibber
sibber3mo ago
lol i forgot how json worked this is wrong this is what your model looks like in json:
Want results from more Discord servers?
Add your server