nodes: 0, module; 1, function_definition; 2, function_name:Cleanse; 3, parameters; 4, block; 5, identifier:obj; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, identifier:encoding; 10, string; 11, comment:"""Makes Python object appropriate for JSON serialization.
  
    - Replaces instances of Infinity/-Infinity/NaN with strings.
    - Turns byte strings into unicode strings.
    - Turns sets into sorted lists.
    - Turns tuples into lists.
  
    Args:
      obj: Python data structure.
      encoding: Charset used to decode byte strings.
  
    Returns:
      Unicode JSON data structure.
    """; 12, call; 13, block; 14, elif_clause; 15, elif_clause; 16, elif_clause; 17, elif_clause; 18, elif_clause; 19, else_clause; 20, string_content:utf-8; 21, identifier:isinstance; 22, argument_list; 23, return_statement; 24, call; 25, block; 26, call; 27, block; 28, call; 29, block; 30, call; 31, block; 32, call; 33, block; 34, block; 35, identifier:obj; 36, identifier:int; 37, identifier:obj; 38, identifier:isinstance; 39, argument_list; 40, if_statement; 41, identifier:isinstance; 42, argument_list; 43, return_statement; 44, identifier:isinstance; 45, argument_list; 46, return_statement; 47, identifier:isinstance; 48, argument_list; 49, return_statement; 50, identifier:isinstance; 51, argument_list; 52, return_statement; 53, return_statement; 54, identifier:obj; 55, identifier:float; 56, comparison_operator:obj == _INFINITY; 57, block; 58, elif_clause; 59, elif_clause; 60, else_clause; 61, identifier:obj; 62, identifier:bytes; 63, call; 64, identifier:obj; 65, tuple; 66, list_comprehension; 67, identifier:obj; 68, identifier:set; 69, list_comprehension; 70, identifier:obj; 71, identifier:dict; 72, dictionary_comprehension; 73, identifier:obj; 74, identifier:obj; 75, identifier:_INFINITY; 76, return_statement; 77, comparison_operator:obj == _NEGATIVE_INFINITY; 78, block; 79, call; 80, block; 81, block; 82, attribute; 83, argument_list; 84, identifier:list; 85, identifier:tuple; 86, call; 87, for_in_clause; 88, call; 89, for_in_clause; 90, pair; 91, for_in_clause; 92, string; 93, identifier:obj; 94, identifier:_NEGATIVE_INFINITY; 95, return_statement; 96, attribute; 97, argument_list; 98, return_statement; 99, return_statement; 100, attribute; 101, identifier:as_text; 102, identifier:obj; 103, identifier:encoding; 104, identifier:Cleanse; 105, argument_list; 106, identifier:i; 107, identifier:obj; 108, identifier:Cleanse; 109, argument_list; 110, identifier:i; 111, call; 112, call; 113, call; 114, pattern_list; 115, call; 116, string_content:Infinity; 117, string; 118, identifier:math; 119, identifier:isnan; 120, identifier:obj; 121, string; 122, identifier:obj; 123, identifier:tf; 124, identifier:compat; 125, identifier:i; 126, identifier:encoding; 127, identifier:i; 128, identifier:encoding; 129, identifier:sorted; 130, argument_list; 131, identifier:Cleanse; 132, argument_list; 133, identifier:Cleanse; 134, argument_list; 135, identifier:k; 136, identifier:v; 137, attribute; 138, argument_list; 139, string_content:-Infinity; 140, string_content:NaN; 141, identifier:obj; 142, identifier:k; 143, identifier:encoding; 144, identifier:v; 145, identifier:encoding; 146, identifier:obj; 147, identifier:items
code: def Cleanse(obj, encoding='utf-8'):
    """Makes Python object appropriate for JSON serialization.
  
    - Replaces instances of Infinity/-Infinity/NaN with strings.
    - Turns byte strings into unicode strings.
    - Turns sets into sorted lists.
    - Turns tuples into lists.
  
    Args:
      obj: Python data structure.
      encoding: Charset used to decode byte strings.
  
    Returns:
      Unicode JSON data structure.
    """
    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