Can not serialise a spatial point in .NET 8 using System.Text.Json

42 Views Asked by At

I have some code to serialize a spatial point CurrentHabitat.SiteLocation (in a server side Blazor 8 app). This fails. The object CurrentHabitat was generated as part of a reverse-engineered EF model from an MSSQL database & the field SiteLocation started life in the database as (geography,null).

var options = new JsonSerializerOptions
        {
            Converters =
                {
                new MicrosoftSpatialGeoJsonConverter()
                },
            NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals,
            ReferenceHandler = ReferenceHandler.Preserve,
            MaxDepth=100
        };

string thisLatLong = JsonSerializer.Serialize(CurrentHabitat.SiteLocation,options);

The value of CurrentHabitat.SiteLocation shows as

{POINT (-3.89695 55.99762)}
    Area: 0
    Boundary: {GEOMETRYCOLLECTION EMPTY}
    BoundaryDimension: False
    Centroid: {POINT (-3.89695 55.99762)}
    Coordinate: {(-3.89695, 55.99762)}
    CoordinateSequence: {((-3.89695, 55.99762))}
    Coordinates: {NetTopologySuite.Geometries.Coordinate[1]}
    Dimension: P
    Envelope: {POINT (-3.89695 55.99762)}
    EnvelopeInternal: {Env[-3.89695 : -3.89695, 55.99762 : 55.99762]}
    Factory: {GeometryFactory[PM=Floating, SRID=4326, CSFactory=CoordinateArraySequenceFactory, GeometryOverlay:Legacy]}
    GeometryType: "Point"
    InteriorPoint: {POINT (-3.89695 55.99762)}
    IsEmpty: false
    IsGeometryCollection: false
    IsRectangle: false
    IsSimple: true
    IsValid: true
    Length: 0
    M: NaN
    NumGeometries: 1
    NumPoints: 1
    OgcGeometryType: Point
    PointOnSurface: {POINT (-3.89695 55.99762)}
    PrecisionModel: {Floating}
    SRID: 4326
    SortIndex: Point
    UserData: null
    X: -3.89695
    Y: 55.99762
    Z: NaN

The serialization fails: {"Specified argument was out of the range of valid values. (Parameter 'X called on empty Point')"}

And an associated stack trace shows:

at NetTopologySuite.Geometries.Point.get_X()
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
   at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
   at HabitatHub3.Pages.ShowPond.<onSaveClick>d__42.MoveNext() in C:\Users\Blahblah\source\repos\HabitatHub3\HabitatHub3\Pages\ShowPond.razor:line 371

What I am doing wrong?

1

There are 1 best solutions below

0
Guru Stron On

What I am doing wrong?

It seems that you are using MicrosoftSpatialGeoJsonConverter from Microsoft.Azure.Core.Spatial, while serializing Point from NetTopologySuite, either convert it to corresponding type from Microsoft.Spatial or try using JSON converter from the NetTopologySuite.IO.GeoJSON4STJ:

var options = new JsonSerializerOptions
{
    Converters =
    {
        new NetTopologySuite.IO.Converters.GeoJsonConverterFactory()
    },
    ...
}