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