GeoJSON-Net / GeoJSON.Text

.Net library for GeoJSON types & corresponding System.TExt.Json (de)serializers
MIT License
38 stars 9 forks source link

Polymorphism? #17

Open SebastianStehle opened 2 years ago

SebastianStehle commented 2 years ago

Have you not handled polymorphism? I do not see converters for GeoJSONObject ot the interfaces.

matt-lethargic commented 2 years ago

Can you give me an example of what can't be done without this converter so I can understand the issue a little better?

SebastianStehle commented 2 years ago

Lets say, I have the following request definition:

class CreateGeometry
{
 [Required]
 public GeoJsonObject Geometry {get; set; }
}

So the user can either give in a point or circle or polygon or whatever.

I don't see how this is possible at the moment and I also tested it and got an error.

MartinCarpentier commented 2 years ago

What you want works by using the geometry interface.

IGeometryObject

SebastianStehle commented 2 years ago

Oh, sorry ... have not seen that. Why is there both a interface and a abstract class?

MartinCarpentier commented 2 years ago

I sadly do not know, since i have not been a part of the initial implementation of the library, i just converted it to system.text.json, but i guess the abstract class exists to share common properties to all of the geometry object types. Maybe it should be internal, since it makes no sense to use it externally.

andreas-foreflight commented 2 years ago

Is there a way to get this to work with any GeoJson object, not only geometry types (i.e. also Feature and Feature Collection). Using IGeoJsonObject does not seem to work?

I would like to serialize any type of geoJson object, but this does not work:

>>> System.Text.Json.JsonSerializer.Serialize((GeoJSON.Text.IGeoJSONObject)source.Data)
"{\"type\":8,\"crs\":null,\"bbox\":null}"

I can properly serialize it as a feature collection, but this is not polymorphic:

>>> System.Text.Json.JsonSerializer.Serialize((FeatureCollection)source.Data)
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[90,50],[25,0]]},\"properties\":{}}]}"

Using IGeometryObject is not an option, because not all GeoJson objects are geometry objects:

>>> System.Text.Json.JsonSerializer.Serialize((IGeometryObject)source.Data)
'System.Text.Json.JsonSerializer.Serialize((IGeometryObject)source.Data)' threw an exception of type 'System.InvalidCastException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147467262
    HelpLink: null
    InnerException: null
    Message: "Specified cast is not valid."
    Source: "006cbf0666ad4386b97cab13a906b566"
    StackTrace: "   at <>x.<>m0(<OnLoad>d__4 <>4__this)"
    TargetSite: {System.String <>m0(<OnLoad>d__4)}
hu-xd commented 1 year ago

ditto. need to parse GeoJson data without knowing beforehand whether it is a geometry or feature or feature collection etc. how ?

crozone commented 11 months ago

It looks like this is within reach, it just requires another converter for the top level IGeoJSONObject which can switch internally on GeoJSONObjectType, similar to how the polymorphic IGeometryObject converter works:

https://github.com/GeoJSON-Net/GeoJSON.Text/blob/31ec322c320a24fdb7136620de81812fc28aaeb3/src/GeoJSON.Text/Converters/GeometryConverter.cs#L86-L107