Skip to content

JSON Serializer that handles common non-json types (i.e. Date, DateTime) #9

@ftobia

Description

@ftobia

Something like in https://stackoverflow.com/q/11875770/1812727

class DateTimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, (datetime.date, datetime.datetime)):
            return obj.isoformat()

        return json.JSONEncoder.default(self, obj)

def json_dumps(obj):
    return json.dumps(obj, cls=DateTimeEncoder)

But like, there are probably more things we could do, or more edge cases to consider. Basically sane default json encoding deserves the pockets treatment.

I'll also accept a link to a simple library that already does this sort of thing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions