Suppose I have this classes:
internal class Test
{
public string value { get; set; }
public Test(string value)
{
this.value = value;
}
}
public class Response<T>
{
private string errorMessage { get; }
private T returnValue { get; }
public Response(string errorMessage, T returnValue)
{
this.errorMessage = errorMessage;
this.returnValue = returnValue;
}
public bool HasError() { return errorMessage != ""; }
public bool AssertEqualsReturnValue(object obj) { return returnValue.Equals(obj); }
}
and I have this Main function:
string json =
@"{
""errorMessage"": ""This is an error"",
""returnValue"": {
""value"": ""this is what returns""
}
}
";
Response<Test> r = JsonSerializer.Deserialize<Response<Test>>(json); ;
Console.WriteLine(r.AssertEqualsReturnValue("this is what returns"));
Console.WriteLine(r.HasError());
I get the next error message:
System.InvalidOperationException: 'Each parameter in the deserialization constructor on type 'IntroSE.Kanban.Backend.Response`1[ConsoleApp1.Test]' 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.'
It won't desirialize because I need to somehow tell the JsonSerializer I have a Test object in returnValue key. I saw some solutions online but the problem is they all use JsonConverter which is no in use anymore in .Net 6.0.