title: How to inject something into JsonConverter implementation (MVC Core, .NET 4.6.1)
body: <p><strong>Note</strong>
  I don't believe this is a duplicate of <a href="https://stackoverflow.com/questions/23017716/json-net-how-to-deserialize-without-using-the-default-constructor">this question</a>. in which the objective is to deserialise a model using a non-default constructor, whereas I need my custom deserialiser, not the model, to instantiate using its non-default constructor, injecting some dependency set up in the API statup. I tried the <code>JsonConstructor</code> anyway, but it still produced the same error.</p>
  
  <blockquote>
    <p>Newtonsoft.Json.JsonException: No parameterless constructor defined for 'Domain.UserProjector'.</p>
  </blockquote>
  
  <hr>
  
  <p>I'm working on an API that should allow the consumer to dictate resource projections based on some query string parameters. I though it'd be easiest handling it during JSON serialisation.</p>
  
  <p><strong>The model</strong></p>
  
  <p>I decorate my model with the custom serialiser.</p>
  
  <pre><code>[JsonConverter(typeof(CustomProjector))]
  public class SomeDTO { /* members */ }
  </code></pre>
  
  <p><strong>The converter</strong></p>
  
  <p>I implement my custom serialiser, injecting the required HttpContext.</p>
  
  <pre><code>public class CustomProjector : JsonConverter
  {
      public CustomProjector(IHttpContextAccessor httpContext)
      {
          /* IHttpContextAccessor is already set up in the dependency container. Then get the requirements from the query string. */
      }
  
      /* JsonConverter implementation */
  }
  </code></pre>
  
  <p><strong>The API method</strong></p>
  
  <p>And all I have to do is return the result.</p>
  
  <pre><code>public IActionResult GetSomething(int id)
  {
      SomeDTO something = domain.GetSomething(id);
  
      return this.Ok(somthing);
  }
  </code></pre>
  
  <p>Unfortunately this doesn't work because <code>JsonConverter</code> implementations need default constructors, and an error is thrown due to the paramaterised constructor in <code>CustomProjector</code>, producing an error:</p>
  
  <blockquote>
    <p>Newtonsoft.Json.JsonException: No parameterless constructor defined for 'Domain.UserProjector'.</p>
  </blockquote>
  
  <hr>
  
  <p>A possible solution is to simply provide <code>CustomProjector</code> with the query string and do the convertion manually.</p>
  
  <p><strong>The model</strong></p>
  
  <p>No decoration required.</p>
  
  <pre><code>public class SomeDTO { /* members */ }
  </code></pre>
  
  <p><strong>The converter</strong></p>
  
  <p>HttpContext is replaced by the query string, since I'm going to instantiate it manually.</p>
  
  <pre><code>public class CustomProjector : JsonConverter
  {
      public CustomProjector(string queryString)
      {
          /* Get requirements from query string. */
      }
  
      /* JsonConverter implementation */
  }
  </code></pre>
  
  <p><strong>The API method</strong></p>
  
  <p>Instantiate the converter, and use it convert, before returning the result.</p>
  
  <pre><code>public IActionResult GetSomething(int id)
  {
      SomeDTO something = domain.GetSomething(id);
  
      CustomProjector projector = new CustomProjector(this.Request.QueryString.Value);
      string somethingSerialised = JsonConvert.SerializeObject(something, projector);
  
      return this.Ok(somethingSerialised);
  }
  </code></pre>
  
  <p><em>or</em></p>
  
  <p>Pass the individual query string parameters to the constructor, but this is essentially the same as the option above.</p>
  
  <p><strong>The model</strong></p>
  
  <pre><code>public class CustomProjector : JsonConverter
  {
      public CustomProjector(string reqParam1, string reqParam2)
      {
          /* Use requirements provided. */
      }
  
      /* JsonConverter implementation */
  }
  </code></pre>
  
  <p><strong>The API method</strong></p>
  
  <pre><code>public IActionResult GetSomething(int id, string reqParam1, string reqParam2)
  {
      SomeDTO something = domain.GetSomething(id);
  
      CustomProjector projector = new CustomProjector(reqParam1, reqParam2);
      string somethingSerialised = JsonConvert.SerializeObject(something, projector);
  
      return this.Ok(somethingSerialised);
  }
  </code></pre>
  
  <p>I would like to avoid these solutions as it requires API methods to include the parameters and requires that each method converts the results manually, or the returned data will be incorrect.</p>
  
  <hr>
  
  <p>Is there something I can change to make it work the way I want? Perhaps something to do with DI? Perhaps a different serialiser?</p>
  
  <p>Much appreciated!</p>