JysonDecoding » History » Version 1
Alan Kennedy, 2009-03-17 03:30 PM
1 | 1 | Alan Kennedy | h1. Using Jyson for decoding json documents |
---|---|---|---|
2 | |||
3 | {{toc}} |
||
4 | |||
5 | h2. API compatibility with the cpython json module. |
||
6 | |||
7 | When I originally wrote jyson, I devised my own api, to make it suitable for usage both from jython and from java. However, as discussed on the wiki front page, I have decided that it is better for jyson to use the same API as the "cpython json module":http://docs.python.org/library/json.html . |
||
8 | |||
9 | Therefore, I have changed the jyson API to be closer to the json module API, a process that is not yet complete. However, the API can be made the same at a higher level, by using it like this (assuming that you have placed the jyson.jar file on your classpath) |
||
10 | |||
11 | <pre> |
||
12 | >>> from com.xhaus.jyson import JysonCodec as json |
||
13 | >>> json.loads('["Hello World!"]') |
||
14 | ['Hello World!'] |
||
15 | </pre> |
||
16 | |||
17 | h2. Usage |
||
18 | |||
19 | To decode JSON text into jython objects, simply pass the JSON data to the loads method, like so |
||
20 | |||
21 | <pre> |
||
22 | >>> from com.xhaus.jyson import JysonCodec as json |
||
23 | >>> json.loads('{"Hello":"World"}') |
||
24 | {'Hello': 'World'} |
||
25 | >>> json.loads('[1,1.1,null,"Hello World"]') |
||
26 | [1, 1.1, None, 'Hello World'] |
||
27 | >>> |
||
28 | </pre> |
||
29 | |||
30 | h2. Strings passed to jyson must not be byte-encoded |
||
31 | |||
32 | Note that the string that is passed to the decode method must be a jython string or java.lang.String that is *not byte encoded*. If you wish to decode a string that is byte encoded, then you should decode it yourself first before passing it to the method. The following code illustrates |
||
33 | |||
34 | <pre> |
||
35 | >>> from com.xhaus.jyson import JysonCodec as json |
||
36 | >>> utf8_json_data = '["Al\xc3\xa1in"]' |
||
37 | >>> decoded_json_data = utf8_json_data.decode('utf-8') |
||
38 | >>> decoded_json_data |
||
39 | u'["Al\xe1in"]' |
||
40 | >>> json.loads(decoded_json_data) |
||
41 | ['Al\xe1in'] |
||
42 | >>> |
||
43 | </pre> |
||
44 | |||
45 | h2. JSON data object types supported |
||
46 | |||
47 | Jyson includes builtin support for all JSON data types. |
||
48 | |||
49 | h3. JSON null |
||
50 | |||
51 | 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*). |
||
52 | |||
53 | <pre> |
||
54 | >>> from com.xhaus.jyson import JysonCodec as json |
||
55 | >>> json.loads('[null]') |
||
56 | [None] |
||
57 | >>> |
||
58 | </pre> |
||
59 | |||
60 | h3. JSON true and false. |
||
61 | |||
62 | 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). |
||
63 | |||
64 | <pre> |
||
65 | >>> from com.xhaus.jyson import JysonCodec as json |
||
66 | >>> json.loads('[true,false]') |
||
67 | [True, False] |
||
68 | >>> |
||
69 | </pre> |
||
70 | |||
71 | h3. JSON integers. |
||
72 | |||
73 | JSON integers are decoded as either a jython *int* (*org.python.core.PyInteger*) or *long* (*org.python.core.PyLong*) |
||
74 | |||
75 | <pre> |
||
76 | >>> from com.xhaus.jyson import JysonCodec as json |
||
77 | >>> json.loads('[42]') |
||
78 | [42] |
||
79 | >>> |
||
80 | </pre> |
||
81 | |||
82 | 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 |
||
83 | |||
84 | <pre> |
||
85 | >>> import sys |
||
86 | >>> json_ints = "[%d,%d]" % (sys.maxint, sys.maxint+1) |
||
87 | >>> json_ints |
||
88 | '[2147483647,2147483648]' |
||
89 | >>> json.loads(json_ints) |
||
90 | [2147483647, 2147483648L] |
||
91 | >>> |
||
92 | </pre> |
||
93 | |||
94 | h3. JSON floats |
||
95 | |||
96 | JSON floats are decoded as jython *float* (*org.python.core.PyFloat*) |
||
97 | |||
98 | <pre> |
||
99 | >>> from com.xhaus.jyson import JysonCodec as json |
||
100 | >>> json.loads('[42.0]') |
||
101 | [42.0] |
||
102 | >>> |
||
103 | </pre> |
||
104 | |||
105 | h3. JSON strings |
||
106 | |||
107 | JSON strings are decoded as jython *strings* (*org.python.core.PyString*) |
||
108 | |||
109 | <pre> |
||
110 | >>> from com.xhaus.jyson import JysonCodec as json |
||
111 | >>> json.loads('["Hello World!"]') |
||
112 | ['Hello World!'] |
||
113 | >>> |
||
114 | </pre> |
||
115 | |||
116 | h3. JSON objects |
||
117 | |||
118 | JSON objects are decoded as jython *dictionaries* (*org.python.core.PyStringMap*) |
||
119 | |||
120 | <pre> |
||
121 | >>> from com.xhaus.jyson import JysonCodec as json |
||
122 | >>> json.loads('{"hello": 1, "world": 2.0}') |
||
123 | {'world': 2.0, 'hello': 1} |
||
124 | >>> |
||
125 | </pre> |
||
126 | |||
127 | *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. |
||
128 | |||
129 | h3. JSON arrays |
||
130 | |||
131 | JSON arrays are decoded as jython *lists* (*org.python.core.PyList*) |
||
132 | |||
133 | <pre> |
||
134 | >>> from com.xhaus.jyson import JysonCodec as json |
||
135 | >>> json.loads('[1, 42, "Hello world!"]') |
||
136 | [1, 42, 'Hello world!'] |
||
137 | >>> |
||
138 | </pre> |
||
139 | |||
140 | Array elements may be of any JSON type, including arrays and objects. |
||
141 | |||
142 | h2. Options for controlling the decoding process. |
||
143 | |||
144 | There are various options that you can use to control the way that the jyson decoder treats the JSON to be decoded. |