          elif isinstance(obj, QuerySet):
              return tuple(obj)
          elif isinstance(obj, bytes):
              # Best-effort for binary blobs. See #4187.
              return obj.decode('utf-8')
          elif hasattr(obj, 'tolist'):
              # Numpy arrays and array scalars.
              return obj.tolist()
          elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
              raise RuntimeError(
 
</s> remove         # On python 2.x json.dumps() returns bytestrings if ensure_ascii=True,
        # but if ensure_ascii=False, the return type is underspecified,
        # and may (or may not) be unicode.
        # On python 3.x json.dumps() returns unicode strings.
        if isinstance(ret, str):
            # We always fully escape \u2028 and \u2029 to ensure we output JSON
            # that is a strict javascript subset. If bytes were returned
            # by json.dumps() then we don't have these characters in any case.
            # See: http://timelessrepo.com/json-isnt-a-javascript-subset
            ret = ret.replace('\u2028', '\\u2028').replace('\u2029', '\\u2029')
            return bytes(ret.encode('utf-8'))
        return ret
 </s> add         # We always fully escape \u2028 and \u2029 to ensure we output JSON
        # that is a strict javascript subset.
        # See: http://timelessrepo.com/json-isnt-a-javascript-subset
        ret = ret.replace('\u2028', '\\u2028').replace('\u2029', '\\u2029')
        return ret.encode() </s> remove             # On python 2.x the return type for json.dumps() is underspecified.
            # On python 3.x json.dumps() returns unicode strings.
            if isinstance(value, str):
                value = bytes(value.encode('utf-8'))
 </s> add             value = value.encode() </s> remove                 content = content.decode('utf-8')
 </s> add                 content = content.decode() </s> remove         return json.dumps(structure, indent=4).encode('utf-8')
 </s> add         return json.dumps(structure, indent=4).encode() </s> remove         return yaml.dump(structure, default_flow_style=False).encode('utf-8')
 </s> add         return yaml.dump(structure, default_flow_style=False).encode()
            return obj.decode()
          elif isinstance(obj, QuerySet):
              return tuple(obj)
          elif isinstance(obj, bytes):
              # Best-effort for binary blobs. See #4187.
            return obj.decode()
          elif hasattr(obj, 'tolist'):
              # Numpy arrays and array scalars.
              return obj.tolist()
          elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
              raise RuntimeError(
keep keep keep keep replace keep keep keep keep keep
 Drop default 'utf-8' to .encode()/.decode() (#6633)

A Python 3 cleanup that allows for less noise in the code.

https://docs.python.org/3/library/stdtypes.html#bytes.decode
https://docs.python.org/3/library/stdtypes.html#str.encode 
rest_framework/utils/encoders.py