-1

I have this class

namespace Watermark
{
    public class Template
    {
        int Width { get; set; } = 700;
        int Height { get; set; } = 700;
        Picture Picture { get; set; }
        List<Shape> Shape { get; set; }
        List<Word> Word { get; set; }
    }

    public class Picture
    { 
        int X { get; set; }
        int Y { get; set; }
    }

    public class Shape
    { 
        List<PointF> Point { get; set; }
        string Color { get; set; }
    }

    public class Word
    { 
        string Text { get; set; }
        int Width { get; set; }
        HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Center;
        string Color { get; set; }
        string FontFamily { get; set; }
        int Size { get; set; }
        FontStyle Style { get; set; } = FontStyle.Regular;
    }
}

And I have this JSON file

{
  "folderPath": "D:\\bfi",
  "folderName": "ORI",
  "Template": {
    "Width": "700",
    "Height": "700",
    "Picture": {
      "X": "270",
      "Y": "0"
    },
    "Shape": [
      {
        "Point": [
          {
            "X": 35,
            "Y": 0
          },
          {
            "X": 260,
            "Y": 0
          },
          {
            "X": 260,
            "Y": 250
          },
          {
            "X": 147.5,
            "Y": 310
          },
          {
            "X": 35,
            "Y": 250
          },
          {
            "X": 35,
            "Y": 0
          }
        ],
        "Color": "13293D"
      },
      {
        "Point": [
          {
            "X": 0,
            "Y": 555
          },
          {
            "X": 700,
            "Y": 555
          },
          {
            "X": 700,
            "Y": 700
          },
          {
            "X": 0,
            "Y": 700
          },
          {
            "X": 35,
            "Y": 250
          },
          {
            "X": 0,
            "Y": 555
          }
        ],
        "Color": "13293D"
      }
    ],
    "Word": [
      {
        "Text": "TEST",
        "Width": 205,
        "Alignment": "Center",
        "Color": "FAA916",
        "FontFamily": "Arial",
        "Size": 40,
        "Style": "Regular"
      }
    ]
  }
}

I tried mapping the JSON file to my class

        var jsonString = File.ReadAllText("appsettings.json");
        var jsonModel = JsonSerializer.Deserialize<Template>(jsonString);

But all of the complex object is showing null, what am I doing wrong?

enter image description here

tickwave
  • 3,335
  • 6
  • 41
  • 82
  • 1
    Your properties are not public. See: [Why are some members missing when trying to print an object by serializing to JSON?](https://stackoverflow.com/q/48156976/3744182). – dbc Nov 02 '19 at 15:25
  • @SomnathGhosh: Picture is inside template because I want to map to property `Picture` inside `Template` class. `Picture Picture { get; set; }` – tickwave Nov 02 '19 at 15:27
  • @dbc: Made everything public, still same issue, I dont think public is the issue here since Width and Height is being mapped just fine. – tickwave Nov 02 '19 at 15:29
  • Width and Height are not actually deserializing; you are initializing them to 700 in your code. The value just happens to match what is defined in the JSON. I added an answer showing how to fix your code. – Brian Rogers Nov 02 '19 at 15:57
  • @BrianRogers: omg youre right – tickwave Nov 02 '19 at 15:59

1 Answers1

2

You have two issues here:

  1. Your properties are not public, as @dbc pointed out in the comments
  2. You are missing a class at the root level. You need to add this class:

    public class RootObject
    {
        public string FolderPath { get; set; }
        public string FolderName { get; set; }
        public Template Template { get; set; }
    }
    

    and deserialize into that instead of Template:

    var jsonModel = JsonSerializer.Deserialize<RootObject>(jsonString);
    

If you do both of those things, your JSON will deserialize property.

Fiddle: https://dotnetfiddle.net/DAEN7G

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300