import json


def serialise_to_json(dictionary, encode=True):
    """Take a dictionary and JSON-encode it for HTTP submission

    :param dictionary: A dictionary to be serialised
    :param encode: Should the putput be ensured to be ASCII
    """

    # Just return the JSON representation, and output as raw if requested
    # The latin1 encoding is a hack that just allows a 8-bit-clean byte-wise
    # output path. Using UTF-8 here would make Unicode libraries barf when using
    # fuzzed data. The character set is communicated to the client in the
    # HTTP headers anyway, so this shouldn't have an effect on efficacy.
    return json.dumps(dictionary, ensure_ascii=encode, encoding="iso-8859-1")