title: How do I deserialise different NodaTime LocalDate patterns within the same JSON object
body: <p>I'm trying to use NodaTime to interpret dates retrieved from a third-party API. The dates come in an annoying array of formats in the same response, one that I'm particularly having trouble with is similar to:</p>
  
  <pre><code>{
      "ShortDate": "2017-01-01",
      "LongDate": "01 January 2017"
  }
  </code></pre>
  
  <p>I can correctly deserialise one format or the other using NodaPatternConverter, but not both.</p>
  
  <p>A simple example showing the problem is like this:</p>
  
  <pre><code>using Newtonsoft.Json;
  using NodaTime;
  using NodaTime.Serialization.JsonNet;
  using NodaTime.Text;
  
  namespace NodaLocalDateConverterTest
  {
      class ExampleDatedModel
      {
          public LocalDate ShortDate { get; set; }
  
          public LocalDate LongDate { get; set; }
      }
  
  
      class Program
      {
          static void Main(string[] args)
          {
              var exampleJsonString =
  @"{
      ""ShortDate"": ""2017-01-01"",
      ""LongDate"": ""01 January 2017""
  }";
  
              var serialisationSettings = new JsonSerializerSettings();
              //NodaTime default converter supports ShortDate format
              serialisationSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
  
              //Exception on LongDate property
              var deserialisedExample1 = JsonConvert.DeserializeObject&lt;ExampleDatedModel&gt;(exampleJsonString, serialisationSettings);
  
              serialisationSettings.Converters.Remove(NodaConverters.LocalDateConverter);
              serialisationSettings.Converters.Add(new NodaPatternConverter&lt;LocalDate&gt;(LocalDatePattern.CreateWithInvariantCulture("dd MMMM yyyy")));
  
              //Exception on ShortDate property
              var deserialisedExample2 = JsonConvert.DeserializeObject&lt;ExampleDatedModel&gt;(exampleJsonString, serialisationSettings);
          }
      }
  }
  </code></pre>
  
  <p>Using the default serialiser gives throws an exception on the LongDate property:</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'NodaTime.Text.UnparsableValueException' occurred in
    Newtonsoft.Json.dll</p>
    
    <p>Additional information: The value string does not match the required
    number from the format string "yyyy". Value being parsed: '^01 January
    2017'. (^ indicates error position.)</p>
  </blockquote>
  
  <p>Replacing with a custom pattern converter throws an exception on the ShortDate property:</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'NodaTime.Text.UnparsableValueException' occurred in
    Newtonsoft.Json.dll</p>
    
    <p>Additional information: The value string does not match a simple
    character in the format string " ". Value being parsed: '20^17-01-01'.
    (^ indicates error position.)</p>
  </blockquote>
  
  <p>In principle I think I could use two different converters for the two properties, such as</p>
  
  <pre><code>class ExampleDatedModel
  {
      [JsonConverter(typeof(ShortDateConverter)]
      public LocalDate ShortDate { get; set; }
  
      [JsonConverter(typeof(LongDateConverter)]
      public LocalDate LongDate { get; set; }
  }
  </code></pre>
  
  <p>However I don't see how to use NodaTime's NodaPatternConverter with the attribute as you can't instantiate the converter with a pattern.</p>
  
  <p>The <a href="http://nodatime.org/1.3.x/userguide/serialization" rel="nofollow noreferrer">documentation</a> helpfully says "Custom converters can be created easily from patterns using NodaPatternConverter." but doesn't give any examples!</p>
  
  <p>Possible solutions I've though about are</p>
  
  <ul>
  <li>Create a pair of converters derived from NodaPatternConverter which are configured for the two patterns.
  
  <ul>
  <li>NodaPatternConverter is sealed, so can't be inherited from.</li>
  </ul></li>
  <li>Create a pair of converters derived from JsonConverter to deal with both patterns
  
  <ul>
  <li>I guess these would call two versions of LocalDatePattern.Parse with the different patterns internally.</li>
  <li>Reimplementation of the whole of JsonConverter seems overkill.</li>
  </ul></li>
  <li>Deserialise the dates as strings then convert the dates afterwards.
  
  <ul>
  <li>Needs implementing for each class which has a mixture of types.</li>
  <li>Makes generic methods fetching resources from the API more complex.</li>
  </ul></li>
  </ul>
  
  <p>But I hope I'm just missing a way of marking up the resource classes to use existing converters.</p>