Hekmatyar
Hekmatyar

Reputation: 405

JSON.NET deserialization of class containing a list of classes

So I'm trying to deserialize class "MyDoc" containing a list of classes (the problematic one's the list of MyPolyline class entities) that goes like that

class MyPoint2d
{
    public double X { get; set; }
    public double Y { get; set; }

    [JsonConstructor]
    public MyPoint2d(double x, double y)
    {
        X = x;
        Y = y;
    }
}
class MyPolyline
{
    public List<MyPoint2d> Points { get; set; }

    [JsonConstructor]
    public MyPolyline(List<Point2d> points)
    {
        Points = new List<MyPoint2d>();

        foreach (Point2d p in points)
        {
            Points.Add(new MyPoint2d(p));
        }
    }
}

class MyLine
{
  public MyPoint2d StartPoint { get; set; }
  public MyPoint2d EndPoint { get; set; }

    [JsonConstructor]
    public MyLine(Point2d st, Point2d ed)
    {
        StartPoint = new MyPoint2d(st);
        EndPoint = new MyPoint2d(ed);
    }
}

class MyDoc
{
    public List<MyLine> Lines { get; set; }
    public List<MyPolyline> Polylines { get; set; }
}

(The Point2d class is that from AutoCad geometry but I'm using that "MyPoint2d" to limit the amount of properties that are being exported to json)
So after converting the file with standard

MyDoc deserialized = JsonConvert.DeserializeObject<MyDoc>(jsonFileContent);

The list of MyLine classes after that is working fine with all the properies keeping the correct values yet reading through MyPolyline list returns a correct amount of entities with one problem which is all the points have properties X and Y equal to zero.
The json file itself is alright and I really can't come up with anything to get this thing to work. Just in case the json file structure is rather obvious but it looks like this:

{
    "Lines": [
        {
            "StartPoint": {
                "X": 1594.9640653785937,
                "Y": 1490.1708760910014
            },
            "EndPoint": {
                "X": 1455.6957137348581,
                "Y": 1381.9184832054661
            }
        }
    ],
    "Polylines": [
        {
            "Points": [
                {
                    "X": 2155.1322935779444,
                    "Y": 2022.1540617522687
                },
                {
                    "X": 2291.3057975869833,
                    "Y": 1728.326139136384
                }
            ]
        }
    ]
}

So the question would be - is there any good way to handle this?

Upvotes: 2

Views: 421

Answers (1)

Nayan
Nayan

Reputation: 164

You can remove jsonProperty attribute if you have same name of variable as in json file.

class MyPoint2d
{
    [JsonProperty("X")]
    public double X { get; set; }

    [JsonProperty("Y")]
    public double Y { get; set; }
}
class MyPolyline
{
    [JsonProperty("Points")]
    public List<MyPoint2d> Points { get; set; }
}

class MyLine
{
    [JsonProperty("StartPoint")]
    public MyPoint2d StartPoint { get; set; }

    [JsonProperty("EndPoint")]
    public MyPoint2d EndPoint { get; set; }
}

class MyDoc
{
    [JsonProperty("Lines")]
    public List<MyLine> Lines { get; set; }

    [JsonProperty("Polylines")]
    public List<MyPolyline> Polylines { get; set; }
}

Upvotes: 3

Related Questions