JysonDecodeMappings » History » Version 1
Alan Kennedy, 2009-03-17 07:55 PM
1 | 1 | Alan Kennedy | h1. How jyson maps JSON data to jython data |
---|---|---|---|
2 | |||
3 | When decoding a JSON document, jyson maps JSON data types and constants to jython data types and objects as described below. |
||
4 | |||
5 | h2. JSON null |
||
6 | |||
7 | 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*). |
||
8 | |||
9 | <pre> |
||
10 | >>> from com.xhaus.jyson import JysonCodec as json |
||
11 | >>> json.loads('[null]') |
||
12 | [None] |
||
13 | >>> |
||
14 | </pre> |
||
15 | |||
16 | h2. JSON true and false. |
||
17 | |||
18 | 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). |
||
19 | |||
20 | <pre> |
||
21 | >>> from com.xhaus.jyson import JysonCodec as json |
||
22 | >>> json.loads('[true,false]') |
||
23 | [True, False] |
||
24 | >>> |
||
25 | </pre> |
||
26 | |||
27 | h2. JSON integers. |
||
28 | |||
29 | JSON integers are decoded as either a jython *int* (*org.python.core.PyInteger*) or *long* (*org.python.core.PyLong*) |
||
30 | |||
31 | <pre> |
||
32 | >>> from com.xhaus.jyson import JysonCodec as json |
||
33 | >>> json.loads('[42]') |
||
34 | [42] |
||
35 | >>> |
||
36 | </pre> |
||
37 | |||
38 | 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 |
||
39 | |||
40 | <pre> |
||
41 | >>> import sys |
||
42 | >>> json_ints = "[%d,%d]" % (sys.maxint, sys.maxint+1) |
||
43 | >>> json_ints |
||
44 | '[2147483647,2147483648]' |
||
45 | >>> json.loads(json_ints) |
||
46 | [2147483647, 2147483648L] |
||
47 | >>> |
||
48 | </pre> |
||
49 | |||
50 | h2. JSON floats |
||
51 | |||
52 | JSON floats are decoded as jython *float* (*org.python.core.PyFloat*) |
||
53 | |||
54 | <pre> |
||
55 | >>> from com.xhaus.jyson import JysonCodec as json |
||
56 | >>> json.loads('[42.0]') |
||
57 | [42.0] |
||
58 | >>> |
||
59 | </pre> |
||
60 | |||
61 | h2. JSON strings |
||
62 | |||
63 | JSON strings are decoded as jython *strings* (*org.python.core.PyString*) |
||
64 | |||
65 | <pre> |
||
66 | >>> from com.xhaus.jyson import JysonCodec as json |
||
67 | >>> json.loads('["Hello World!"]') |
||
68 | ['Hello World!'] |
||
69 | >>> |
||
70 | </pre> |
||
71 | |||
72 | h2. JSON objects |
||
73 | |||
74 | JSON objects are decoded as jython *dictionaries* (*org.python.core.PyStringMap*) |
||
75 | |||
76 | <pre> |
||
77 | >>> from com.xhaus.jyson import JysonCodec as json |
||
78 | >>> json.loads('{"hello": 1, "world": 2.0}') |
||
79 | {'world': 2.0, 'hello': 1} |
||
80 | >>> |
||
81 | </pre> |
||
82 | |||
83 | *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. |
||
84 | |||
85 | h2. JSON arrays |
||
86 | |||
87 | JSON arrays are decoded as jython *lists* (*org.python.core.PyList*) |
||
88 | |||
89 | <pre> |
||
90 | >>> from com.xhaus.jyson import JysonCodec as json |
||
91 | >>> json.loads('[1, 42, "Hello world!"]') |
||
92 | [1, 42, 'Hello world!'] |
||
93 | >>> |
||
94 | </pre> |
||
95 | |||
96 | Array elements may be of any JSON type, including arrays and objects. |