I´m creating a SeverObjectExtension using ArcObjects for Server 10.6. Here I have a method that reads a geometry from HTTP-request as follows:
public IGeometry Json2Geometry(JsonObject json)
IGeometry geometry = null;
IJSONReader jsonReader = new JSONReaderClass();
IJSONDeserializer jsonDeserializer = null;
jsonDeserializer = new JSONConverterGeometry() as IJSONDeserializer;
geometry = ((IJSONConverterGeometry)jsonDeserializer).ReadGeometry(jsonReader, esriGeometryType.esriGeometryAny, false, false);
Now I´m doing something with that geometry. In my case I cast it to IPolyline:
if(geometry.GeometryType == esriGeometryPolyline)
var polyline = (IPolyline) geometry;
I get an InvalidCastException on the coe above. So what does ReadGeometry return if not an instance of IPolyline (in case of geometrytype polyline as shown above).
The JSON looks like this:
I also tried to create a completely new polyline based on two points. Then I converted that geometry to json using the following:
ILine line = new LineClass();
(line as IGeometry).SpatialReference = point1.SpatialReference;
object optional = Missing.Value;
IPolyline pline = new PolylineClass();
((ISegmentCollection)pline).AddSegment((ISegment)line, ref optional, ref optional);
((IJSONConverterGeometry)jsonSerializer).WriteGeometry(jsonWriter, null, pline, false);
When I convert that back to an IGeometry using ReadGeometry as mentioned above I also get that exception.
Update: I also tried to use the less generic method ReadPolyline, which should return an IPolyline. Instead I get the mentioned exception. So whatever the method returns, it´s not an IPolyline.