This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author barry
Recipients barry, eddygeek, eli.bendersky, ethan.furman, ezio.melotti, pitrou, rhettinger, serhiy.storchaka
Date 2014-08-30.20:26:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <20140830132603.4f3e8db0@anarchist>
In-reply-to <>
On Aug 30, 2014, at 07:34 PM, Ethan Furman wrote:

>In other words, this was a bug that no one noticed for many many releases,
>and I'm not sure we should fix it in 2.7 now.
>Arguments for fixing?

-1 on fixing it, but we *can* document workarounds.  Here's what I use in
Mailman 3.

class ExtendedEncoder(json.JSONEncoder):
    """An extended JSON encoder which knows about other data types."""

    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        elif isinstance(obj, timedelta):
            # as_timedelta() does not recognize microseconds, so convert these
            # to floating seconds, but only if there are any seconds.
            if obj.seconds > 0 or obj.microseconds > 0:
                seconds = obj.seconds + obj.microseconds / 1000000.0
                return '{0}d{1}s'.format(obj.days, seconds)
            return '{0}d'.format(obj.days)
        elif isinstance(obj, Enum):
            # It's up to the decoding validator to associate this name with
            # the right Enum class.
        return json.JSONEncoder.default(self, obj)

(Frankly, I wish it was easier to extend the encoder, e.g. by registering
callbacks for non-standard types.)

I don't automatically decode enums because on PUTs, POSTs, and PATCHs, I know
which attributes should be enums, so I can convert them explicitly when I
validate input forms.
Date User Action Args
2014-08-30 20:26:22barrysetrecipients: + barry, rhettinger, pitrou, ezio.melotti, eli.bendersky, ethan.furman, serhiy.storchaka, eddygeek
2014-08-30 20:26:22barrylinkissue22297 messages
2014-08-30 20:26:22barrycreate