C
C#6mo 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
sibber6mo 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
המשורר
המשוררOP6mo 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
sibber6mo 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
המשורר
המשוררOP6mo ago
so flags should be:
"flags": {
"flagname": "flagvalue",
}
"flags": {
"flagname": "flagvalue",
}
sibber
sibber6mo ago
wait so is this the correct json? because this is now an array of objects
המשורר
המשוררOP6mo ago
still incorrect
sibber
sibber6mo ago
im lost is this your api? what do you want to do exactly?
המשורר
המשוררOP6mo 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
sibber6mo 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
המשורר
המשוררOP6mo ago
There is but it doesn't matter ig
sibber
sibber6mo ago
wdym? that's why you're getting the exception
המשורר
המשוררOP6mo 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
sibber6mo ago
and whats the entire json youre deserializing?
המשורר
המשוררOP6mo 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
sibber6mo ago
lol i forgot how json worked this is wrong this is what your model looks like in json:
MODiX
MODiX6mo ago
sibber
REPL Result: Success
var t = new Command("test", [], [new("pretty", "test")], []);
var c = System.Text.Json.JsonSerializer.Serialize(t);

return c;

class Root
{
public Command[] commands { get; set; }
}

class Command
{

public string Name { get; }
public HashSet<string> _allowedFlags { get; }
public List<Flag> _flags { get; }
public List<string> _arguments {get; }

[System.Text.Json.Serialization.JsonConstructor]
public Command(
string name,
HashSet<string> allowedFlags,
List<Flag> flags,
List<string> arguments)
{
Name = name;
_allowedFlags = allowedFlags;
_flags = flags;
_arguments = arguments;
}
}
record Flag(string Name, string Value);
var t = new Command("test", [], [new("pretty", "test")], []);
var c = System.Text.Json.JsonSerializer.Serialize(t);

return c;

class Root
{
public Command[] commands { get; set; }
}

class Command
{

public string Name { get; }
public HashSet<string> _allowedFlags { get; }
public List<Flag> _flags { get; }
public List<string> _arguments {get; }

[System.Text.Json.Serialization.JsonConstructor]
public Command(
string name,
HashSet<string> allowedFlags,
List<Flag> flags,
List<string> arguments)
{
Name = name;
_allowedFlags = allowedFlags;
_flags = flags;
_arguments = arguments;
}
}
record Flag(string Name, string Value);
Result: string
{"Name":"test","_allowedFlags":[],"_flags":[{"Name":"pretty","Value":"test"}],"_arguments":[]}
{"Name":"test","_allowedFlags":[],"_flags":[{"Name":"pretty","Value":"test"}],"_arguments":[]}
Compile: 473.919ms | Execution: 80.263ms | React with ❌ to remove this embed.
sibber
sibber6mo ago
typing code on my phone is a pita
המשורר
המשוררOP6mo ago
How can I make it without underscore
sibber
sibber6mo ago
thats just because i copy pasted the properties because im lazy you can name them whatever you want and make it case insensitive
Want results from more Discord servers?
Add your server