I totally agree, but the JSONEncoder/JSONDecoder mechanism already exist. I'm certainly removing stuff. I'm just proposing adding a simpler way to do one of the things - serialising. An example use case would be creating a simple class for something like a REST API and that class having a __json__ function. Python doesn't need to deserialise the JSON, so you only need one half of the story, the half that __json__ would make easier.
--- Dan Cojocaru On 6 Apr 2020, 21:48 +0300, Andrew Barnert <abarn...@yahoo.com>, wrote: > On Apr 6, 2020, at 09:22, Dan Cojocaru <dan.cojocar...@e-uvt.ro> wrote: > > > > > > Addressing your last concern, about __json__ being only class -> JSON, not > > JSON -> class, classes implementing __str__ only go class -> str, not str > > -> class. > > Sure, but str is inherently one-way. There’s no unambiguous mapping from > human-readable text to data. > > That’s not true for JSON; the entire point of it is data interchange. You > expect to be able to dump an object, send it over the wire or store it to a > file, load it (or even parse it in JS or ObjC or Go or whatever) and get back > an equivalent object. It’s easy to come up with ways to build on top of JSON > to interchange things like time points or raw binary strings or higher-level > structured objects, but they require doing something on both the encode side > and the decode side. Just being able to encode them to something > human-readable is useless—if I encode a datetime object, I need to get back a > datetime (or Date or NSDate or whatever) on the other end, not a str (or > string or NSString or whatever) that a human could tell is probably meant to > be a datetime but will raise an exception when I try to subtract it from > now(). > > (Of course JSON isn’t perfect, as anyone who’s tried to interchange, say, > int64 values discovers… but it’s good enough for many applications.) > >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/6YUK427KZC7LFI5NNPYH7A622CR4OT4J/ Code of Conduct: http://python.org/psf/codeofconduct/