Reputation: 51
I am using JSON.NET to parse some data into some particular properties using reflection.
property.SetValue(comInstance, field.Value);
one this line it throws an ArgumentException
:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Object of type 'Newtonsoft.Json.Linq.JObject' cannot be converted to type 'Microsoft.Xna.Framework.Vector2'.
I know this must have something to do with my JsonConverter
for Vector2
types, which looks like this:
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("X");
serializer.Serialize(writer, v.X);
writer.WritePropertyName("Y");
serializer.Serialize(writer, v.Y);
writer.WriteEndObject();
}
}
now, I noticed that write gets called, but read never does. I deserialize like this:
JsonConvert.DeserializeObject<EntityJson>(json, JsonUtility.Settings);
the JsonUility.Settings
contain the Vector2Converter
.
and I serialize like this:
JsonConvert.SerializeObject(entityJson, Formatting.Indented, JsonUtility.Settings);
The field in the JSON itself looks good as well:
"Center": {
"X": 116.0,
"Y": 65.0
},
Can anyone help me out?
Edit:
internal class EntityJson
{
public string Name { get; set; }
public Dictionary<string, ComponentJson> Components { get; set; }
public EntityJson()
{
Components = new Dictionary<string, ComponentJson>();
}
public Entity ToEntity()
{
Entity entity = new Entity(Name);
foreach(KeyValuePair<string, ComponentJson> com in Components)
{
ObjectHandle handle = Activator.CreateInstance(com.Value.Namespace, com.Value.Namespace +"."+ com.Key);
Object handleValue = handle.Unwrap();
Component comInstance = (Component)handleValue;
foreach(KeyValuePair<string, object> field in com.Value.Fields)
{
PropertyInfo property = comInstance.GetType().GetProperty(field.Key,
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance);
property.SetValue(comInstance, field.Value); // <-- field.Value
}
entity.AddUnsafe(comInstance);
}
return entity;
}
}
the field.value will be a JObject
instead of a Vector2
, the exception states:
it cannot be casted.
Upvotes: 3
Views: 12918
Reputation: 51
Ok, ive managed to resolve it doing this:
object result = field.Value;
if (field.Value.GetType() == typeof(JObject))
{
JToken token = (JToken)field.Value;
result = token.ToObject(property.PropertyType, JsonSerializer.Create(JsonUtility.Settings));
}
else
{
result = Convert.ChangeType(result, property.PropertyType);
}
property.SetValue(comInstance, result);
Is this a stable solution ?
Upvotes: 1