refactor serializer in scala