nodes: 0, module; 1, function_definition; 2, function_name:serialize; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, comment:# generate a list; 12, expression_statement; 13, if_statement; 14, return_statement; 15, identifier:as_dict; 16, False; 17, identifier:sort; 18, None; 19, comment:"""
          Dump built files as a list or dictionary, for JSON or other serialization.
  
              sort: a key function to sort a list, or simply True
          """; 20, assignment; 21, identifier:as_dict; 22, block; 23, assignment; 24, call; 25, block; 26, elif_clause; 27, call; 28, identifier:files; 29, call; 30, return_statement; 31, identifier:data; 32, generator_expression; 33, identifier:callable; 34, argument_list; 35, return_statement; 36, comparison_operator:sort is True; 37, block; 38, identifier:list; 39, argument_list; 40, identifier:getattr; 41, argument_list; 42, call; 43, call; 44, for_in_clause; 45, identifier:sort; 46, call; 47, identifier:sort; 48, True; 49, return_statement; 50, identifier:data; 51, identifier:self; 52, string; 53, call; 54, identifier:dict; 55, generator_expression; 56, attribute; 57, argument_list; 58, identifier:p; 59, call; 60, identifier:sorted; 61, argument_list; 62, call; 63, string_content:files; 64, attribute; 65, argument_list; 66, tuple; 67, for_in_clause; 68, identifier:p; 69, identifier:to_dict; 70, attribute; 71, argument_list; 72, identifier:data; 73, keyword_argument; 74, identifier:sorted; 75, argument_list; 76, identifier:self; 77, identifier:run; 78, identifier:fn; 79, call; 80, pattern_list; 81, call; 82, identifier:files; 83, identifier:values; 84, identifier:key; 85, identifier:sort; 86, identifier:data; 87, attribute; 88, argument_list; 89, identifier:fn; 90, identifier:p; 91, attribute; 92, argument_list; 93, identifier:p; 94, identifier:to_dict; 95, identifier:files; 96, identifier:items
code: def serialize(self, as_dict=False, sort=None):
          """
          Dump built files as a list or dictionary, for JSON or other serialization.
  
              sort: a key function to sort a list, or simply True
          """
          files = getattr(self, 'files', self.run())
  
          if as_dict:
              return dict((fn, p.to_dict()) for fn, p in files.items())
  
          # generate a list
          data = (p.to_dict() for p in files.values())
  
          if callable(sort):
              return sorted(data, key=sort)
  
          elif sort is True:
              return sorted(data)
  
          return list(data)