move generic serialization code into its own module