title: Can I parse json either into a string or another concrete type as object?
body: <p>I'd like to have property of type <code>object</code> that can be either a <code>string</code> or <code>Template</code> type.</p>
  
  <p>Is it possible to tell Json.NET to parse something into one of several specified types?</p>
  
  <pre><code>class MyClass
  {
      public object Template { get; set; }
  }
  </code></pre>
  
  <p>where <code>Template = "TemplateName"</code></p>
  
  
  
  <pre><code>{
      "Template": "TemplateName"
  }
  </code></pre>
  
  <p>or <code>Template = new Template()</code></p>
  
  
  
  <pre><code>{
      "Template": { "Name": "OtherTamplate", ... }
  }
  </code></pre>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>I tried to follow @krillgar' advice and create a custom <code>JsonConverter</code> but unfortunatelly the <code>CanConvert</code> method receives only the target type, in this case <code>object</code>. This information is not enough to tell wheter it can be deserialized (in case I had other <code>object</code> properties). I guess I need it to be a <code>Template</code> after all or create a derived type like <code>TemplateReference</code> or something:</p>
  
  <pre><code>class myconverter : JsonConverter
  {
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  
      public override bool CanConvert(Type objectType)
      {
          // objectType = typeof(object)
          throw new NotImplementedException();
      }
  }
  
  Configuration = JsonConvert.DeserializeObject&lt;MyClass&gt;(text, new myconverter());
  </code></pre>
  
  <hr>
  
  <h2>Disclaimer</h2>
  
  <p>This question has once been closed as a duplicate of <a href="https://stackoverflow.com/questions/20432166/how-to-deserialize-a-json-property-that-can-be-two-different-data-types-using-js">How to deserialize a JSON property that can be two different data types using Json.NET</a>. Because at the time of writing my question I hadn't known that there already was a similar one I'd like to clarify the difference between them to prevent it from being closed in future:</p>
  
  <p>The other question is about how to deserialize different values into a <em>concrete</em> type whereas mine is about deserializing different values into an <em>object</em>. It might seem to be the same at the first look because in both examples only the type of the property is different but it has a huge impact on the overall application design. It's important for me that I can use an <em>object</em> to store different specialized types rather then one type having multiple responsibilities.</p>