xero-python icon indicating copy to clipboard operation
xero-python copied to clipboard

to_dict generates non serializable object because of enums

Open farfanoide opened this issue 2 years ago • 2 comments

using Xero-python 1.8 on python 3.10.4 trying to convert a Journal to dictionary using to_dict method.

I expect to have a json serializable dict of primitive values however I get an AccountType enum which is not serializable. simple enough to have a custom encoder that handles enums but it would also be rather simple to extend the serializer process here to enable proper conversion.

adding following code to models.py fixes the issue:

from enum import Enum
@serialize_to_dict.register(Enum)
def serialize_enum_to_dict(value):
    return value.value

please let me know if a pr is welcome for this or if further information is required.

farfanoide avatar Jun 26 '22 17:06 farfanoide

bumping this issue as I'm having the same problem.

mseflek avatar Oct 03 '22 22:10 mseflek

Enum issue was solved for me by upgrading to xero-python==1.16.0 however note that this introduced different errors for UUID and None


from xero_python.api_client.serializer import serialize_model

@serialize_model.register(UUID)
def serialize_uuid_model(model):
    """Serializes api model into an json serializable object.

    :param model: BaseModel instance to serialize
    :return: serialized object
    """
    return model.hex

farfanoide avatar Oct 04 '22 13:10 farfanoide