Returning UUID types from services using JSON

Hi everyone!

When implementing services using SimpleIO that return data returning the result of a query through SQLAlchemy in which there is at least one field that is a UUID, if you want to return a JSON you get into trouble because it’s not a type that is JSON-serializable:

TypeError: UUID('fea602c9-bc4d-432a-ad57-74d88416c04b') is not JSON serializable

I have tried using the Unicode() type conversion but I just get a different error:

AttributeError: 'UUID' object has no attribute 'decode'

Maybe adding a UUID type converter that returns obj.hex or casts it to str? Example from Stack Overflow here.

Can you open a ticket in GH for it? Once I complete the SimpleIO rewrite, there will be documentation on how to add custom elements.

If you are fine with checking out the internals then you can give it a try today, you just need to subclass zato.server.service.ForceType and implement method to_json - perhaps looking at how Int, Bool or List do it will be the gentlest guide.

I’ve opened the ticket with number #897.

I am having very little time these last weeks to keep trying and testing my Zato prototype, but I’ll see if I can find the moment and give it a try, as I’ll be glad to help.

@dsuch, any chances this UUID() type will be present in the new 3.1 version?

I have just set the ticket’s milestone to 3.1 on GitHub to ensure that it will included - thanks again.

1 Like