[Python-ideas] Serialization of CSV vs. JSON

David Shawley daveshawley at gmail.com
Sat Nov 3 09:01:44 EDT 2018


On Nov 2, 2018, at 12:28 PM, Calvin Spealman <cspealma at redhat.com> wrote:

> Second, JSON is a specific serialization format that explicitly rejects
> datetime objects in *all* the languages with JSON libraries. You can only
> use date objects in JSON if you control or understand both serialization
> and deserialization ends and have an agreed representation.

I would hardly say that "rejects datetime objects in *all* languages..."

Most Javascript implementations do handle dates correctly which is a bit
telling for me.  For example, the Mozilla reference calls out Date as
explicitly supported [1].  I also ran it through the Javascript console and
repl.it to make sure that it wasn't a doc glitch [2].

Go also supports serialization of date/times as shown in this repl.it
session [3].  As does rust, though rust doesn't use ISO-8601 [4].


That being said, I'm +1 on adding support for serializing datetime.date and
datetime.datetime *but* I'm -1 on automatically deserializing anything that
looks like a ISO-8601 in json.load*.  The asymmetry is the only thing that
kept me from bringing this up previously.

What about implementing this as a protocol?

The Javascript implementation of JSON.stringify looks for a method named
toJSON() when it encounters a non-primitive type and uses the result for
serialization.  This would be a pretty easy lift in json.JSONEncoder.default:

class JSONEncoder(object):
    def default(self, o):
        if hasattr(o, 'to_json'):
            return o.to_json(self)
        raise TypeError(f'Object of type {o.__class__.__name__} '
                        f'is not JSON serializable')

I would recommend passing the JSONEncoder instance in to ``to_json()`` as I
did in the snippet.  This makes serialization much easier for classes since
they do not have to assume a particular set of JSON serialization options.

Is this something that is PEP-worthy or is a PR with a simple flag to enable
the functionality in JSON encoder enough?

- cheers, dave.

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[2]: https://repl.it/@dave_shawley/OffensiveParallelResource
[3]: https://repl.it/@dave_shawley/EvenSunnyForce
[4]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=73de1454da4ac56900cde37edb0d6c8f


More information about the Python-ideas mailing list