1

I have a JsonDocument which I need to deserialize into a known model.
The problem with deserializing is that I am not getting the inner lists filled out.
How do I go about fixing that?

My JSON looks like this:

{
          "version": 1,
          "sections": [
            {
              "label": "General Information",
              "fields": [
                {
                  "order": 1,
                  "internalName": "a"
                },
                {
                  "order": 2,
                  "internalName": "b"
                },
                {
                  "order": 3,
                  "internalName": "c"
                }
              ]
            }
          ]
        }

This is stored in a JsonDocument that I want to dererialize to a model that I have defined like this:

public class UILayout
{ 
    public long Version { get; set; }
    public Section[] Sections { get; set; }
}

public class Section
{
    public string Label { get; set; }
    public Field[] Fields { get; set; }
}

public class Field
{
    public long Order { get; set; }
    public string InternalName { get; set; }
}

But when I deserialize it, I notice that UILayout.Sections property is empty?

var serializedLayout = JsonSerializer.Deserialize<UILayout>(Layout.UILayout.RootElement.ToString());

Why is it empty? How do I deserialize the JSON to a defined model?

dbc
  • 104,963
  • 20
  • 228
  • 340
kafka
  • 573
  • 1
  • 11
  • 28
  • the top level element has only a "UILayout" property, I would guess the deserializer sees that and doesn't find a match for any properties and exits there. You probably need another class "Layout" that contains only a UILayout member – Klaycon May 07 '20 at 19:04
  • 2
    Your json is invalid, post the correct and valid one – Pavel Anikhouski May 07 '20 at 19:24
  • Are you looking for [System.Text.Json.JsonElement ToObject workaround](https://stackoverflow.com/q/58138793/3744182), which is a generic answer, or do you have some specific problem mapping the JSON to your model? – dbc May 07 '20 at 21:05
  • @PavelAnikhouski I edited that – kafka May 08 '20 at 15:14
  • 1
    You need to set `JsonSerializerOptions {PropertyNameCaseInsensitive = true}` as shown in [this answer](https://stackoverflow.com/a/58879380) to [System.Text.JSON doesn't deserialize what Newtonsoft does](https://stackoverflow.com/q/58879190/3744182). You need use the `ToObject()` method from [this answer](https://stackoverflow.com/a/59047063/3744182) to [System.Text.Json.JsonElement ToObject workaround](https://stackoverflow.com/q/58138793/3744182) that takes an options argument. Then your `JsonDocument` can be deserialized to your model, see https://dotnetfiddle.net/a5jPq7 – dbc May 08 '20 at 15:49

1 Answers1

-1

As i understan what you are expecting shoul be as this.

public class Parent
{
 public Layout layout {get; set;}
}

public class Layout {
 public UILayout UILayout {get; set;}
}

public class UILayout {
 public int version {get; set;}
 public Section[] sections {get; set;}  
}

public class Section {
 public string label {get; set;}
 public Field[] fields {get; set;}
}

class Field {
 int order {get; set;}
 string intrnalName {get; set;}
}

And therefore you have to try it like

var serializedLayout = JsonSerializer.Deserialize<Parent>(Layout.UILayout.RootElement.ToString()); 
Mertuarez
  • 901
  • 7
  • 24