Be careful not to confuse metadata and datum, as they are different things. Metadata are part of a transaction, while a datum is part of an UtxO.
Metadata are completely optional and to the discretion of the transaction issuer, while the datum is mandatory when dealing with script addresses.
Some metadata can optionally be attached to a transaction independently of your decision to add some datum to the different UtxO. In addition, you don't send the datum directly, but its hash.
It is only when you want an UtxO back from a script that you have to provide a datum whose hash has to match with the one you defined previously.
This datum will then be part of the script context, as long a additional data you can provide through redeemers.
So, you can indeed record the datum intended for the script in the transaction metadata, but it not mandatory. It is just a mechanism to recover the correct information in case it is not possible otherwise.
Now, for the "how to do that ?", the best is to study some open source code, like the one for the SpaceBudz market. It heavily uses the serialization lib to provide the different information required by this wallet.