最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unity game engine - Best way to deserialize into updated data structure? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

If 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.

发布评论

评论列表(0)

  1. 暂无评论