jsondate, for example, supports both .load[s]() and .dump[s](); but only for UTC datetimes<div><br></div><div><a href="https://github.com/rconradharris/jsondate/blob/master/jsondate/__init__.py">https://github.com/rconradharris/jsondate/blob/master/jsondate/__init__.py</a></div><div><br></div><div>UTC is only sometimes a fair assumption; otherwise it's dangerous to assume that timezone-naieve [ISO8601] strings represent UTC-0 datetimes. In that respect - aside from readability - arbitrary-precision POSIX timestamps are less error-prone.<br><div><br></div><div><br><br>On Saturday, November 3, 2018, David Shawley <<a href="mailto:daveshawley@gmail.com">daveshawley@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">On Nov 3, 2018, at 9:29 AM, Chris Angelico <<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>> wrote:<br><div><blockquote type="cite"><br><div><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">I think we need to clarify an important distinction here. JSON, as a</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">format, does *not* support date/time objects in any way. But</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">JavaScript's JSON.stringify() function is happy to accept them, and</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">will represent them as strings.</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"></div></blockquote><div><br></div><div><div>Very good point.  The JSON document type only supports object literals,</div><div>numbers, strings, and Boolean literals.  My suggestion was specifically to</div><div>provide an extensible mechanism for encoding arbitrary objects into the</div><div>supported primitives.</div></div><br><blockquote type="cite"><div><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">If the suggestion here is to have json.dumps(datetime.date(2018,<wbr>11,4))</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">to return an encoded string, either by natively supporting it, or by</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">having a protocol which the date object implements, that's fine and</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">reasonable; but json.loads(s) won't return that date object. So, yes,</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">it would be asymmetric. I personally don't have a problem with this</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">(though I also don't have any strong use-cases). Custom encoders and</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">decoders could do this, with or without symmetry. What would it be</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">like to add a couple to the json module that can handle these extra</span><br style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Verdana;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">types?</span></div></blockquote></div><br><div><div>Completely agreed here.  I've seen many attempts to support "round trip"</div><div>encode/decode in JSON libraries and it really doesn't work well unless you go</div><div>down the path of type hinting.  I believe that MongoDB uses something akin to</div><div>hinting when it handles dates.  Something like the following representation</div><div>if I recall correctly.</div><div><br></div><div>{</div><div>    "now": {</div><div>        "$type": "JSONDate",</div><div>        "value": "2018-11-03T09:52:20-0400"</div><div>    }</div><div>}</div><div><br></div><div>During deserialization they recognize the hint and instantiate the object</div><div>instead of parsing it.  This is interesting but pretty awful for</div><div>interoperability since there isn't a standard that I'm aware of.  I'm</div><div>certainly not proposing that but I did want to mention it for completeness.</div><div><br></div><div>I'll try to put together a PR/branch that adds protocol support in JSON encoder</div><div>and to datetime, date, and uuid as well.  It will give us something to point at</div><div>and discuss.</div></div><div><br></div><div>- cheers, dave.</div><div><div><span style="color:rgb(34,34,34);background-color:rgb(255,255,255)"><font face="Helvetica-Light" size="2">--</font></span></div><div><font color="#222222" face="Helvetica-Light" size="2">Mathematics provides a framework for dealing precisely with notions of "what is".</font></div><div><span style="color:rgb(34,34,34);font-family:Helvetica-Light;font-size:small">Computation provides a framework for dealing precisely with notions of "how to".</span></div><div><span style="color:rgb(34,34,34);font-family:Helvetica-Light;font-size:small">SICP Preface</span></div></div></div></blockquote></div></div>