Implemented serialize.flatten using `apply_shape`.