private void TestRoundTrip <TCoordinates>(string expected, GeoJsonGeometryCollection <TCoordinates> geometryCollection) where TCoordinates : GeoJsonCoordinates { var json = geometryCollection.ToJson(); Assert.AreEqual(expected, json); var rehydrated = BsonSerializer.Deserialize <GeoJsonGeometryCollection <TCoordinates> >(json); Assert.AreEqual(expected, rehydrated.ToJson()); }
/// <summary> /// Parses the specified <paramref name="json"/> object into an instance deriving from <see cref="GeoJsonObject"/>. /// /// As the GeoJSON format specified the type of a /// </summary> /// <param name="json">The JSON object.</param> /// <returns>An instance that derives from <see cref="GeoJsonObject"/>.</returns> private static GeoJsonObject Parse(JObject json) { if (json == null) { return(null); } // Get the value of the "type" property string type = json.GetString("type"); if (string.IsNullOrWhiteSpace(type)) { throw new GeoJsonParseException("The JSON object doesn't specify a type", json); } // Parse the type into an enum if (EnumUtils.TryParseEnum(type, out GeoJsonType result) == false) { throw new GeoJsonParseException($"Unknown type {type}", json); } switch (result) { case GeoJsonType.Feature: return(GeoJsonFeature.Parse(json)); case GeoJsonType.FeatureCollection: return(GeoJsonFeatureCollection.Parse(json)); case GeoJsonType.Point: return(GeoJsonPoint.Parse(json)); case GeoJsonType.LineString: return(GeoJsonLineString.Parse(json)); case GeoJsonType.Polygon: return(GeoJsonPolygon.Parse(json)); case GeoJsonType.MultiPoint: return(GeoJsonMultiPoint.Parse(json)); //case GeoJsonType.MultiLineString: // return GeoJsonMultiLineString.Parse(obj); case GeoJsonType.MultiPolygon: return(GeoJsonMultiPolygon.Parse(json)); case GeoJsonType.GeometryCollection: return(GeoJsonGeometryCollection.Parse(json)); default: throw new GeoJsonParseException($"Unknown type {type}", json); } }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The value.</param> protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, GeoJsonGeometryCollection <TCoordinates> value) { _helper.SerializeMembers(context, value, SerializeDerivedMembers); }
private void SerializeDerivedMembers(BsonSerializationContext context, GeoJsonGeometryCollection <TCoordinates> value) { SerializeGeometries(context, value.Geometries); }