Serializing List<T> subclass fails Serializing List<T> subclass fails dart dart

Serializing List<T> subclass fails


This is a weakness in the current implementation. You can do it, but it's more work than I'd like. So the default set of rules includes ListRule, which will grab anything that "is List". Sometimes that's what you want, for example, you can't really serialize an ObservableList with the things that are observing it, so it will get saved as if it were a List and then restored the same way. However, in your case that's not what you want.

The only hook available for that right now is to have another rule that grabs your subclass and add it ahead of the default. So, e.g. subclass ListRule and add it first. It would be nice to have some sort of mechanism for the "most specific" rule, or at least to be able to indicate something as a fallback rule in case no other rule wanted it.

class SublistRule extends ListRule {  appliesTo(thing, writer) => thing is Sublist;  inflateEssential(List state, Reader r) => new Sublist();}main() {  test("SerializeEncapsulateTest", () {    Serialization ser = new Serialization.blank()      ..addRule(new SublistRule())      ..addDefaultRules();    EncapsulateTest tm = new EncapsulateTest();    ...  });}