codec: encode invalid values as nil.

Fixes #40