I want to import json data from within the class which is the target of the deserialization. Is this possible with System.Text.Json without additional mapping? Ideally I would use "this" instead of the generic type parameter. I know that is impossible, but is there a similar option? Here is my test code which works, because it creates the data object only to map it to the property. Ideally, I would not need to instantiate "Test" twice.
public class Test
{
public string? Bar { get; set; }
public void ImportJson(string payload)
{
var data = System.Text.Json.JsonSerializer.Deserialize<Test>(payload);
Bar = data?.Bar; // Don't want to map
}
}
string foo = "{ \"Bar\": \"baz\" }";
var t = new Test();
t.ImportJson(foo);
Console.WriteLine(t.Bar);