title: Getting this exception "Newtonsoft.Json.JsonSerializationException", when trying to deserialize Json to object
body: <p>As part of my Xamarin Forms app, developed for Android, i have a Json config file wherein i save some settings, like whether or not the app is in debug mode or things like a User, used to log into a website.</p>
  
  <p>However trying to get this User object from the Json file throws the mentioned exception, which can also be seen in full below.</p>
  
  <pre><code>EXCEPTION   10-02-2020 14:06:08  Newtonsoft.Json.JsonSerializationException =&gt; Error converting value "{
    "$type": "Dental.App.Models.User, Dental.App",
    "username": "Ole",
    "password": "ole",
    "verifiedStatus": false,
    "creationTime": "10-02-2020 13:35:13"
  }" to type 'Dental.App.Models.User'. Path 'User', line 5, position 197.; Stacktrace =&gt;   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType (Newtonsoft.Json.JsonReader reader, System.Object value, System.Globalization.CultureInfo culture, Newtonsoft.Json.Serialization.JsonContract contract, System.Type targetType) [0x000bd] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x000d7] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.JsonConverter propertyConverter, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Object target) [0x00061] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) [0x00267] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00154] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x0006d] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000d9] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00053] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in &lt;f393073e86a643d9809b1a5d0c498495&gt;:0 
    at Wolf.Utility.Main.Transport.JsonManipulator.ReadValueViaModel[T,U] (System.String path, System.String propertyName) [0x000c8] in &lt;46eeb80ce9a440109e5bc07b0f1af244&gt;:0 
    at Dental.App.Config.get_User () [0x00013] in &lt;c94e9f8f60b14ec69bd9794bdf834717&gt;:0 
    at Dental.App.Views.DentalWebPage.DentalWebView_LoadFinished (System.Object sender, System.EventArgs e) [0x00035] in &lt;c94e9f8f60b14ec69bd9794bdf834717&gt;:0 
  </code></pre>
  
  <p>The User and ConfigModel objects that i'm trying to deserialize into is quite simple id say. They can be seen below, along with a section of my Config file that contains some of my properties stored in the Json, followed by my Json.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class User
      {
          [JsonProperty("username")]
          public string Username { get; set; }
          [JsonProperty("password")]
          public string Password { get; set; }
          [JsonProperty("verifiedStatus")]
          public bool VerifiedStatus { get; set; }
          [JsonProperty("creationTime")]
          public string CreationTime { get; private set; }
  
          [JsonIgnore]
          public DateTime TimeOfCreation =&gt; Convert.ToDateTime(CreationTime);
  
          public User()
          {
              CreationTime = DateTime.Now.ToString(CultureInfo.CurrentCulture);
          }
  
          [JsonConstructor]
          public User(string creationTime)
          {
              CreationTime = creationTime;
          }
      }
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>public class ConfigModel
      {
          public User User { get; set; }
      }
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>public class Config
      {
          private static User user = new User();
          public static User User
          {
              get =&gt; CanSave ? JsonManipulator.ReadValueViaModel&lt;User, ConfigModel&gt;(ConfigPath, nameof(User)) : user;
              set
              {
                  if (CanSave) JsonManipulator.WriteValue(ConfigPath, nameof(User), value);
                  else user = value;
              }
          }
  
          private static bool debugMode = true;
          public static bool DebugMode
          {
              get =&gt; CanSave ? JsonManipulator.ReadValue&lt;bool&gt;(ConfigPath, nameof(DebugMode)) : debugMode;
              set
              {
                  if (CanSave) JsonManipulator.WriteValue(ConfigPath, nameof(DebugMode), value);
                  else debugMode = value;
              }
          }       
  ...
  }
  
  </code></pre>
  
  <pre><code>{
    "DebugMode": "true",
    "UseCustomOptions": "false",
    "CustomFormats": "{\n  \"$type\": \"System.Collections.Generic.List`1[[ZXing.BarcodeFormat, zxing.portable]], mscorlib\",\n  \"$values\": []\n}",
    "User": "{\n  \"$type\": \"Dental.App.Models.User, Dental.App\",\n  \"username\": \"Ole\",\n  \"password\": \"ole\",\n  \"verifiedStatus\": false,\n  \"creationTime\": \"10-02-2020 13:35:13\"\n}",
    "SelectedMenu": "3"
  }
  </code></pre>
  
  <p>The part of my code that does the blunt of the work, for this process, comes from my utility library. This library is a submodule on my github and the full one can be found here: <a href="https://github.com/andr9528/Wolf.Utility.Main" rel="nofollow noreferrer">https://github.com/andr9528/Wolf.Utility.Main</a></p>
  
  <p>Beneath is the methods from my JsonManipulator class, which throws the exception mentioned when trying to Deserialize into the User object, i.e T is User. For the full class go to the link above.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class JsonManipulator
      {
          /// &lt;summary&gt;
          /// Parses Json file, and returns the attribute specified by 'propertyName'.
          /// &lt;/summary&gt;
          /// &lt;typeparam name="T"&gt;The type returned, from the property named after the input 'propertyName'.&lt;/typeparam&gt;
          /// &lt;param name="path"&gt;Path of the Json file.&lt;/param&gt;
          /// &lt;param name="propertyName"&gt;Name of the property to return.&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          public static T ReadValue&lt;T&gt;(string path, string propertyName)
          {
              if (!File.Exists(path))
                  throw new ArgumentNullException(nameof(path), $@"No file Exist on the specified path =&gt; {path}");
  
              if (path.Split('.').Last().ToLowerInvariant() != "json")
                  throw new ArgumentException("The path given did not end in 'json'");
  
              var json = File.ReadAllText(path);
  
              try
              {
                  var obj = JObject.Parse(json);
                  if (obj != null)
                      return obj[propertyName].ToObject&lt;T&gt;();
                  throw new OperationFailedException($"Failed to parse Json from the file located at =&gt; {path}");
  
              }
              catch (Exception ex)
              {
                  throw;
              }
          }
          /// &lt;summary&gt;
          /// Deserializes Json file into specified model, and returns the property from it by the value specified in 'propertyName'.
          /// &lt;/summary&gt;
          /// &lt;typeparam name="T"&gt;The type returned, from the property named after the input 'propertyName'.&lt;/typeparam&gt;
          /// &lt;typeparam name="U"&gt;The model that is deserialized into, and from which the property is taken and returned.&lt;/typeparam&gt;
          /// &lt;param name="path"&gt;Path of the Json file.&lt;/param&gt;
          /// &lt;param name="propertyName"&gt;Name of the property to return.&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          public static T ReadValueViaModel&lt;T, U&gt;(string path, string propertyName)
          {
              if (!File.Exists(path))
                  throw new ArgumentNullException(nameof(path), $@"No file Exist on the specified path =&gt; {path}");
  
              if (path.Split('.').Last().ToLowerInvariant() != "json")
                  throw new ArgumentException("The path given did not end in 'json'");
  
              var json = File.ReadAllText(path);
  
              try
              {
                  var obj = JsonConvert.DeserializeObject&lt;U&gt;(json, new JsonSerializerSettings() 
                  { 
                      NullValueHandling = NullValueHandling.Ignore, 
                      TypeNameHandling = TypeNameHandling.All
                  });
  
                  var prop = obj.GetType().GetProperties().First(x =&gt; x.Name == propertyName);
  
                  return (T)prop.GetValue(obj);
              }
              catch (Exception)
              {
  
                  throw;
              }
          }
  ...
  }
  </code></pre>
  
  <p>It is only the Get part of the User Property in my Config that has this issue, which is the one i need to call to get the User object containing the Username and Password.</p>
  
  <p>To try and solve the issue i have made a number of changes, which can all be seen in the above code. Some examples, if not all, are as follows.</p>
  
  <ol>
  <li><p>Added JsonProperty specifications to properties on the User object</p></li>
  <li><p>Added a JsonConstructor on the User object</p></li>
  <li><p>Changed the type of the property 'CreationTime' on the User object from DateTime to string.</p></li>
  <li><p>Created the method ReadValueViaModel in JsonManipulator together with the ConfigModel class </p></li>
  </ol>
  
  <p>I have that annoying felling that, what i'm missing is just a tiny piece of code or formatting somewhere, but i simply can't figure out where and what i'm missing for it to work.</p>
  
  <p>Fell free to ask me questions to clear up any missing information.</p>
  
  <p>EDIT 1: Updated Json formatting - Directly copied from the autogenerated json file. According to <a href="https://jsonlint.com/" rel="nofollow noreferrer">https://jsonlint.com/</a>, it is valid Json, so my WriteValue Method is creating valid json.</p>