I know there is a lot of questions about this on SO but I still can't figure why this deserialization is not working. Maybe one more pair of eyes helps me with this.
I have this DTOs:
public class BasicDTO
{
public OperatorEnum Operator {get;set;}
public List<BasicChildrenDTO> Children {get;set;}
}
public class BasicChildrenDTO
{
public string Name {get;set;}
public ChildOperatorEnum Operator {get;set;}
public string Value {get;set;}
}
Then I have a windows form text box where I input a json and my methods is like this:
private void button1_Click(object sender, EventArgs e)
{
var str = textBox1.Text;
var object = JsonSerializer.Deserialize<BasicDTO>(str);
//more Code
}
But when I debug my object it came with all properties as null and enums as default. But, deserializer recognize how many objects are in the list, so in my json put 1, 2 or x children object children length is 1, 2 or x (but with null properties).
I Tried the special paste with my json to look for any errors and this is the result
public class Rootobject
{
public string _operator { get; set; }
public Children[] children { get; set; }
}
public class Children
{
public string name { get; set; }
public string _operator { get; set; }
public string value { get; set; }
}
Any help? Thank you
EDIT This is my json code:
{
"operator": "OR",
"children": [
{
"name": "Foo",
"operator": "EQUALS",
"value": "Foo"
},
{
"name": "Bar",
"operator": "EQUALS",
"value": "Bar"
}
]
}
And this is how is received:
"{\n "operator": "OR",\n "children": [\n {\n "name": "Foo",\n "operator": "EQUALS",\n "value": "Foo"\n },\n {\n "name": "Bar",\n "operator": "EQUALS",\n "value": "Bar"\n }\n ]\n}"
I'm using System.Text.Json and I tried both with lower and capitalize first char but none of them worked.
EDIT 2
This question is already close and answered but I edit with my correct code for anyone helps. My two problems was that my deserializacion becames with null properties because the case-sensitive problem. Then I had problem with the deserialization of the enums. So my final code for that was:
private void button1_Click(object sender, EventArgs e)
{
var str = textBox1.Text;
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() }
};
var object = JsonSerializer.Deserialize<BasicDTO>(str, options);
//more code
}