[Python-ideas] Re: JSON encoder protocol

2019-08-17 Thread Christopher Barker
> > But it does become worse if you have lots of classes you want to > serialize. You need to write a default function somewhere that knows about > all of your classes: > Somehow I didn't think of this until now, but when I've needed to use JSON to serialize a non-trivial hierachy of types, I' es

[Python-ideas] Re: JSON encoder protocol

2019-08-17 Thread Paul Moore
On Sat, 17 Aug 2019 at 08:28, Andrew Barnert via Python-ideas wrote: > def jsonize_my_tree(obj): > if isinstance(obj, MyTreeNode): > return [obj.data(), *map(jsonize_my_tree, obj.children())] > raise TypeError() > > myjson = json.dumps(my_thing_that_might_includ

[Python-ideas] Re: JSON encoder protocol

2019-08-17 Thread Kyle Stanley
Ah, that makes sense, thank you for the detailed example and explanation. I had only read through the start of the previous topic, as it started to become a bit difficult to follow (largely due to the author being non-specific with what precisely they were looking for and the discussion forking off

[Python-ideas] Re: JSON encoder protocol

2019-08-17 Thread Andrew Barnert via Python-ideas
On Aug 16, 2019, at 20:35, Kyle Stanley wrote: > > Speaking of examples, I think it would be helpful to provide a brief example > of ``object.__json__()`` being used for some added clarity. Would it be a > direct alias of ``json.dumps()`` with the same exact parameters and usage, or > would th

[Python-ideas] Re: JSON encoder protocol

2019-08-16 Thread Kyle Stanley
> True for yourself, I assume. But json.dumps is *not* why *the rest of > us* do that. We do it because we've *always* done it. The Python > objects we are serializing themselves lack units, precision, and pet's > name! Until our Python programs become unit- and precision- aware, > support for

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-15 Thread Wes Turner
On Thursday, August 15, 2019, Andrew Barnert wrote: > On Aug 15, 2019, at 10:23, Christopher Barker wrote: > > > > This is all making me think it's time for a broader discussion / PEP: > > > > The future of the JSON module. > > I think this is overreacting. There’s really only two issues here, a

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-15 Thread Andrew Barnert via Python-ideas
On Aug 15, 2019, at 10:23, Christopher Barker wrote: > > This is all making me think it's time for a broader discussion / PEP: > > The future of the JSON module. I think this is overreacting. There’s really only two issues here, and neither one is that major. But nobody has shown any need for

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-15 Thread Christopher Barker
This is all making me think it's time for a broader discussion / PEP: The future of the JSON module. *maybe* the way forward is to try to make a new "category killer" JSON lib (or at least a platform for standard protocols). But it also may make sense to start within Python itself -- at least for

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-15 Thread Wes Turner
On Thursday, August 15, 2019, Andrew Barnert wrote: > On Aug 14, 2019, at 20:35, Wes Turner wrote: > > > A few questions then are: > > > > Should __json__() be versioned? > > Why? While there are sort of multiple versions of JSON (pre-spec, ECMA404, > and the successive RFCs), even when you know

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-15 Thread Andrew Barnert via Python-ideas
On Aug 14, 2019, at 20:35, Wes Turner wrote: > A few questions then are: > > Should __json__() be versioned? Why? While there are sort of multiple versions of JSON (pre-spec, ECMA404, and the successive RFCs), even when you know which one you’re dealing with, there’s not really anything you’d

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Wes Turner
Setting aside the arguments for just having reusable linked data JSON-LD in the first place (which is also JSON that a non JSON-LD app can pretty easily consume), A few questions then are: Should __json__() be versioned? Should __json__() return JSON5 (with IEEE 754 ±Infinity and NaN)? What prev

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Andrew Barnert via Python-ideas
On Aug 14, 2019, at 19:48, Wes Turner wrote: > > Native serialization and deserialization support for either or both JSON5, > JSON lines, and JSON-LD would be great to have. PyPI has packages for all of these things (plus the two not-quite-identical variations on JSONlines, and probably other

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Wes Turner
On Wednesday, August 14, 2019, Wes Turner wrote: > There's JSON5; which supports comments, trailing commas, IEEE 754 > ±Infinity and NaN, [...] > https://json5.org/ > > There's also JSON-LD, which supports xsd:datetime, everything that can be > expressed as RDF, @context vocabulary, compact and e

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Wes Turner
There's JSON5; which supports comments, trailing commas, IEEE 754 ±Infinity and NaN, [...] https://json5.org/ There's also JSON-LD, which supports xsd:datetime, everything that can be expressed as RDF, @context vocabulary, compact and expanded representations, @id, and lots of other cool features

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Andrew Barnert via Python-ideas
On Aug 14, 2019, at 11:38, Chris Angelico wrote: > > Side point: Does anyone else think it was an egotistical idea to > create JSON as a non-versioned specification? "I can't possibly get > this wrong". And now look what's happened. Well, it was supposed to be an antidote to complicated specific

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Chris Angelico
On Thu, Aug 15, 2019 at 2:23 AM Random832 wrote: > > On Sun, Aug 11, 2019, at 20:42, Chris Angelico wrote: > > class float: > > def __json__(self): > > if math.isfinite(self): return str(self) > > return "null" > > Er, to be clear, the current JSON decoder returns 'Infinity', '

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Andrew Barnert via Python-ideas
On Aug 14, 2019, at 09:22, Random832 wrote: > >> On Sun, Aug 11, 2019, at 20:42, Chris Angelico wrote: >> class float: >>def __json__(self): >>if math.isfinite(self): return str(self) >>return "null" > > Er, to be clear, the current JSON decoder returns 'Infinity', '-Infinity

[Python-ideas] Re: JSON encoder protocol (was Re: adding support for a "raw output" in JSON serializer)

2019-08-14 Thread Random832
On Sun, Aug 11, 2019, at 20:42, Chris Angelico wrote: > class float: > def __json__(self): > if math.isfinite(self): return str(self) > return "null" Er, to be clear, the current JSON decoder returns 'Infinity', '-Infinity', or 'NaN', which are invalid JSON, not null. This beh