JysonDecodeOptions » History » Version 1
Alan Kennedy, 2009-03-17 08:17 PM
1 | 1 | Alan Kennedy | h1. Options for controlling the jyson decoding process. |
---|---|---|---|
2 | |||
3 | {{toc}} |
||
4 | |||
5 | h2. Default mode is strict compliance. |
||
6 | |||
7 | If you use jyson in it's default mode, which is *strict* mode, then parsing will be performed strictly according to the original "JSON":http://json.org specification. If an error is found, a *JSONDecodeError* exception is raised, giving the location of the offending construct in the document, as described on the [[JysonErrorReporting|jyson error reporting]] page. |
||
8 | |||
9 | For a discussion of compliance with "RFC 4627":http://www.ietf.org/rfc/rfc4627.txt?number=4627 , see the [[JysonDifferences|jyson comparison]] page. |
||
10 | |||
11 | h2. Why have options? |
||
12 | |||
13 | One nice feature of JSON is that it's syntax is valid in more than one language. As well being valid "javascript":http://en.wikipedia.org/wiki/JavaScript (and thus also "ECMAScript":http://en.wikipedia.org/wiki/ECMAScript ) , JSON documents are also valid "python":http://python.org . |
||
14 | |||
15 | Python permits the user to represent data in different ways. For example, the integer constant *42* could be represented in hexadecimal as *0x2a*, an also as the octal constant *052*. When representing characters in strings, the letter *a* can be represented as *\x61* (hexadecimal escape), or as *\u0061* (unicode escape). This capability allows the user to directly bypass the character encodings of their terminals and viewing programs, and represent unambiguously what they mean. |
||
16 | |||
17 | Another important issue is that of "dangling commas":http://mail.python.org/pipermail/web-sig/2008-April/003385.html in JSON documents. It is often the case that JSON documents are written by hand (when they are template documents, for example), and contain hard to spot mistakes. It is "arguable whether JSON parsers should accept such (strictly invalid) documents":http://mail.python.org/pipermail/web-sig/2008-April/003386.html : I wrote jyson to accept dangling commas, at user option. |
||
18 | |||
19 | These are the options you can use to control parsing with jyson. |
||
20 | |||
21 | h2. How to use these options |
||
22 | |||
23 | All decode options are passed to the *loads* method as keyword arguments with boolean values *True* or *false*., like this |
||
24 | |||
25 | <pre> |
||
26 | >>> from com.xhaus.jyson import JysonCodec as json |
||
27 | >>> json.loads("42.0", accept_any_primary_datum=True) |
||
28 | 42.0 |
||
29 | >>> |
||
30 | </pre> |
||
31 | |||
32 | Which instructs jyson to accept any top level datum in the document, as described below. |
||
33 | |||
34 | h2. Option: strict_mode |
||
35 | |||
36 | This option controls all of the options listed below. If you set this option to *true*, then jyson is set into strict JSON compliance mode, and all of the options below are set to *false*. |
||
37 | |||
38 | If you set this option to *false*., then all non-JSON-conformant options listed below will be set to *true*. |
||
39 | |||
40 | The default value for the *strict_mode* option is *true*. |
||
41 | |||
42 | h2. Option: accept_any_primary_datum |
||
43 | |||
44 | This option controls whether or not jyson will accept documents that contain something other than a JSON object or array as the top level object. For example, this document is strictly invalid |
||
45 | |||
46 | <pre> |
||
47 | 42.0 |
||
48 | </pre> |
||
49 | |||
50 | because the top level object is a number, which is a breach of "RFC 4627":http://www.ietf.org/rfc/rfc4627.txt?number=4627 . |
||
51 | |||
52 | If you parse with this option set to *true*, then the above document would be accepted, as follows |
||
53 | |||
54 | <pre> |
||
55 | >>> from com.xhaus.jyson import JysonCodec as json |
||
56 | >>> json.loads("42.0") |
||
57 | com.xhaus.jyson.JSONDecodeError: JSON expressions must strictly be either objects or lists: position=4 |
||
58 | >>> json.loads("42.0", accept_any_primary_datum=True) |
||
59 | 42.0 |
||
60 | >>> |
||
61 | </pre> |
||
62 | |||
63 | The default value for *accept_any_primary_datum* is *false*. |
||
64 | |||
65 | h2. Option: accept_dangling_commas |
||
66 | |||
67 | If this is option is *true*, then dangling commas in a JSON document will be accepted. |
||
68 | |||
69 | <pre> |
||
70 | >>> from com.xhaus.jyson import JysonCodec as json |
||
71 | >>> json.loads("[1,]") |
||
72 | com.xhaus.jyson.JSONDecodeError: Commas after last element of array not accepted: position=4 |
||
73 | >>> json.loads("[1,]", accept_dangling_commas=True) |
||
74 | [1] |
||
75 | >>> json.loads('{"hello":"world",}') |
||
76 | com.xhaus.jyson.JSONDecodeError: Commas after last entry of object not accepted: position=18 |
||
77 | >>> json.loads('{"hello":"world",}', accept_dangling_commas=True) |
||
78 | {'hello': 'world'} |
||
79 | >>> |
||
80 | </pre> |
||
81 | |||
82 | The default value for *accept_dangling_commas* is *false*. |
||
83 | |||
84 | h2. Option: accept_shell_style_comments |
||
85 | |||
86 | This option controls whether jyson accepts shell style comments, as would be seen in a python program |
||
87 | |||
88 | <pre> |
||
89 | >>> from com.xhaus.jyson import JysonCodec as json |
||
90 | >>> json.loads("""# my json document |
||
91 | ... [1] |
||
92 | ... # end of my json document""") |
||
93 | com.xhaus.jyson.JSONDecodeError: Shell style comments are not accepted: position=1 |
||
94 | >>> json.loads("""# my json document |
||
95 | ... [1] |
||
96 | ... # end of my json document""", accept_shell_style_comments=True) |
||
97 | [1] |
||
98 | >>> |
||
99 | </pre> |
||
100 | |||
101 | The default value for *accept_shell_style_comments* is *false*. |
||
102 | |||
103 | h2. Option: accept_single_quoted_strings |
||
104 | |||
105 | This option controls whether jyson accepts quotes delimited with single quote characters ('is this a string?'). Such string delimiters are valid in both python and javascript, but not in JSON. |
||
106 | |||
107 | <pre> |
||
108 | >>> from com.xhaus.jyson import JysonCodec as json |
||
109 | >>> json.loads("['Hello World!']") |
||
110 | com.xhaus.jyson.JSONDecodeError: Single quoted strings are not accepted: position=2 |
||
111 | >>> json.loads("['Hello World!']", accept_single_quoted_strings=True) |
||
112 | ['Hello World!'] |
||
113 | >>> |
||
114 | </pre> |
||
115 | |||
116 | The default value for *accept_single_quoted_strings* is *false*. |
||
117 | |||
118 | h2. Option: accept_hex_char_escapes |
||
119 | |||
120 | This option controls whether jyson accepts hexadecimal character escapes, as are commonly used in python strings ("A" == "\x41") |
||
121 | |||
122 | <pre> |
||
123 | >>> from com.xhaus.jyson import JysonCodec as json |
||
124 | >>> json.loads('["This is a capital \\x41"]') |
||
125 | com.xhaus.jyson.JSONDecodeError: Hexadecimal escapes for characters are not accepted: position=22 |
||
126 | >>> json.loads('["This is a capital \\x41"]', accept_hex_char_escapes=True) |
||
127 | ['This is a capital A'] |
||
128 | >>> |
||
129 | </pre> |
||
130 | |||
131 | The default value for *accept_hex_char_escapes* is *false*. |
||
132 | |||
133 | h2. Option: accept_hexadecimal_integers |
||
134 | |||
135 | This option ontrols whether jyson accepts hexadecimal integer constants, which are indicated with a leading *0x* and are case insensitive: e.g. 255=0xFF=0xff. |
||
136 | |||
137 | <pre> |
||
138 | >>> from com.xhaus.jyson import JysonCodec as json |
||
139 | >>> json.loads('[255,0xFF,0xff]') |
||
140 | com.xhaus.jyson.JSONDecodeError: Hexadecimal integers are not accepted.: position=9 |
||
141 | >>> json.loads('[255,0xFF,0xff]', accept_hexadecimal_integers=True) |
||
142 | [255, 255, 255] |
||
143 | >>> |
||
144 | </pre> |
||
145 | |||
146 | The default value for *accept_hexadecimal_integers* is *false*. |
||
147 | |||
148 | h2. Option: accept_octal_integers |
||
149 | |||
150 | This option controls whether jyson accepts octal integer constants, which are indicated with a leading *0*: e.g. 0100=64=0x40. |
||
151 | |||
152 | <pre> |
||
153 | >>> from com.xhaus.jyson import JysonCodec as json |
||
154 | >>> json.loads('[0100,64]') |
||
155 | com.xhaus.jyson.JSONDecodeError: Octal integers are not accepted.:position=5 |
||
156 | >>> json.loads('[0100,64]', accept_octal_integers=True) |
||
157 | [64, 64] |
||
158 | >>> |
||
159 | </pre> |
||
160 | |||
161 | The default value for *accept_octal_integers* is *false*. |
||
162 | |||
163 | h2. Option: accept_junk_after_data |
||
164 | |||
165 | This option controls whether jyson accepts extraneous data after the primary object |
||
166 | |||
167 | <pre> |
||
168 | >>> json.loads('{"an": "object"} ["and some more stuff"]') |
||
169 | com.xhaus.jyson.JSONDecodeError: Only whitespace is permitted after the primary datum: not '[': position=18 |
||
170 | >>> json.loads('{"an": "object"} ["and some more stuff"]', accept_junk_after_data=True) |
||
171 | {'an': 'object'} |
||
172 | >>> |
||
173 | </pre> |
||
174 | |||
175 | The default value for *accept_junk_after_data* is *false*. |