Better serialize function