On Sun, Aug 11, 2019 at 10:05 PM Chris Angelico <rosuav@gmail.com> wrote:
> But it makes me nervous -- I think the goal of the json module is to produce valid json, and nothing else. Opening up a protocol would allow users to fairly easily, and maybe inadvertently, create invalid JSON. I'm not sure there are compelling use cases that make that worth it.
>

You can already make invalid JSON, and far more easily.

>>> json.dumps({"spam": [1,2,3]}, separators=(' : ',', '))
'{"spam", [1 : 2 : 3]}'

Wow! I had NOT noticed that before. I have to wonder why in the world the json module makes it easy to use arbitrary separators. The use case called out in the docs is that you may want to specify different amounts of whitespace around your separators -- fair enough, but why allowing ANY separator ever made sense is beyond me.

If nothing else, a not in the docs that anything other that ":" and "," (with various whitespace) will result in invalid JSON may be worthwhile.

That may mean the cat's out of the bag, and we can neglect any role of the json module to try to enforce valid JSON, but still...
  
It's not the module's job to stop you from shooting yourself in the foot.

Sure, and I take that approach in most cases. But that doesn't mean we should hand people foot guns if there is no legitimate reason to have a gun at all.

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython