Skip to content

Polymorphism? #17

Open
Open

Description

@SebastianStehle

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

Activity

  1. matt-lethargic commented on Jul 24, 2022

    @matt-lethargic
    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?

  2. SebastianStehle commented on Jul 24, 2022

    @SebastianStehle
    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.

  3. MartinCarpentier commented on Jul 30, 2022

    @MartinCarpentier
    Contributor

    What you want works by using the geometry interface.

    IGeometryObject

  4. SebastianStehle commented on Jul 30, 2022

    @SebastianStehle
    Author

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

  5. MartinCarpentier commented on Jul 30, 2022

    @MartinCarpentier
    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.

  6. andreas-foreflight commented on Sep 9, 2022

    @andreas-foreflight

    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)}
    
  7. hu-xd commented on Jul 19, 2023

    @hu-xd

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

  8. crozone commented on Oct 16, 2023

    @crozone

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions