#4137 tidy serialization code with helper
#4137 fix deserialization