Project

General

Profile

Actions

How jyson maps JSON data to jython data

When decoding a JSON document, jyson maps JSON data types and constants to jython data types and objects as described below.

JSON null

The JSON constant null is decoded as a jython None (specifically, the jython singleton object Py.None which is of type org.python.core.PyObject).

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('[null]')
[None]
>>>

JSON true and false.

The JSON constants true and false are decoded as jython True and False respectively (specifically as the jython singleton objects Py.True and Py.False respectively).

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('[true,false]')
[True, False]
>>>

JSON integers.

JSON integers are decoded as either a jython int (org.python.core.PyInteger) or long (org.python.core.PyLong)

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('[42]')
[42]
>>>

If an JSON integer is greater than the maximum value representable by a jython integer, then it will instead be returned as a jython long. The following illustrates

>>> import sys
>>> json_ints = "[%d,%d]" % (sys.maxint, sys.maxint+1)
>>> json_ints
'[2147483647,2147483648]'
>>> json.loads(json_ints)
[2147483647, 2147483648L]
>>>

JSON floats

JSON floats are decoded as jython float (org.python.core.PyFloat)

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('[42.0]')
[42.0]
>>>

JSON strings

JSON strings are decoded as jython strings (org.python.core.PyString)

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('["Hello World!"]')
['Hello World!']
>>>

JSON objects

JSON objects are decoded as jython dictionaries (org.python.core.PyStringMap)

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('{"hello": 1, "world": 2.0}')
{'world': 2.0, 'hello': 1}
>>>

NOTE Only objects with string keys can be decoded, as according to the JSON spec. Any attempt to decode an object with a non-string key will cause a JSONDecodeError. The values in an object can be any JSON type, including arrays and objects.

JSON arrays

JSON arrays are decoded as jython lists (org.python.core.PyList)

>>> from com.xhaus.jyson import JysonCodec as json
>>> json.loads('[1, 42, "Hello world!"]')
[1, 42, 'Hello world!']
>>>

Array elements may be of any JSON type, including arrays and objects.

Updated by Alan Kennedy about 15 years ago ยท 1 revisions