0

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
}
Weenhallo
  • 324
  • 2
  • 8
  • Can you an example of the json input you used? – Fildor May 17 '22 at 15:37
  • Ah, wait a sec: Realize how the names are lowercase? Are you using NewtonSoft.JSON or System.Text.Json? – Fildor May 17 '22 at 15:39
  • For System.Text.Json see [How to customize property names and values with System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties?pivots=dotnet-6-0) and/or [How to enable case-insensitive property name matching with System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-casing) – Fildor May 17 '22 at 15:40
  • ^^ The System.Text.Json deserializer defaults to case-sensitive matching. The second link I posted above teaches you how to switch to case-insesitive matching. _I tried the special paste with my json to look for any errors"_ - that was an excellent idea! – Fildor May 17 '22 at 15:44
  • You have to show your json that you are trying to deserialize. – Serge May 17 '22 at 15:48
  • Updated with my json – Weenhallo May 17 '22 at 15:52
  • I tried your solutions Fildor but with case-insensitive enabled I get this error: The JSON value could not be converted to operator. Path: $.Operator | LineNumber: 1 | – Weenhallo May 17 '22 at 15:55
  • As mentioned in comments above, System.Text.Json is case sensitive. See [JsonSerializer.Deserialize fails](https://stackoverflow.com/q/60123376/3744182) for a duplicate question + answers. – dbc May 17 '22 at 16:08
  • 2
    Regarding the new error *The JSON value could not be converted to operator. Path: $.Operator | LineNumber: 1* this is likely occurring because you are now successfully trying to deserialize the `Operator` enum and so have exposed a second bug which had been hidden by the first. This second issue is probably a duplicate of [ASP.NET MVC Core API Serialize Enums to String](https://stackoverflow.com/q/59096102/3744182) but if not, please ask a second question with a [mcve]. – dbc May 17 '22 at 16:09
  • 1
    Have you tried to explicitly use the JsonStringEnumConverter ? To tell the system search for strings not for intégral values – Romka May 17 '22 at 16:12
  • 1
    Finally it was the the two problems: case sensitive and deserialization of enums. So much thank you everyone! – Weenhallo May 18 '22 at 07:25

0 Answers0