Use with MongoDB
See original GitHub issueThis library is nice, but we can not easily use classes like Point, MultiPoints… in our POCO mapped to MongoDB document. Imagine a document like that :
{
"_id": "5a7b8a6f70217a0c44b0ecaa",
"name": "Foo",
"position": {
"type": "Point",
"coordinates": ["2.879", "45.5931"]
}
}
Deserialization of document by C# MongoDB driver fails because it cannot deserialize the position embedded document as the type field does not exists in the Point class : there is no type attribute, neither setter for Type property.
Error message is :
FormatException: An error occurred while deserializing the Position property of class MyProject.MyClass : Element 'type' does not match any field or property of class GeoJSON.Net.Geometry.Point.
Can we consider to have a private setter on Type property. The property could be available in the abstract base class GeoJSONObject, and its constructor could take the type as parameter and set it.
public abstract class GeoJSONObject : ...
{
public GeoJSONObject(GeoJSONObjectType type)
{
this.Type = type;
}
public GeoJSONObjectType Type { get; private set }
.....
}
So geometry classes could be simplified like this :
public class Point : GeoJSONObject, ...
{
public Point(IPosition coordinates) : base(GeoJSONObjectType.Point)
{
this.Coordinates = coordinates ?? throw new ArgumentNullException(nameof(coordinates));
}
// This property is already available in base class
// public override GeoJSONObjectType Type => GeoJSONObjectType.Point;
....
}
What do you think ?
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (3 by maintainers)

Top Related StackOverflow Question
I’m struggling with this as well. I’m experimenting registering class maps (BsonClassMap.RegisterClassMap) for each relevant class in GeoJSON.NET package.
I would like to have House come through WebAPI and serialize it straight like that into MongoDB:
class House { public Point Location {get;set;}; }However some fields such as type and coordinates are not being stored in Mongodb.
EDIT: ended up writing my own POCOs for GeoJSON. If I tried using MongoDB C# driver provided GeoJSON, WebAPI wouldn’t work. With these, Mongo serialization wouldn’t work.
For those still looking for a solution, I have created a MongoDB.NetTopologySuite.Serialization package which adds support for serializing/deserializing NetTopologySuite models to BSON and back.