Returns the character encoding for a JSON string.

def detect_encoding(value):
    """Returns the character encoding for a JSON string."""
    # https://tools.ietf.org/html/rfc4627#section-3
    if six.PY2:
        null_pattern = tuple(bool(ord(char)) for char in value[:4])
    else:
        null_pattern = tuple(bool(char) for char in value[:4])

    encodings = {
        # Zero is a null-byte, 1 is anything else.
        (0, 0, 0, 1): 'utf-32-be',
        (0, 1, 0, 1): 'utf-16-be',
        (1, 0, 0, 0): 'utf-32-le',
        (1, 0, 1, 0): 'utf-16-le',
    }

    return encodings.get(null_pattern, 'utf-8')