title: Deserialise JSON to more than one data type (Geospatial system)
body: <p>After scouring SO and Google for answers I'm at a loss, so hoping someone can help me on this one!</p>
  <p>I have a project that consumes data from a 3rd party API. The data is a JSON message containing asset locations (lat and lng).</p>
  <p>But the API can support polygons and points (shown respectively below):</p>
  <pre><code>    {
      &quot;geom&quot;: {
          &quot;coordinates&quot;: [
              [
                  [
                      0.07666021585464479,
                      51.49331798632394
                  ],
                  [
                      0.07707864046096803,
                      51.49337476490021
                  ],
                  [
                      0.07717519998550416,
                      51.49315433003204
                  ],
                  [
                      0.07676750421524049,
                      51.49309087131179
                  ],
                  [
                      0.07666021585464479,
                      51.49331798632394
                  ]
              ]
          ],
          &quot;type&quot;: &quot;Polygon&quot;
      }
  }
  </code></pre>
  <p>And a point looks like this:</p>
  <pre><code>{
      &quot;geom&quot;: {
          &quot;coordinates&quot;: [
              -0.00203667,
              51.51020028
          ],
          &quot;type&quot;: &quot;Point&quot;
      }
  }
  </code></pre>
  <p>I've tried to mock this up and deserialise it with the following:</p>
  <pre><code>public class Item
  {
      [JsonProperty(&quot;geom&quot;)]
      public Geom Geom { get; set; }
  }
  
  public class Geom
  {
      [JsonProperty(&quot;coordinates&quot;)]
      public double[] Coords { get; set; }
  
      [JsonProperty(&quot;type&quot;)]
      public string Type { get; set; }
  }
  </code></pre>
  <p>And calling it with:</p>
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          new Program();
      }
  
      public Program()
      {
          var single = &quot;{\&quot;geom\&quot;:{\&quot;coordinates\&quot;:[-0.00203667,51.51020028],\&quot;type\&quot;:\&quot;Point\&quot;}}&quot;;
          var s = JsonConvert.DeserializeObject&lt;Item&gt;(single);
  
          var poly = &quot;{\&quot;geom\&quot;:{\&quot;coordinates\&quot;:[[[0.07666021585464479,51.49331798632394],[0.07707864046096803,51.49337476490021],[0.07717519998550416,51.49315433003204],[0.07676750421524049,51.49309087131179],[0.07666021585464479,51.49331798632394]]],\&quot;type\&quot;:\&quot;Polygon\&quot;}}&quot;;
          var p = JsonConvert.DeserializeObject&lt;Item&gt;(poly);
      }
  }
  </code></pre>
  <p>I understand the problem well enough, the poly string needs to be a <code>double[][]</code>. But I can't seem to get the same deserialisation process to work for both strings.</p>
  <p>It works if I deserialise them to an <code>object</code>, but then I can't use it in my concrete classes without casting everywhere.</p>
  <p>I've also seen some attempts on wrapping the 'point' deserialisation in a try/catch and using the catch to deserialise a 'poly' but this seems like a hack and there are potentially other types of coordinate systems in the API.</p>
  <p>I'm using Newtonsoft.Json and I've looked into all different approaches, the closest I've gotten is to have different varients of the classes, but as this is a sub class to a larger asset class I don't want to have to create variants all the way up the to top level.</p>
  <p>I've also looked at custom deserialisation, but I don't understand it well enough to resolve this.</p>
  <p>Also happy to change serialiser if someone thinks this can be solved with another library.</p>