import codecs
import json


def dump(data, handle, **kwargs):
    """Serialize ``data`` as a JSON formatted stream to ``handle``.

    We use ``ensure_ascii=False`` to write unicode characters specifically as this improves the readability of the json
    and reduces the file size.
    """
    try:
        if 'b' in handle.mode:
            handle = codecs.getwriter('utf-8')(handle)
    except AttributeError:
        pass

    return json.dump(data, handle, ensure_ascii=False, **kwargs)