title: How can I use Json.net to populate a model with custom bindings/mappings?
body: <p>Here's my JSON:</p>
  
  <pre><code>{
      "macAddress": "000A959D6816",
      "softwareVersion": "1.2.1.5-UnnecessaryInfo",
      "lastUpdated": "2015-04-03 20:46:40.375 -0500",
      "charging": true
  }
  </code></pre>
  
  <p>And using Json.NET, I can do the following in C#:</p>
  
  <pre><code>namespace JsonTest
  {
      public class Tablet
      {
          public string MacAddress { get; set; }
          public string SoftwareVersion { get; set; }
          public DateTime LastUpdated { get; set; }
          public bool Charging { get; set; }
      }
  
      public class TestClass
      {
          public void Test()
          {
              var json = "{ ... }"; // filled in with JSON info from above
              var model = new Tablet();
              try
              {
                  JsonConvert.PopulateObject(json, model);
              }
              catch (JsonSerializationException ex)
              {
                  Console.WriteLine(ex);
              }
          }
      }
  }
  </code></pre>
  
  <p>So far, so good. The code I have here works great. It populates my model object with all the data from the Json. However, I don't really want the <code>SoftwareVersion</code> property of my model to be a string; I'd rather have it be an instance of the <a href="https://msdn.microsoft.com/en-us/library/system.version%28v=vs.110%29.aspx" rel="nofollow">System.Version</a> class. In other words, I'd like my <code>Tablet</code> class to look more like this:</p>
  
  <pre><code>public class Tablet
  {
      public string MacAddress { get; set; }
      public Version SoftwareVersion { get; set; }
      public DateTime LastUpdated { get; set; }
      public bool Charging { get; set; }
  }
  </code></pre>
  
  <p>I don't care about the unnecessary info that gets appended on the end of that version string, so I'd like to write some sort of mapper/binder class that examines the version string from the Json, strips off the unnecessary info, and then parses that field into a <code>Version</code> object before proceeding to populate my model. I <em>do</em> know how to write that individual parsing method; this would do the trick:</p>
  
  <pre><code>private static Version ParseVersion(object versionObj)
  {
      var pattern = new Regex(@"^[\d.]+");
      var versionString = versionObj.ToString();
      if (!pattern.IsMatch(versionString)) return null;
  
      var match = pattern.Match(versionString);
      versionString = match.Groups[0].ToString();
  
      Version version;
      Version.TryParse(versionString, out version);
      return version;
  }
  </code></pre>
  
  <p>What I don't know is where and how to "plug this in" during the JsonConvert process. I see that the <code>PopulateObject</code> takes an optional <a href="http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm" rel="nofollow">JsonSerializerSettings</a> parameter, and in turn that has several different object initializer parameters like <a href="http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_Binder.htm" rel="nofollow">Binder</a> and <a href="http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_Converters.htm" rel="nofollow">Converters</a>. But I'm not sure which one to use, nor how to write either of those classes to do what I'm describing here. How do I do it? And what is the difference between a Binder and a Converter?</p>