Fix (de)serialization of aggregate types with functions returning by ref

This fixes a number of corner cases present in the serialization module, original report is issue #802