<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def serialize(ad_objects, output_format='json', indent=2, attributes_only=False):
 """Serialize the object to the specified format :param ad_objects list: A list of ADObjects to serialize :param output_format str: The output format, json or yaml. Defaults to json :param indent int: The number of spaces to indent, defaults to 2 :param attributes only: Only serialize the attributes found in the first record of the list of ADObjects :return: A serialized, formatted representation of the list of ADObjects :rtype: str """


    # If the request is to only show attributes for objects returned
    # in the query, overwrite ad_objects with only those attributes present in
    # the first object in the list
    if attributes_only:
        ad_objects = [key for key in sorted(ad_objects[0].keys())]

    if output_format == 'json':
        return json.dumps(ad_objects, indent=indent, ensure_ascii=False, sort_keys=True)
    elif output_format == 'yaml':
        return yaml.dump(sorted(ad_objects), indent=indent)