import codecs


def serializer_with_encoder_constructor(serialization_func, encoder_type='utf-8', encoder_error_mode='strict'):
    """
    Wrap a serialization function with string encoding. This is important for JSON, as it serializes objects into
    strings (potentially unicode), NOT bytestreams. An extra encoding step is needed to get to a bytestream.

    :param serialization_func: The base serialization function.
    :param encoder_type: The encoder type. Default: 'utf-8'
    :param encoder_error_mode: The encode error mode. Default: 'strict'.
    :return: The serializer function wrapped with specified encoder.
    :rtype: T -> bytes | bytearray | str
    """
    encoder = codecs.getencoder(encoder_type)

    def serialize(payload):
        serialized, _ = encoder(serialization_func(payload), encoder_error_mode)
        return serialized
    return serialize