# Custom JSON encoder for python

The json module only can encode str-like data.
To encode other datatypes, just write a custom encoder class:

class UUIDEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, uuid.UUID):
            # if the obj is uuid, we simply return the value of uuid
            return obj.hex
        elif isinstance(obj, datetime.date):
            return obj.strftime("%d.%m.%Y")
        return json.JSONEncoder.default(self, obj)

Use it with

json.dumps(obj, cls=UUIDEncoder)