we used to have some old code that looked like this:
[System.Serializable]
public class Coordinate
{
public float Latitude = 0;
public float Longitude = 0;
public Coordinate()
{
}
...
}
[System.Serializable]
public class Waypoint : Coordinate
{
...
}`
[System.Serializable]
public class FPoint : Waypoint
{
...
}
which was recently refactored as:
public interface IHaveGeoCoords
{
GeoCoords Coords { get; }
}
[System.Serializable]
public struct GeoCoords : IEquatable<GeoCoods>
{
[SerializeField] double m_Latitude;
[SerializeField] double m_Longitude;
..
}
[System.Serializable]
public class Waypoint : IHaveGeoCoords
{
[SerializeField] GeoCoords m_Coordinates;
public GeoCoords Coords
{
get { return m_Coordinates; }
set { m_Coordinates = value; }
}
...
}`
[System.Serializable]
public class FPoint : Waypoint
{
...
}
We have lots of textfiles containing lists of FPoints that were serialized when the old code was in place. We'd like to keep retro compatibility by being able to deserialize those into the updated FPoint type correctly. I'm pretty new to serialization in general but my question is: do I really have to keep some version of the old Waypoint type, call it FPoint_Old, read my data into that first, and then convert it to fit the updated data type? Isn't there a smarter way to do this?
Note: working with Unity and Newtonsoft Json package.
we used to have some old code that looked like this:
[System.Serializable]
public class Coordinate
{
public float Latitude = 0;
public float Longitude = 0;
public Coordinate()
{
}
...
}
[System.Serializable]
public class Waypoint : Coordinate
{
...
}`
[System.Serializable]
public class FPoint : Waypoint
{
...
}
which was recently refactored as:
public interface IHaveGeoCoords
{
GeoCoords Coords { get; }
}
[System.Serializable]
public struct GeoCoords : IEquatable<GeoCoods>
{
[SerializeField] double m_Latitude;
[SerializeField] double m_Longitude;
..
}
[System.Serializable]
public class Waypoint : IHaveGeoCoords
{
[SerializeField] GeoCoords m_Coordinates;
public GeoCoords Coords
{
get { return m_Coordinates; }
set { m_Coordinates = value; }
}
...
}`
[System.Serializable]
public class FPoint : Waypoint
{
...
}
We have lots of textfiles containing lists of FPoints that were serialized when the old code was in place. We'd like to keep retro compatibility by being able to deserialize those into the updated FPoint type correctly. I'm pretty new to serialization in general but my question is: do I really have to keep some version of the old Waypoint type, call it FPoint_Old, read my data into that first, and then convert it to fit the updated data type? Isn't there a smarter way to do this?
Note: working with Unity and Newtonsoft Json package.
Share Improve this question edited Mar 8 at 9:33 AlexG asked Mar 6 at 22:56 AlexGAlexG 112 bronze badges1 Answer
Reset to default 0If the Waypoint JSON files are not going to be changed, nothing is stopping you from writing an Editor script that reads them, deserializes them as your old Waypoint class, converts them to your new Waypoint class, and then overwrites the textfies with the new structure.
Something like this (untested):
foreach (string file in files)
{
string json = File.ReadAllText(file);
Waypoint_old oldWaypoint = JsonUtility.FromJson<Waypoint_old>(json);
Waypoint newWaypoint = new Waypoint
{
Coords = new GeoCoords(oldWaypoint.Latitude, oldWaypoint.Longitude)
};
// Save the converted JSON
string newJson = JsonUtility.ToJson(newWaypoint, true);
File.WriteAllText(file, newJson);
}
Alternatively, if you know you will always have Waypoint JSON files with mixed formats, you could write a generic custom JsonConverter. The issue here is that you'll need to figure out a way to distinguish wich file is encoded in which format.