Skip to content

Request to add support for handling bytearray when outputting data in YAML format #291

@FumingZhang

Description

@FumingZhang

The current implementation of yaml formatter uses the safe_dump method, which does not support data of type bytearray, which leads to the following issue

File "/home/fumingzhang/aenv/lib/python3.12/site-packages/yaml/representer.py", line 58, in represent_data
node = self.yaml_representers[None](self, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/fumingzhang/aenv/lib/python3.12/site-packages/yaml/representer.py", line 231, in represent_undefined
raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', bytearray(b'-----BEGIN CERTIFICATE-----\nxxxx\n-----END CERTIFICATE-----'))

def format_yaml(obj):
    import yaml
    try:
        return yaml.safe_dump(obj.result, default_flow_style=False, allow_unicode=True)
    except yaml.representer.RepresenterError:
        # yaml.safe_dump fails when obj.result is an OrderedDict. knack's --query implementation converts the result to an OrderedDict. https://github.com/microsoft/knack/blob/af674bfea793ff42ae31a381a21478bae4b71d7f/knack/query.py#L46. # pylint: disable=line-too-long
        return yaml.safe_dump(json.loads(json.dumps(obj.result)), default_flow_style=False, allow_unicode=True)

https://github.com/microsoft/knack/blob/dev/knack/output.py#L46C1-L50C46

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions