Using the same example given in the json262 documentation
>>> import datetime
>>> import json
>>> from json262 import JSON262Encoder
>>> data = {'day': datetime.date(2010, 2, 17)}
>>> data == json.loads(json.dumps(data, cls=JSON262Encoder))
False
>>> json.loads(json.dumps(data, cls=JSON262Encoder))
{u'day': u'2010-02-17'}
When decoded there is no way to distinguish the value of the attribute day as a date. So JSON decoder decodes it as unicode.
To overcome this limitation, tryton project which implements RPC over JSON implements its JSON encoder and decoder.
(Install using pip install trytond)
>>> import datetime
>>> import json
>>> from trytond.protocols.jsonrpc import object_hook, JSONEncoder
>>> data = {'day': datetime.date(2010, 2, 17)}
>>> data == json.loads(json.dumps(data, cls=JSONEncoder), object_hook=object_hook)
True
@sharoonthomas thanks for writing this up!
I think your use case is valid, but that it's not a good use case for this package. The use case I have in mind is for taking Python objects and turning them into language-independent JSON. As described on json.org, "JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language." I will add this bit to the FAQ in the moment.
Also from the FAQ:
Does StandardJSONEncoder provide info about the Python type of the object?
No. StandardJSONEncoder purposely does not, in favor of a human-style, type-agnostic approach.
When encoded by StandardJSONEncoder, there is no differentiation between the string "2010-02-17" and the date object date(2010, 2, 17)}. This is the same approach described in ECMA-404, Introduction, paragraph 2:
Anyhow, hope this helps to clarify :)