Hi I have been trying to serialize a polygon to a variable using GeoJSON4STJ for Nettopologysuite. So far deserialization works fine, but I am unable to serialize it. Is there any way to do this?
I have added the following code to the startup file as required
public void ConfigureServices(IServiceCollection services) {
services.AddControllers()
.AddJsonOptions(options => {
options.JsonSerializerOptions.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());
});
}
And I am trying to use the following lin
geoStr = JsonSerializer.Serialize(geometry);
2
Answers
It's been a while but since people are still interested, using GeoJSON4STJ requires setting JsonSerializerOptions for System.Text.Json. This is needed for both serialization and deserialization.
Example:
If you don't specifically need NetTopologySuite.IO.GeoJSON4STJ or System.Text.Json and are fine with NetTopologySuite.IO.GeoJSON, the answer of @shage_in_excelsior will serve you just fine.
I know this question is aged and this answer doesn’t quite exactly match your ask but I think it might make your specific request for
GeoJSON4STJ
unnecessary.You can convert
NetTopologySuite
Geometry
directly to GeoJSON using `NetTopologySuite.IO’ as follows:using NetTopologySuite.IO;
var geometryData = <Your NetTopologySuite Geometry data>;
GeoJsonWriter _geoJsonWriter = new GeoJsonWriter();
var str = _geoJsonWriter.Write(geometryData);
This will very simply sidestep your need for
GeoJSON4STJ
to do the work.Hope this helps someone.