Bulletproof json.dump?
Jon Ribbens
jon+usenet at unequivocal.eu
Mon Jul 6 09:04:41 EDT 2020
On 2020-07-06, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Jul 6, 2020 at 10:11 PM Jon Ribbens via Python-list
><python-list at python.org> wrote:
>> While I agree entirely with your point, there is however perhaps room
>> for a bit more helpfulness from the json module. There is no sensible
>> reason I can think of that it refuses to serialize sets, for example.
>
> Sets don't exist in JSON. I think that's a sensible reason.
It is not. Tuples don't exist either, and yet they're supported.
>> Going a bit further and, for example, automatically calling isoformat()
>> on date/time/datetime objects would perhaps be a bit more controversial,
>> but would frequently be useful, and there's no obvious downside that
>> occurs to me.
>
> They wouldn't round-trip without some way of knowing which strings
> represent date/times.
The 'json' module already fails to provide round-trip functionality:
>>> for data in ({True: 1}, {1: 2}, (1, 2)):
... if json.loads(json.dumps(data)) != data:
... print('oops', data, json.loads(json.dumps(data)))
...
oops {True: 1} {'true': 1}
oops {1: 2} {'1': 2}
oops (1, 2) [1, 2]
> Maybe what people want is a pretty printer instead?
No. I want a JSON encoder to output JSON to be read by a JSON decoder.
More information about the Python-list
mailing list