How to serialize and deserialize geojson in C#

geojson.net example c#
geojson.net featurecollection
deserialize json c#
nettopologysuite geojson
geojson net core
geojson format
geojson rfc7946
c# read geojson file

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.

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.

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  To deserialize from a file by using synchronous code, read the file into a string, as shown in the following example: jsonString = File.ReadAllText(fileName); weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString); To deserialize from a file by using asynchronous code, call the DeserializeAsync method:

To be able to serialize and deserialize properly your class structure should be similar to this:

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; }
}

GeoJSON-Net/GeoJSON.Net: .Net library for GeoJSON , 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  DataContractJsonSerializer class helps to serialize and deserialize JSON. It is present in namespace System.Runtime.Serialization.Json which is available in assembly System.Runtime.Serialization.dll. Using the class we can serialize an object into JSON data and deserialize JSON data into an object.

Using the GeoJSON.Net library modify your model as follows :

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 FeatureCollection Values { get; set; }
}

Initialize a request object :

var polygon = new Polygon(new List<LineString>
{
    new LineString(new List<IPosition>
    {
        new Position(20.236237,39.4116761),
        new Position(20.2363602,39.4115249),
        new Position(20.2365152,39.4110652),
        new Position(20.2364942,39.4104468),
        new Position(20.236237,39.4116761),
    })
});
var featureCollection = new FeatureCollection(new List<Feature>()
{
    new Feature(polygon)
});
var request = new Request()
{
    Id = 1,
    Name = "MyRequest",
    Fence = new Fence()
    {
        Type = 2,
        Values = featureCollection
    }
};

And finally serialize the object:

var json = JsonConvert.SerializeObject(request);

How to serialize and deserialize JSON using C#, Net library for GeoJSON types & corresponding Json. and provides Newtonsoft Json.NET converters for serialization and deserialization of GeoJSON data. In simply Serialization refers to converting a custom .NET object into JSON string format and Deserialization is the opposite of the serialization. When JSON string is transmitted over the network, by using deserialization, it will be converted to a custom object. From the below example, it will be more clear.

Serialize and deserialize JSON using C#, Json namespace to serialize and deserialize to and from JavaScript Object Notation (JSON). If you're porting existing code from Newtonsoft. Some instructions on how to use Newtonsoft JSON to serialize and deserialize your objects in C#. Step 1: Create a new Visual Studio project. Just a simple console application will do: Step 2: Install Newtonsoft Json using Nuget.

GeoJSON.Net Serializing to valid GeoJson file, Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). The library design emphasizes  In this blog post, you will learn how to serialize and deserialize JSON objects using the Newtonsoft library with an example in C#. Keep reading on What is the difference between abstract class and interface in C#, How to check the .net framework version.

GeoJSon to C# Object using Json.NET, MultiPoint to a VALID MultiPoint GeoJSON file? All I can find is how to serialize a random Geometry object to a JSON file that cannot be read for  The next thing is to write the code to deserialize the object. Add a click event for Deserialize button. When clicking on the Deserialize button we want to deserialize the object and show its values on screen.

Comments
  • your json is invaid
  • Updated the question with valid geojson specification