JysonDecodeOptions

Version 1 (Alan Kennedy, 03/17/2009 08:17 pm)

1 1
h1. Options for controlling the jyson decoding process.
2 1
3 1
{{toc}}
4 1
5 1
h2. Default mode is strict compliance.
6 1
7 1
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 1
9 1
For a discussion of compliance with "RFC 4627":http://www.ietf.org/rfc/rfc4627.txt?number=4627 , see the [[JysonDifferences|jyson comparison]] page.
10 1
11 1
h2. Why have options?
12 1
13 1
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 1
15 1
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 1
17 1
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 1
19 1
These are the options you can use to control parsing with jyson.
20 1
21 1
h2. How to use these options
22 1
23 1
All decode options are passed to the *loads* method as keyword arguments with boolean values *True* or *false*., like this
24 1
25 1
<pre>
26 1
>>> from com.xhaus.jyson import JysonCodec as json
27 1
>>> json.loads("42.0", accept_any_primary_datum=True)
28 1
42.0
29 1
>>>
30 1
</pre>
31 1
32 1
Which instructs jyson to accept any top level datum in the document, as described below.
33 1
34 1
h2. Option: strict_mode
35 1
36 1
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 1
38 1
If you set this option to *false*., then all non-JSON-conformant options listed below will be set to *true*.
39 1
40 1
The default value for the *strict_mode* option is *true*.
41 1
42 1
h2. Option: accept_any_primary_datum
43 1
44 1
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 1
46 1
<pre>
47 1
42.0
48 1
</pre>
49 1
50 1
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 1
52 1
If you parse with this option set to *true*, then the above document would be accepted, as follows
53 1
54 1
<pre>
55 1
>>> from com.xhaus.jyson import JysonCodec as json
56 1
>>> json.loads("42.0")
57 1
com.xhaus.jyson.JSONDecodeError: JSON expressions must strictly be either objects or lists: position=4
58 1
>>> json.loads("42.0", accept_any_primary_datum=True)
59 1
42.0
60 1
>>>
61 1
</pre>
62 1
63 1
The default value for *accept_any_primary_datum* is *false*.
64 1
65 1
h2. Option: accept_dangling_commas
66 1
67 1
If this is option is *true*, then dangling commas in a JSON document will be accepted.
68 1
69 1
<pre>
70 1
>>> from com.xhaus.jyson import JysonCodec as json
71 1
>>> json.loads("[1,]")
72 1
com.xhaus.jyson.JSONDecodeError: Commas after last element of array not accepted: position=4
73 1
>>> json.loads("[1,]", accept_dangling_commas=True)
74 1
[1]
75 1
>>> json.loads('{"hello":"world",}')
76 1
com.xhaus.jyson.JSONDecodeError: Commas after last entry of object not accepted: position=18
77 1
>>> json.loads('{"hello":"world",}', accept_dangling_commas=True)
78 1
{'hello': 'world'}
79 1
>>>
80 1
</pre>
81 1
82 1
The default value for *accept_dangling_commas* is *false*.
83 1
84 1
h2. Option: accept_shell_style_comments  
85 1
86 1
This option controls whether jyson accepts shell style comments, as would be seen in a python program
87 1
88 1
<pre>
89 1
>>> from com.xhaus.jyson import JysonCodec as json
90 1
>>> json.loads("""# my json document
91 1
... [1]
92 1
... # end of my json document""")
93 1
com.xhaus.jyson.JSONDecodeError: Shell style comments are not accepted: position=1
94 1
>>> json.loads("""# my json document
95 1
... [1]
96 1
... # end of my json document""", accept_shell_style_comments=True)
97 1
[1]
98 1
>>>
99 1
</pre>
100 1
101 1
The default value for *accept_shell_style_comments* is *false*.
102 1
103 1
h2. Option: accept_single_quoted_strings
104 1
105 1
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 1
107 1
<pre>
108 1
>>> from com.xhaus.jyson import JysonCodec as json
109 1
>>> json.loads("['Hello World!']")
110 1
com.xhaus.jyson.JSONDecodeError: Single quoted strings are not accepted: position=2
111 1
>>> json.loads("['Hello World!']", accept_single_quoted_strings=True)
112 1
['Hello World!']
113 1
>>>
114 1
</pre>
115 1
116 1
The default value for *accept_single_quoted_strings* is *false*.
117 1
118 1
h2. Option: accept_hex_char_escapes
119 1
120 1
This option controls whether jyson accepts hexadecimal character escapes, as are commonly used in python strings ("A" == "\x41")
121 1
122 1
<pre>
123 1
>>> from com.xhaus.jyson import JysonCodec as json
124 1
>>> json.loads('["This is a capital \\x41"]')
125 1
com.xhaus.jyson.JSONDecodeError: Hexadecimal escapes for characters are not accepted: position=22
126 1
>>> json.loads('["This is a capital \\x41"]', accept_hex_char_escapes=True)
127 1
['This is a capital A']
128 1
>>>
129 1
</pre>
130 1
131 1
The default value for *accept_hex_char_escapes* is *false*.
132 1
133 1
h2. Option: accept_hexadecimal_integers
134 1
135 1
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 1
137 1
<pre>
138 1
>>> from com.xhaus.jyson import JysonCodec as json
139 1
>>> json.loads('[255,0xFF,0xff]')
140 1
com.xhaus.jyson.JSONDecodeError: Hexadecimal integers are not accepted.: position=9
141 1
>>> json.loads('[255,0xFF,0xff]', accept_hexadecimal_integers=True)
142 1
[255, 255, 255]
143 1
>>>
144 1
</pre>
145 1
146 1
The default value for *accept_hexadecimal_integers* is *false*.
147 1
148 1
h2. Option: accept_octal_integers
149 1
150 1
This option controls whether jyson accepts octal integer constants, which are indicated with a leading *0*: e.g. 0100=64=0x40.
151 1
152 1
<pre>
153 1
>>> from com.xhaus.jyson import JysonCodec as json
154 1
>>> json.loads('[0100,64]')
155 1
com.xhaus.jyson.JSONDecodeError: Octal integers are not accepted.:position=5
156 1
>>> json.loads('[0100,64]', accept_octal_integers=True)
157 1
[64, 64]
158 1
>>>
159 1
</pre>
160 1
161 1
The default value for *accept_octal_integers* is *false*.
162 1
163 1
h2. Option: accept_junk_after_data
164 1
165 1
This option controls whether jyson accepts extraneous data after the primary object
166 1
167 1
<pre>
168 1
>>> json.loads('{"an": "object"} ["and some more stuff"]')
169 1
com.xhaus.jyson.JSONDecodeError: Only whitespace is permitted after the primary datum: not '[': position=18
170 1
>>> json.loads('{"an": "object"} ["and some more stuff"]', accept_junk_after_data=True)
171 1
{'an': 'object'}
172 1
>>>
173 1
</pre>
174 1
175 1
The default value for *accept_junk_after_data* is *false*.