title: "Invalid cast from 'DateTime' to 'Int64'. While Deserializing JSON String
body: <p>I am parsing a large data file with JSON elements in C#.  Deserialization fails for some strings with the error below.</p>
  
  <pre><code> Exception  = Invalid cast from 'DateTime' to 'Int64'.
   Message    = Error converting value 3/28/2172 12:00:00 AM to type 'System.Int64'.  Path 'ProductDetail.productId', line 1, position 4003."
  </code></pre>
  
  <p>The error doesn't occur for all strings, but seems to suggest the value is being interpreted as a DateTime value; although, it's defined as a <strong>long</strong> within the associated class. </p>
  
  <p>Can I force the value to be interpreted as a <strong>long</strong>?</p>
  
  <p>ProductDetail.productId is defined as a member of the following type:</p>
  
  <pre><code>   public class ProductDetail
      {
          public string accreditedInvestorInd { get; set; }
          public string accrueAcreteInd { get; set; }
          public DateTime issueDate { get; set; }
          // ... truncated for simplicity
  
          public long productId { get; set; }
  
         // ... truncated for simplicity
          public string wkndHolPayInd { get; set; }
          public string zeroCouponInd { get; set; }
      }
  </code></pre>
  
  <p>I tried unsuccessfully to use a converter attribute with the hope of forcing a different interpretation.  (Not sure if I'm using it correctly)...</p>
  
  <pre><code>    [JsonConverter(typeof(string))]
      public long productId { get; set; }
  </code></pre>
  
  <p>Also, I am using a custom DateTime format.</p>
  
  <p>Here is a subset of the large data file I am trying to deserialize that corresponds to <code>ProductDetail</code>:</p>
  
  <pre><code>{"accrueAcreteInd":"Y","callableInd":"Y","calledInd":"N","cpnCurrency":"USD","cpnPmtFreqCode":"S","cpnRate":"2.8","curAmtOutstanding":"7825000.0","currencyCode":"USD","cusip":"3130H0AX1","datedDate":"20160303","dayCountMonthCode":"30","dayCountYearCode":"360","daysToSettle":"1","dtcEligibleInd":"Y","eomPmtInd":"N","exchange":"TRACE","fatcaCode":"1","firstCpnPmtDate":"20160901","hqlaCode":"2A","identifier":"3130H0AX1","identifierType":"cusip","inDefaultInd":"N","infoSourceCode":"R21RB","intAccrued":".91777778","intTypeCode":"FIX","is144aInd":"N","issueCountry":"US","issueDate":"20160303","issuePrice":"100.0","issuerIndustryCode":"11","issuerName":"FARMER MAC","issuerTicker":"FAMCA","lastCpnPmtDate":"20250901","lastUpdateTime":"20160830","lastUpdateTimeExt":"00:06:58","longDesc":"FAMCA 2.8 03/01/2026","maturityDate":"20260301","minDenom":"1000.0","minParAmount":"1000.0","mktSectorDes":"Govt","mtnInd":"Y","nextCallDate":"20170301","nextCallPrice":"100.0","nextCpnPmtDate":"20170301","origIssueAmt":"7825000.0","prevCpnPmtDate":"20160901","primaryIssInd":"Y","privatePlacementInd":"N","productId":"21720328","productTypeCode":"FRMNT","putableInd":"N","pxCleanInd":"Y","redempVal":"100.0","regSInd":"N","restrictedInd":"N","secHoldInd":"N","securityType":"FAMCCL","series":"NOTZ","shortDesc":"FAMCA 2.8 03/26","sinkableInd":"N","traceEligibleInd":"N","truePutInd":"N","wkndHolPayInd":"N","zeroCouponInd":"N"}
  </code></pre>
  
  <p>And here is how I am trying to deserialize the subset, where <code>Line</code> is the JSON string above:</p>
  
  <pre><code>var dateFormatSettings = new JsonSerializerSettings { DateFormatString = "yyyyMMdd" };
  var detail = JsonConvert.DeserializeObject&lt;ProductDetail&gt;(Line, dateFormatSettings);
  </code></pre>
  
  <p>And here is the full exception for the simplified JSON:</p>
  
  <pre><code>Newtonsoft.Json.JsonSerializationException: Error converting value 3/28/2172 12:00:00 AM to type 'System.Int64'. Path 'productId', line 1, position 1117. ---&gt; System.InvalidCastException: Invalid cast from 'DateTime' to 'Int64'.
     at System.DateTime.System.IConvertible.ToInt64(IFormatProvider provider)
     at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
     --- End of inner exception stack trace ---
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
     at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
     at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
     at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
  </code></pre>