What you need is not directly available in the API, but I think it’s still possible.
You need to iterate over the inventory, it’s children and each itemstack. Store the data in some structure.
Then at a later date you rebuild the inventory from the structure.