title: Object of type 'Newtonsoft.Json.Linq.JObject' cannot be converted to type
body: <p>I am using JSON.NET to parse some data into some particular properties using reflection.</p>
  <p><code>property.SetValue(comInstance, field.Value);</code></p>
  <p>one this line it throws an <code>ArgumentException</code>:</p>
  <blockquote>
  <p>An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll</p>
  <p>Additional information: Object of type 'Newtonsoft.Json.Linq.JObject' cannot be converted to type 'Microsoft.Xna.Framework.Vector2'.</p>
  </blockquote>
  <p>I know this must have something to do with my <code>JsonConverter</code> for <code>Vector2</code> types, which looks like this:</p>
  <pre><code>public class Vector2Converter : JsonConverter
  {
      public override bool CanConvert(Type objectType)
      {
          return (objectType == typeof(Vector2));
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          JObject jsonObject = JObject.Load(reader);
          var properties = jsonObject.Properties().ToList();
          return new Vector2
          {
              X = (float)properties[0].Value,
              Y = (float)properties[1].Value
          };
      }
      
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          Vector2 v = (Vector2)value;
          writer.WriteStartObject();
          writer.WritePropertyName(&quot;X&quot;);
          serializer.Serialize(writer, v.X);
          writer.WritePropertyName(&quot;Y&quot;);
          serializer.Serialize(writer, v.Y);
          writer.WriteEndObject();
          
      }
  }
  </code></pre>
  <p>now, I noticed that write gets called, but read never does.
  I deserialize like this:</p>
  <pre><code>JsonConvert.DeserializeObject&lt;EntityJson&gt;(json, JsonUtility.Settings);
  </code></pre>
  <p>the <code>JsonUility.Settings</code> contain the <code>Vector2Converter</code>.</p>
  <p>and I serialize like this:</p>
  <pre><code>JsonConvert.SerializeObject(entityJson, Formatting.Indented, JsonUtility.Settings);
  </code></pre>
  <p>The field in the JSON itself looks good as well:</p>
  <pre><code>&quot;Center&quot;: {
        &quot;X&quot;: 116.0,
        &quot;Y&quot;: 65.0
      },
  </code></pre>
  <p>Can anyone help me out?</p>
  <p>Edit:</p>
  <pre><code>internal class EntityJson
  {
      public string Name { get; set; }
      public Dictionary&lt;string, ComponentJson&gt; Components { get; set; }
      
      public EntityJson()
      {
          Components = new Dictionary&lt;string, ComponentJson&gt;();
      }
  
      public Entity ToEntity()
      {
          Entity entity = new Entity(Name);
  
          foreach(KeyValuePair&lt;string, ComponentJson&gt; com in Components)
          {
              ObjectHandle handle = Activator.CreateInstance(com.Value.Namespace, com.Value.Namespace +&quot;.&quot;+ com.Key);
              Object handleValue = handle.Unwrap();
              Component comInstance = (Component)handleValue;
  
              foreach(KeyValuePair&lt;string, object&gt; field in com.Value.Fields)
              {
                  PropertyInfo property = comInstance.GetType().GetProperty(field.Key, 
                      BindingFlags.NonPublic | 
                      BindingFlags.Public | 
                      BindingFlags.Instance);
  
                  property.SetValue(comInstance, field.Value); // &lt;-- field.Value
              }
  
              entity.AddUnsafe(comInstance);
          }
  
          return entity;
      }
  }
  </code></pre>
  <p>the field.value will be a <code>JObject</code> instead of a <code>Vector2</code>, the exception states:</p>
  <blockquote>
  <p>it cannot be casted.</p>
  </blockquote>