Refactored serialization: removed switch and divided logic to separate classes