Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharoonthomas/f24a0faa3b31f0204308 to your computer and use it in GitHub Desktop.
Save sharoonthomas/f24a0faa3b31f0204308 to your computer and use it in GitHub Desktop.
Example of why JSON262 cannot be used in the real world as critical information is lost in the serialization JSON object

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
Copy link
Author

Tried with javascript too

    > typeof(JSON.parse('{"day": "2010-02-17"}').day)
    "string"

@audreyfeldroy
Copy link

@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:

JSON is agnostic about numbers. In any programming language, there can be a variety of number types of various capacities and complements, fixed or floating, binary or decimal. That can make interchange between different programming languages difficult. JSON instead offers only the representation of numbers that humans use: a sequence of digits. All programming languages know how to make sense of digit sequences even if they disagree on internal representations. That is enough to allow interchange.

Anyhow, hope this helps to clarify :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment