using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
[Newtonsoft.Json.JsonConverter(typeof(MyPolygonConverter))]
public NetTopologySuite.Geometries.Polygon poly { get; set; }
public class MyPolygonConverter : JsonConverter<Polygon>
public override void WriteJson(JsonWriter writer, Polygon value, JsonSerializer serializer)
var geoJsonSerializer = NetTopologySuite.IO.GeoJsonSerializer.Create();
geoJsonSerializer.Serialize(writer, value);
public override Polygon ReadJson(JsonReader reader, Type objectType, Polygon existingValue, bool hasExistingValue, JsonSerializer serializer)
throw new NotImplementedException();
public static void Test()
poly = new Polygon(new LinearRing(new[]
new Coordinate(-100, 45),
new Coordinate(-100, 45),
var geoJson = JsonConvert.SerializeObject(model, Formatting.Indented,
new JsonSerializerSettings
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
Console.WriteLine(geoJson);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];