title: Newtonsoft.JSON cannot convert model with TypeConverter attribute
body: <p>I have an <strong>C# MVC</strong> application which stores data as JSON strings in an XML document and also in MySQL DB Tables. </p>
  
  <p>Recently I have received the requirement to <strong>store JSON strings in MySQL Database fields</strong>, to be converted into <strong>C# objects via Newtonsoft.Json</strong>, so I decided to implement a TypeConverter to convert JSON strings into custom C# Models.</p>
  
  <p>Unfortunately I cannot use the following command anywhere in my solution to deserialize my JSON strings when the <strong>TypeConverter attribute is added to my C# Model</strong>:</p>
  
  <pre><code>JsonConvert.DeserializeObject&lt;Foo&gt;(json);
  </code></pre>
  
  <p>Removing the attribute resolve the issue however this prevents me from converting MySQL DB fields into custom C# objects.</p>
  
  <p>Here is my <strong>C# Model</strong> with the TypeConverter Attribute added:</p>
  
  <pre><code>using System.ComponentModel;
  
  [TypeConverter(typeof(FooConverter))]
  public class Foo
  {
      public bool a { get; set; }
      public bool b { get; set; }
      public bool c { get; set; }
      public Foo(){}
  }
  </code></pre>
  
  <p>Here is my <strong>TypeConverter Class</strong>:</p>
  
  <pre><code>using Newtonsoft.Json;
  using System;
  using System.ComponentModel;
  
      public class FooConverter : TypeConverter
      {
          public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
          {
              if (sourceType == typeof(string))
              {
                  return true;
              }
              return base.CanConvertFrom(context, sourceType);
          }
          public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
          {
              if (value is string)
              {
                  string s = value.ToString().Replace("\\","");
                  Foo f = JsonConvert.DeserializeObject&lt;Foo&gt;(s);
                  return f;
              }
              return base.ConvertFrom(context, culture, value);
          }
      }
  }
  </code></pre>
  
  <p><strong>As soon as I add the attribute to the Foo Class I receive the following error:</strong></p>
  
  <p><em>Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Models.Foo' because the type requires a JSON string value to deserialize correctly.</em></p>
  
  <p><em>To fix this error either change the JSON to a JSON string value or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.</em></p>
  
  <p><strong>I am using the following string (which works perfectly without adding the TypeConverter Attribute):</strong></p>
  
  <pre><code>"{\"Foo\":{\"a\":true,\"b\":false,\"c\":false}}"
  </code></pre>
  
  <p>Not sure what's going on here, any ideas?</p>
  
  <p>Many Thanks!!!</p>
  
  <h1>UPDATE</h1>
  
  <p>I have have discovered that I also have issues with actions on <strong>MVC API Controllers</strong> that accept the <strong>Test Class with Foo as a property</strong> or on controllers that accept <strong>Foo as an object</strong> when the <strong>TypeConverter attribute</strong> is added to the Foo Class.</p>
  
  <p>Here is an example of a test controller which has issues:</p>
  
  <pre><code>public class TestController : ApiController
  {
      [AcceptVerbs("POST", "GET")]
      public void PostTestClass(TestClass t)
      {
          // Returns null when TypeConverter attribute is added to the Foo Class
          return t.Foo; 
      }
      AcceptVerbs("POST", "GET")]
      public void PostFooObj(Foo f)
      {
          // Returns null when TypeConverter attribute is added to the Foo Class
          return f;
      }
  }
  </code></pre>
  
  <p>The TypeConverter may be causing issues overriding the WebAPI model binding and returns null when either action above receives JSON via AJAX with the following structure:</p>
  
  <pre><code>// eg. PostTestClass(TestClass T)
  {'Foo': {'a': false,'b': true,'c': false}};
  
  // eg. PostFooObj(Foo f)
  {'a': false,'b': true,'c': false}
  </code></pre>
  
  <p>When the TypeConverter Attribute is added to the Foo Class, the following method on the FooConverter TypeConverter class is called as soon the route is found:</p>
  
  <pre><code>    public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
      {
          if (sourceType == typeof(string))
          {
              return true;
          }
          return base.CanConvertFrom(context, sourceType);
      }
  </code></pre>
  
  <p>The ConvertFrom method on the FooConverter TypeController is NOT called by the ApiController's action, which may be the cause of the issue.</p>
  
  <p>Again it's a similar situation, where the controllers actions will work fine without the TypeConverter Attribute.</p>
  
  <p>Any further help greatly appreciated!! </p>
  
  <p>Many thanks.</p>