How to set up serialization for a custom collection type?

Discussion and help for Easy Save 3
Post Reply
airoll
Posts: 3
Joined: Sun Apr 21, 2024 4:21 am

How to set up serialization for a custom collection type?

Post by airoll »

Hi I am using a custom package that defines its own collection called ObservableList<T>. The code for this class is as follows:

Code: Select all

public class ObservableList<T> : IList, IList<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
        ...
        private List<T> list;
        ...
}
My goal is that if I have an object like the following, that I am able to properly serialize and deserialize these ObservableList<T> such that when I save MyObject, it is able to save the list.

Code: Select all

public class MyObject
{
        ...
        private ObservableList<int> list;
        ...
}
When I try to use the Window > Easy Save 3 > Types, ObservableList<T> does not appear. How can I go about supporting ObservableList<T> so that it can be saved and loaded properly?
User avatar
Joel
Moodkie Staff
Posts: 4851
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to set up serialization for a custom collection type?

Post by Joel »

Hi there,

To save your custom List you would need to make a concrete type for the List you are saving and use this. For example the concrete type for ObservableList<int> would be:

Code: Select all

public class ObservableListInt : ObservableList<int>
{
}
In your case you would also need to make the inner list serializable. I.e.

Code: Select all

public class ObservableList<T> : IList, IList<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
        ...
        [ES3Serializable]
        private List<T> list;
        ...
}
Your ObservableListInt can then be saved and loaded without the need for an ES3Type.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
airoll
Posts: 3
Joined: Sun Apr 21, 2024 4:21 am

Re: How to set up serialization for a custom collection type?

Post by airoll »

Hmm, is there an alternative approach or approaches? If I do was you suggested, then I need to rename every reference I have to ObservableList<T> to ObservableListT in my codebase. Can I create a ES3Type for ObservableList<T>?
airoll
Posts: 3
Joined: Sun Apr 21, 2024 4:21 am

Re: How to set up serialization for a custom collection type?

Post by airoll »

Okay I think I was able to figure this out. I created a ES3ObservableListType and added the relevant logic to ES3TypeMgr.
User avatar
Joel
Moodkie Staff
Posts: 4851
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to set up serialization for a custom collection type?

Post by Joel »

Hi there,

Just a note, we wouldn't be able to provide support for modified source code and updating Easy Save in the future will revert your changes.

An alternative is to convert your list to a List<T> before saving, and convert it back after saving, though this might not be any simpler in your situation. Also note there's a feature request for generic classes which you can vote on here: https://moodkie.com/forum/viewtopic.php?t=1397

We don't currently support generic ES3Types because it would require structural changes which would impact performance for most users, so we would need a large number of users to require this to warrant doing so.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply