Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphism? #17

Open
SebastianStehle opened this issue Jul 23, 2022 · 8 comments
Open

Polymorphism? #17

SebastianStehle opened this issue Jul 23, 2022 · 8 comments

Comments

@SebastianStehle
Copy link

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

@matt-lethargic
Copy link
Member

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
Copy link
Author

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
Copy link
Contributor

What you want works by using the geometry interface.

IGeometryObject

@SebastianStehle
Copy link
Author

SebastianStehle commented Jul 30, 2022

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

@MartinCarpentier
Copy link
Contributor

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
Copy link

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
Copy link

hu-xd commented Jul 19, 2023

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

@crozone
Copy link

crozone commented Oct 16, 2023

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:

switch (geoJsonType)
{
case GeoJSONObjectType.Point:
return value.Deserialize<Point>(options);
case GeoJSONObjectType.MultiPoint:
return value.Deserialize<MultiPoint>(options);
case GeoJSONObjectType.LineString:
return value.Deserialize<LineString>(options);
case GeoJSONObjectType.MultiLineString:
return value.Deserialize<MultiLineString>(options);
case GeoJSONObjectType.Polygon:
return value.Deserialize<Polygon>(options);
case GeoJSONObjectType.MultiPolygon:
return value.Deserialize<MultiPolygon>(options);
case GeoJSONObjectType.GeometryCollection:
return value.Deserialize<GeometryCollection>(options);
case GeoJSONObjectType.Feature:
case GeoJSONObjectType.FeatureCollection:
default:
throw new NotSupportedException("Feature and FeatureCollection types are Feature objects and not Geometry objects");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants