I will be attempting to create a C# library to serialize objects to GeoJSON using Json.NET ( for serialization ) and GeoAPI.NET ( for geometry definitions ).
I have thought about two different approaches for the serialization implementation and I am not clear which one would be the best approach. They are:
Approach 1 - Custom Attributes
The first approach involves creating several custom attributes that could be applied to any class to modify the serialization. For instance, a class might be decorated like so:
[GeoJsonFeature]
public class Building
{
[GeoJsonId]
public Guid Id { get; set; }
[GeoJsonProperty]
public string Name { get; set; }
[GeoJsonProperty]
public int Floorcount { get; set; }
[GeoJsonGeometry]
public GeoAPI.Geometries.IGeometry Geometry { get; set; }
}
Serializing the object would then be as simple as:
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.Data = building;
return jsonNetResult;
The advantage of this approach is that any business object could be turned into a GeoJSON object assuming it has the required properties (e.g., Geometry ). The disadvantage would be that I would need to create a number of custom attributes to support the serialization. Additionally, this has the affect of 'muddying' the business object.
Finally, I have not yet determined if this approach is even possible with JSON.NET, though it seems that it will be.
Approach 2 - Custom JsonConverter
The second approach involves creating custom converters for various types. For instance I might have a GeoJsonConverter that when passed an object of a given