Json serialization for non-native types e.g. datetime and Decimal


#1

Hi,

The anyjson implementation that Zato uses for serialization cannot cope with non-native types, e.g. datetime or Decimal.
I looked for a hook in Zato to add a custom serializer but did not find anything. So I guess custom serialization needs to be done in the Service.handle() code?

Regards, Jan

PS. I saw a pull request by @dsuch for anyjson, but afaic see this has not been merged.


TypeError datetime.date is not JSON serializable (using SQLAlchemy)
#2

You are right that there is no plugin-like way to insert your own serialization methods but if you are interested in contributing new datatypes to SimpleIO please have a look at how subclasses of ForceType work over here:

For instance, consult Bool and Int - they are the simplest ones.

Alternatively, please open a ticket in GitHub listing the new datatypes that you’d like to see added, you mentioned DateTime and Decimal, perhaps there is something else?


#3

Because I needed to do type conversion “deeper” down the object hierarchy of the output, I ended up
doing a recursive walk of the output in service.validate_output() and converting datetime and Decimal objects
to strings.