/// <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); } }
/// <inheritdoc /> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } if (reader.TokenType != JsonToken.StartObject) { return(null); } JObject obj = JObject.Load(reader); string type = obj.Value <string>("type"); switch (type?.ToLower()) { case "feature": return(GeoJsonFeature.Parse(obj)); case "featurecollection": return(GeoJsonFeatureCollection.Parse(obj)); case "point": return(GeoJsonPoint.Parse(obj)); case "multipoint": return(GeoJsonMultiPoint.Parse(obj)); case "linestring": return(GeoJsonLineString.Parse(obj)); case "multilinestring": return(GeoJsonMultiLineString.Parse(obj)); case "polygon": return(GeoJsonPolygon.Parse(obj)); case "multipolygon": return(GeoJsonMultiPolygon.Parse(obj)); default: if (objectType == typeof(GeoJsonProperties)) { return(ReadJsonProperties(reader)); } throw new GeoJsonParseException($"Unknown shape: {type}", obj); } }
public void Parse1() { GeoJsonMultiPolygon multiPolygon = GeoJsonMultiPolygon.Parse(Json1); Assert1(multiPolygon); }