code: def Cleanse(obj, encoding='utf-8'):
    if isinstance(obj, int):
      return obj
    elif isinstance(obj, float):
      if obj == _INFINITY:
        return 'Infinity'
      elif obj == _NEGATIVE_INFINITY:
        return '-Infinity'
      elif math.isnan(obj):
        return 'NaN'
      else:
        return obj
    elif isinstance(obj, bytes):
      return tf.compat.as_text(obj, encoding)
    elif isinstance(obj, (list, tuple)):
      return [Cleanse(i, encoding) for i in obj]
    elif isinstance(obj, set):
      return [Cleanse(i, encoding) for i in sorted(obj)]
    elif isinstance(obj, dict):
      return {Cleanse(k, encoding): Cleanse(v, encoding) for k, v in obj.items()}
    else:
      return obj