This kind of logic was previously scattered around in different serializers. Could be moved into the encoder.