JysonEncoding » History » Version 3
Alan Kennedy, 2009-03-17 08:29 PM
1 | 1 | Alan Kennedy | h1. Using Jyson for encoding JSON objects. |
---|---|---|---|
2 | 2 | Alan Kennedy | |
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.dumps(["Hello World!"]) |
||
14 | u'["Hello World!"]' |
||
15 | >>> |
||
16 | </pre> |
||
17 | |||
18 | h2. Usage |
||
19 | |||
20 | To encode a jython object into JSON, simply pass the object to the *dumps()* method, like so |
||
21 | |||
22 | <pre> |
||
23 | >>> from com.xhaus.jyson import JysonCodec as json |
||
24 | >>> my_object = {"hello": "world", "integer": 42, "float": 42.0} |
||
25 | >>> json.dumps(my_object) |
||
26 | u'{"float":42.0,"integer":42,"hello":"world"}' |
||
27 | >>> |
||
28 | </pre> |
||
29 | |||
30 | You will notice that, because of the syntactic similarities of python/jython and javascript, the JSON representation and jython representation of the object is the same (although the order of dictionary keys may be different). |
||
31 | |||
32 | 3 | Alan Kennedy | h2. Encoding matters |
33 | 2 | Alan Kennedy | |
34 | The JSON string returned from the *dumps()* operation is a java.lang.String, i.e. *it is not byte encoded*. If you want for the string to be byte encoded, then you should do this encoding yourself. The following code sample achieves this |
||
35 | |||
36 | <pre> |
||
37 | >>> from com.xhaus.jyson import JysonCodec as json |
||
38 | >>> jython_object = [u"Hello W\u00f6rld"] |
||
39 | >>> json_data = json.dumps(jython_object) |
||
40 | >>> desired_character_encoding = 'utf-8' |
||
41 | >>> encoded_json_data = json_data.encode(desired_character_encoding) |
||
42 | >>> json_data |
||
43 | u'["Hello W\xf6rld"]' |
||
44 | >>> encoded_json_data |
||
45 | '["Hello W\xc3\xb6rld"]' |
||
46 | >>> |
||
47 | </pre> |
||
48 | |||
49 | 3 | Alan Kennedy | h2. Options to control the encoding process |
50 | 2 | Alan Kennedy | |
51 | 3 | Alan Kennedy | The options you can specify to control the JSON generation process with jyson are desribed on the [[JysonEncodeOptions|jyson encode options]] page. |
52 | 2 | Alan Kennedy | |
53 | 3 | Alan Kennedy | h2. How jyson maps jython objects to JSON objects |
54 | 2 | Alan Kennedy | |
55 | 3 | Alan Kennedy | Jyson maps specific jython object type to JSON object types in a specific way: this mapping is described on the [[JysonEncodeMappings|jyson encode mappings]] page. |
56 | 2 | Alan Kennedy | |
57 | 3 | Alan Kennedy | h2. Generating JSON representations of user classes and objects. |
58 | 2 | Alan Kennedy | |
59 | 3 | Alan Kennedy | Jyson can generate JSON representations of user classes and objects: how to do this is described on the [[JysonUserObjects|representing user objects in JSON]] page. |