def json_safe(string, content_type='<STR_LIT>'):
try:<EOL><INDENT>string = string.decode('<STR_LIT:utf-8>')<EOL>json.dumps(string)<EOL>return string<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>return b'<STR_LIT>'.join([<EOL>b'<STR_LIT>',<EOL>content_type.encode('<STR_LIT:utf-8>'),<EOL>b'<STR_LIT>',<EOL>base64.b64encode(string)<EOL>]).decode('<STR_LIT:utf-8>')<EOL><DEDENT>
Returns JSON-safe version of `string`.

    If `string` is a Unicode string or a valid UTF-8, it is returned unmodified,
    as it can safely be encoded to JSON string.

    If `string` contains raw/binary data, it is Base64-encoded, formatted and
    returned according to "data" URL scheme (RFC2397). Since JSON is not
    suitable for binary data, some additional encoding was necessary; "data"
    URL scheme was chosen for its simplicity.