AnsweredAssumed Answered

JSONConverterGeometryClass creates invalid geometries

Question asked by HimBromBeere on May 22, 2018
Latest reply on May 24, 2018 by HimBromBeere

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;

    jsonDeserializer.InitDeserializer(jsonReader, null);
    geometry = ((IJSONConverterGeometry)jsonDeserializer).ReadGeometry(jsonReader, esriGeometryType.esriGeometryAny, false, false);

    return geometry;

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;
line.PutCoords(poin1, point2);

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.