Serialize a object including it's function into a JSON.