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:
<SPAN class="keyword token">public</SPAN> IGeometry <SPAN class="token function">Json2Geometry</SPAN><SPAN class="punctuation token">(</SPAN>JsonObject json<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
IGeometry geometry <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">;</SPAN>
IJSONReader jsonReader <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">JSONReaderClass</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
jsonReader<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ReadFromString</SPAN><SPAN class="punctuation token">(</SPAN>json<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ToJson</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
IJSONDeserializer jsonDeserializer <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">;</SPAN>
jsonDeserializer <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">JSONConverterGeometry</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> IJSONDeserializer<SPAN class="punctuation token">;</SPAN>
jsonDeserializer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">InitDeserializer</SPAN><SPAN class="punctuation token">(</SPAN>jsonReader<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
geometry <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>IJSONConverterGeometry<SPAN class="punctuation token">)</SPAN>jsonDeserializer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">ReadGeometry</SPAN><SPAN class="punctuation token">(</SPAN>jsonReader<SPAN class="punctuation token">,</SPAN> esriGeometryType<SPAN class="punctuation token">.</SPAN>esriGeometryAny<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">return</SPAN> geometry<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Now I´m doing something with that geometry. In my case I cast it to IPolyline:
<SPAN class="keyword token">if</SPAN><SPAN class="punctuation token">(</SPAN>geometry<SPAN class="punctuation token">.</SPAN>GeometryType <SPAN class="operator token">==</SPAN> esriGeometryPolyline<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> polyline <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>IPolyline<SPAN class="punctuation token">)</SPAN> geometry<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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:
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"paths"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">486893.89699999994</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">5755807.910000002</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">486904.18500000244</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">5755744.383000003</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"spatialReference"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"wkid"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">25832</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"latestWkid"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">25832</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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 <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">LineClass</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">(</SPAN>line <SPAN class="keyword token">as</SPAN> IGeometry<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>SpatialReference <SPAN class="operator token">=</SPAN> point1<SPAN class="punctuation token">.</SPAN>SpatialReference<SPAN class="punctuation token">;</SPAN>
line<SPAN class="punctuation token">.</SPAN><SPAN class="token function">PutCoords</SPAN><SPAN class="punctuation token">(</SPAN>poin1<SPAN class="punctuation token">,</SPAN> point2<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">object</SPAN> optional <SPAN class="operator token">=</SPAN> Missing<SPAN class="punctuation token">.</SPAN>Value<SPAN class="punctuation token">;</SPAN>
IPolyline pline <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">PolylineClass</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>ISegmentCollection<SPAN class="punctuation token">)</SPAN>pline<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">AddSegment</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>ISegment<SPAN class="punctuation token">)</SPAN>line<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">ref</SPAN> optional<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">ref</SPAN> optional<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>ITopologicalOperator<SPAN class="punctuation token">)</SPAN>pline<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">Simplify</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>IJSONConverterGeometry<SPAN class="punctuation token">)</SPAN>jsonSerializer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">WriteGeometry</SPAN><SPAN class="punctuation token">(</SPAN>jsonWriter<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">,</SPAN> pline<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.