Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize and deserialize geojson in C#

Am trying to Deserialize an object to json where the location details should be converted to geojson format. Tried to achieve this using geojson.net nuget package but am not able to acheve the same. There are no examples available for geojson in net. My Object from request:

public class Request
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Fence Fence { get; set; }
}
public class Fence
{
    public int Type { get; set; }
    public List<PValues> Values { get; set; }
}

public class PValues
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

I want to convert the Request object to json that i can achieve using Newtonsoft deserialization but inside the Request PValues has to be converted to geojson polygon type how can i do this in c#?

Am new to GeoJson but when i read the specification the polygon specification is something like below

    {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [80.227249, 12.901617],
            [80.227764, 12.888553],
            [80.232056, 12.89006],
            [80.233086, 12.900779],
            [80.227249, 12.901617]
          ]
        ]
      }
    }
  ]
}

So in place of values i need the above object when i deserialize Request Class.

like image 624
user3625533 Avatar asked Dec 07 '22 14:12

user3625533


1 Answers

To ensure you create the right class to map against json object, use 'paste special' feature of visual studio. What you could do with that is, create a new class,

Edit > Paste Special > Paste JSON as classes

Or simply visit http://json2csharp.com/ and chuck you json data in and click generate button...which will then give you class that you could simply copy.

FYI, VS Paste special generates below class...

    class YourClassName
    {

        public class Rootobject
        {
            public string type { get; set; }
            public Feature[] features { get; set; }
        }

        public class Feature
        {
            public string type { get; set; }
            public Properties properties { get; set; }
            public Geometry geometry { get; set; }
        }

        public class Properties
        {
        }

        public class Geometry
        {
            public string type { get; set; }
            public float[][][] coordinates { get; set; }
        }

    }

And http://json2csharp.com/ will generate below class...

public class Properties
{
}

public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public Properties properties { get; set; }
    public Geometry geometry { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public List<Feature> features { get; set; }
}

Both may work but give them a go and see which gives you more ease of use. Even one of these doesn't work, you could keep these options in mind for future reference.

like image 64
AD8 Avatar answered Dec 29 '22 04:12

AD8