Factor out the serialization into a seperate method for easier overriding.