I have a simple model that contains a NetTopologySuite Polygon:
public class MyModel
{
[Newtonsoft.Json.JsonConverter(typeof(MyPolygonConverter))]
public NetTopologySuite.Geometries.Polygon poly { get; set; }
}
And I've build a custom converter, using NetTopologySuite.IO.GeoJson:
public class MyPolygonConverter : JsonConverter<Polygon>
{
public override void WriteJson(JsonWriter writer, Polygon value, JsonSerializer serializer)
{
var geoJsonSerializer = NetTopologySuite.IO.GeoJsonSerializer.Create();
using var stringWriter = new StringWriter();
using var jsonWriter = new JsonTextWriter(stringWriter);
geoJsonSerializer.Serialize(jsonWriter, value);
var geoJson = stringWriter.ToString();
writer.WriteValue(geoJson);
}
...
}
And then I create a model object and serialize it:
var model = new MyModel
{
poly = new Polygon(new LinearRing(new[]
{
new Coordinate(-100, 45),
new Coordinate(-98, 45),
new Coordinate(-99, 46),
new Coordinate(-100, 45),
}))
};
var geoJson = JsonConvert.SerializeObject(model, Formatting.None,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
});
And what I get contains the Polygon as an escaped GeoJson string:
{
"poly" : "{\"type\":\"Polygon\",\"coordinates\":[[[-100.0,45.0],[-98.0,45.0],[-99.0,46.0],[-100.0,45.0]]]}"
}
What I want is the Polygon as json, in the json object:
{
"poly" : {
"type" : "Polygon",
"coordinates" : [
[
[-100.0,45.0],
[-98.0,45.0],
[-99.0,46.0],
[-100.0,45.0]
]
]
}
}
What I'm thinking is that I shouldn't be using a JsonConverter at all, but something else.
But what?