JysonEncoding
Version 3 (Alan Kennedy, 03/17/2009 08:29 pm)
| 1 | 1 | h1. Using Jyson for encoding JSON objects. |
|
|---|---|---|---|
| 2 | 2 | Alan Kennedy | |
| 3 | 2 | Alan Kennedy | {{toc}} |
| 4 | 2 | Alan Kennedy | |
| 5 | 2 | Alan Kennedy | h2. API compatibility with the cpython json module. |
| 6 | 2 | Alan Kennedy | |
| 7 | 2 | Alan Kennedy | 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 | 2 | Alan Kennedy | |
| 9 | 2 | Alan Kennedy | 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 | 2 | Alan Kennedy | |
| 11 | 2 | Alan Kennedy | <pre> |
| 12 | 2 | Alan Kennedy | >>> from com.xhaus.jyson import JysonCodec as json |
| 13 | 2 | Alan Kennedy | >>> json.dumps(["Hello World!"]) |
| 14 | 2 | Alan Kennedy | u'["Hello World!"]' |
| 15 | 2 | Alan Kennedy | >>> |
| 16 | 2 | Alan Kennedy | </pre> |
| 17 | 2 | Alan Kennedy | |
| 18 | 2 | Alan Kennedy | h2. Usage |
| 19 | 2 | Alan Kennedy | |
| 20 | 2 | Alan Kennedy | To encode a jython object into JSON, simply pass the object to the *dumps()* method, like so |
| 21 | 2 | Alan Kennedy | |
| 22 | 2 | Alan Kennedy | <pre> |
| 23 | 2 | Alan Kennedy | >>> from com.xhaus.jyson import JysonCodec as json |
| 24 | 2 | Alan Kennedy | >>> my_object = {"hello": "world", "integer": 42, "float": 42.0} |
| 25 | 2 | Alan Kennedy | >>> json.dumps(my_object) |
| 26 | 2 | Alan Kennedy | u'{"float":42.0,"integer":42,"hello":"world"}' |
| 27 | 2 | Alan Kennedy | >>> |
| 28 | 2 | Alan Kennedy | </pre> |
| 29 | 2 | Alan Kennedy | |
| 30 | 2 | Alan Kennedy | 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 | 2 | Alan Kennedy | |
| 32 | 3 | Alan Kennedy | h2. Encoding matters |
| 33 | 2 | Alan Kennedy | |
| 34 | 2 | Alan Kennedy | 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 | 2 | Alan Kennedy | |
| 36 | 2 | Alan Kennedy | <pre> |
| 37 | 2 | Alan Kennedy | >>> from com.xhaus.jyson import JysonCodec as json |
| 38 | 2 | Alan Kennedy | >>> jython_object = [u"Hello W\u00f6rld"] |
| 39 | 2 | Alan Kennedy | >>> json_data = json.dumps(jython_object) |
| 40 | 2 | Alan Kennedy | >>> desired_character_encoding = 'utf-8' |
| 41 | 2 | Alan Kennedy | >>> encoded_json_data = json_data.encode(desired_character_encoding) |
| 42 | 2 | Alan Kennedy | >>> json_data |
| 43 | 2 | Alan Kennedy | u'["Hello W\xf6rld"]' |
| 44 | 2 | Alan Kennedy | >>> encoded_json_data |
| 45 | 2 | Alan Kennedy | '["Hello W\xc3\xb6rld"]' |
| 46 | 2 | Alan Kennedy | >>> |
| 47 | 2 | Alan Kennedy | </pre> |
| 48 | 2 | Alan Kennedy | |
| 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. |