private static void WriteGeography(JsonWriter writer, Geography geography) { writer.WriteStartObject(); if (geography is GeographyPoint) { WriteGeographyElement <GeographyPoint>(writer, GeoJsonConstants.GeoTypes.Point, (GeographyPoint)geography, WritePointCoordinates); } else if (geography is GeographyLineString) { WriteGeographyElement <GeographyLineString>(writer, GeoJsonConstants.GeoTypes.LineString, (GeographyLineString)geography, WriteLineStringCoordinates); } else if (geography is GeographyPolygon) { WriteGeographyElement <GeographyPolygon>(writer, GeoJsonConstants.GeoTypes.Polygon, (GeographyPolygon)geography, WritePolygonCoordinates); } else if (geography is GeographyMultiPoint) { WriteGeographyElement <GeographyMultiPoint>(writer, GeoJsonConstants.GeoTypes.MultiPoint, (GeographyMultiPoint)geography, WriteMultiPointCoordinates); } else if (geography is GeographyMultiPoint) { WriteGeographyElement <GeographyMultiLineString>(writer, GeoJsonConstants.GeoTypes.MultiLineString, (GeographyMultiLineString)geography, WriteMultiLineStringCoordinates); } else if (geography is GeographyMultiPolygon) { WriteGeographyElement <GeographyMultiPolygon>(writer, GeoJsonConstants.GeoTypes.MultiPolygon, (GeographyMultiPolygon)geography, WriteMultiPolygonCoordinates); } else if (geography is GeographyCollection) { WriteRawGeographyElement <GeographyCollection>(writer, GeoJsonConstants.GeoTypes.GeometryCollection, (GeographyCollection)geography, GeoJsonConstants.GeometriesPropertyName, WriteGeographyCollection); } else { throw Errors.GeometryTypeNotSupported(geography.GetType()); } writer.WriteEndObject(); }
/// <inheritdoc/> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } Geography result = ProcessSingleGeography(reader); Type resultType = result.GetType(); if (!objectType.IsAssignableFrom(resultType)) { throw new JsonSerializationException($"Deserialization failed: Discovered GeoJson of type '{resultType.Name}', expected '{objectType.Name}'."); } return(result); }
/// <inheritdoc/> public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Null) { return(null); } Geography result = ProcessSingleGeography(ref reader); Type resultType = result.GetType(); if (!typeToConvert.IsAssignableFrom(resultType)) { throw new JsonException($"Deserialization failed. Discovered GeoJson of Type '{resultType.Name}', expected '{typeToConvert.Name}'."); } return(result); }
/// <summary> /// Get the Well Known Text format that DbGeography supports. This is a little different from the Well Known Text that is output /// by Microsoft.Data.Spatial.Geography types, so we can't just directly use that representation here. /// </summary> /// <param name="geography">Geography instance to convert to Well Known Text.</param> /// <returns>Well Known Text for the specified geography instance.</returns> private static string GetWellKnownText(Geography geography) { string extendedWKT = geography.ToString(); int semicolon = extendedWKT.IndexOf(';'); Assert.IsTrue(semicolon > 0, "Expected to find a semicolon in the extended WellKnownText output (using ToString) for the type {0}", geography.GetType()); return extendedWKT.Substring(semicolon + 1); }
/// <summary> /// This mimics what is in the product, but keeping this separate implementation so if the product changes this test will pick it up. /// </summary> /// <param name="geography"></param> /// <param name="writer"></param> private static void WriteGeography(Geography geography, XmlWriter writer) { Assert.IsNotNull(geography, "Can't write a null geography."); Assert.IsNotNull(writer, "XmlWriter is null."); Type type = geography.GetType(); if (typeof(GeographyPoint).IsAssignableFrom(type)) { writer.WriteStartElement(GmlPrefix, GmlPoint, UnitTestsUtil.GmlNamespace.NamespaceName); writer.WriteStartElement(GmlPrefix, GmlPosition, UnitTestsUtil.GmlNamespace.NamespaceName); writer.WriteValue(ToPointString((GeographyPoint)geography)); writer.WriteEndElement(); writer.WriteEndElement(); } else if (typeof(GeographyLineString).IsAssignableFrom(type)) { writer.WriteStartElement(GmlPrefix, GmlLineString, UnitTestsUtil.GmlNamespace.NamespaceName); for (int i = 0; i < ((GeographyLineString)geography).Points.Count; ++i) { writer.WriteElementString(GmlPrefix, GmlPosition, UnitTestsUtil.GmlNamespace.NamespaceName, ToPointString(((GeographyLineString)geography).Points[i])); } writer.WriteEndElement(); } else { Assert.Fail("DSPResourceAtomSerializer does not support writing the Geography type '{0}'. If this is a valid type, the WriteGeography method needs to be updated.", type.Name); } }