המשורר
המשורר
CC#
Created by המשורר on 7/6/2024 in #help
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?
47 replies
CC#
Created by המשורר on 5/12/2024 in #help
C# array init
if I initialize an array of structs, which constructor does it use if any?
MyStruct[,] array = new MyStruct[2, 3];

struct MyStruct {
public MyStruct() {
// ctor
}

public MyStruct(int x) {
// another ctor
}
}
MyStruct[,] array = new MyStruct[2, 3];

struct MyStruct {
public MyStruct() {
// ctor
}

public MyStruct(int x) {
// another ctor
}
}
85 replies
CC#
Created by המשורר on 4/21/2023 in #help
❔ SQL syntax error - INSERT INTO statement
query looks like this
string query = $"INSERT INTO users " +
$"(fname, username, email, password, age, gender, state) " +
$"VALUES ({fname}, {username}, {email}, {password}, {age}, {gender}, {state})";
string query = $"INSERT INTO users " +
$"(fname, username, email, password, age, gender, state) " +
$"VALUES ({fname}, {username}, {email}, {password}, {age}, {gender}, {state})";
get a syntax error please keep in mind this is something for school, stuff like SQL Injections and other new features are dismissible thanks in advance, benAmi
17 replies