Safely transform any object into utf8 encoded bytes
492568
def safe_unicode(string):
    ''''''
    if not isinstance(string, basestring):
        string = unicode(string)
    if isinstance(string, unicode):
        string = string.encode('utf8')
    return string
30110
def to_utf8(obj):
    
    if isinstance(obj, unicode_type):
        return _utf8(obj)
    elif isinstance(obj, dict):
        return dict((to_utf8(k), to_utf8(v)) for (k, v) in obj.items())
    elif isinstance(obj, list):
        return list(to_utf8(i) for i in obj)
    elif isinstance(obj, tuple):
        return tuple(to_utf8(i) for i in obj)

    return obj
337622
def encode_unicode(f):
    
    @wraps(f)
    def wrapped(obj, error):

        def _encode(value):
            """Helper encoding unicode strings into binary utf-8"""
            if isinstance(value, unicode):  # noqa: F821
                return value.encode('utf-8')
            return value

        error = copy(error)
        error.document_path = _encode(error.document_path)
        error.schema_path = _encode(error.schema_path)
        error.constraint = _encode(error.constraint)
        error.value = _encode(error.value)
        error.info = _encode(error.info)
        return f(obj, error)

    return wrapped if PYTHON_VERSION < 3 else f
1088980
def to_unicode(obj, encoding='utf-8'):
    
    if isinstance(obj, string_types) or isinstance(obj, binary_type):
        if not isinstance(obj, text_type):
            obj = text_type(obj, encoding)
    return obj
1073721
def translate_bytes(val):
    '''
    
    '''
    try:
        val = int(val)
    except (TypeError, ValueError):
        if not isinstance(val, six.string_types):
            val = six.text_type(val)
    return val
529105
function utf8Encode(str) {
  var bytes = [], offset = 0, length, char;

  str = encodeURI(str);
  length = str.length;

  while (offset < length) {
    char = str.charAt(offset);
    offset += 1;

    if (char !== '%') {
      bytes.push(char.charCodeAt(0));
    } else {
      char = str.charAt(offset) + str.charAt(offset + 1);
      bytes.push(parseInt(char, 16));
      offset += 2;
    }
  }

  return bytes;
}
408533
def _unicode_to_ascii(obj):  # pragma: no cover
    """"""
    # pylint: disable=E0602,R1717
    if isinstance(obj, dict):
        return dict(
            [
                (_unicode_to_ascii(key), _unicode_to_ascii(value))
                for key, value in obj.items()
            ]
        )
    if isinstance(obj, list):
        return [_unicode_to_ascii(element) for element in obj]
    if isinstance(obj, unicode):
        return obj.encode("utf-8")
    return obj
1063190
def get_unicode_str(obj):
    """"""
    if isinstance(obj, six.text_type):
        return obj
    if isinstance(obj, six.binary_type):
        return obj.decode("utf-8", errors="ignore")
    return six.text_type(obj)
1103707
def _utf8(value):
    
    if isinstance(value, _UTF8_TYPES):
        return value
    if not isinstance(value, unicode_type):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value)
        )
    return value.encode("utf-8")
930355
def ensure_str(data: Union[str, bytes]) -> str:
    
    if isinstance(data, bytes):
        return str(data, 'utf-8')

    return data
1306488
def _decode(self, obj, context):
        
        return b''.join(map(int2byte, [c + 0x60 for c in bytearray(obj)])).decode("utf8")
276362
def to_utf8(self, data):
        
        # if this is a unicode string, return its string representation
        if isinstance(data, unicode):
            return data.encode('utf-8')

        # if this is a list of values, return list of string values
        if isinstance(data, list):
            return [self.to_utf8(item) for item in data]

        # if this is a dictionary, return dictionary of string keys and values
        if isinstance(data, dict):
            return {
                self.to_utf8(key): self.to_utf8(value)
                for key, value in data.iteritems()
            }

        # if it's anything else, return it in its original form
        return data
188615
def to_bytes(s, encoding="utf-8"):
    """"""
    if isinstance(s, six.binary_type):
        return s
    if six.PY3:
        return bytes(s, encoding)
    return s.encode(encoding)
265382
def to_utf8(value):
  
  if isinstance(value, unicode):
    return value.encode('utf-8')

  assert isinstance(value, str)
  return value
701204
function unicodeStringToTypedArray(s) {
    var escstr = encodeURIComponent(s);
    var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    });
    var ua = new Uint8Array(binstr.length);
    Array.prototype.forEach.call(binstr, function (ch, i) {
        ua[i] = ch.charCodeAt(0);
    });
    return ua;
}
557783
def obj_to_string(obj):
    ''''''
    if not obj:
        return None
    elif isinstance(obj, bytes):
        return obj.decode('utf-8')
    elif isinstance(obj, basestring):
        return obj
    elif is_lazy_string(obj):
        return obj.value
    elif hasattr(obj, '__html__'):
        return obj.__html__()
    else:
        return str(obj)
466026
def ensure_bytes(data: Union[str, bytes]) -> bytes:
    
    if isinstance(data, str):
        return bytes(data, 'utf-8')

    return data