Just wondering if anyone knows of the best way to upgrade a JSON structure Deserialisation to a new class type.
To further explain the legacy value was
public string author;
this has now been updated in the api to the following
public class Author
{
public string name;
public string email;
public string url;
}
public Author author;
So now I have an issue where any legacy data does not deserialize into this correctly as it used to be a string and now its a class.
My current solution is if it fails to deserialize then to do it into a class that had the old structure and then use this to go into the new one, but i feel there must be a better way to cast the old sting value into the new class value as part of the process.
Thanks
EDIT-1:
Ok i have made some headway with a converter below
public class AuthorConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Author user = (Author)value;
writer.WriteValue(user);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Author author = new Author();
Debug.Log(objectType + " " + reader.Value);
if (reader.TokenType == JsonToken.String)
{
author.name = (string) reader.Value;
}
else if(reader.TokenType == JsonToken.StartObject)
{
try
{
JObject jObject = JObject.Load(reader);
if (jObject.TryGetValue("name", out JToken name))
{
author.name = name.Value<string>();
}
if (jObject.TryGetValue("email", out JToken email))
{
author.email = email.Value<string>();
}
if (jObject.TryGetValue("url", out JToken url))
{
author.url = url.Value<string>();
}
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
throw;
}
}
return author;
}
Appears to all be working, but feels a bit fiddly to have to get the values 1 by 1 and convert over, i tried using the jObject.ToObject method but appeared to cause an infinite loop. Either way its working, but im sure there is a better way, so still open for ideas.