Page 1 of 1

How to set up serialization for a custom collection type?

Posted: Sun Apr 21, 2024 4:33 am
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?

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

Posted: Sun Apr 21, 2024 7:09 am
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

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

Posted: Sun Apr 21, 2024 7:40 am
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>?

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

Posted: Sun Apr 21, 2024 11:34 pm
by airoll
Okay I think I was able to figure this out. I created a ES3ObservableListType and added the relevant logic to ES3TypeMgr.

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

Posted: Mon Apr 22, 2024 7:14 am
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