Optimized serialize and unserialize.