… | |
… | |
59 | =cut |
59 | =cut |
60 | |
60 | |
61 | package JSON::XS; |
61 | package JSON::XS; |
62 | |
62 | |
63 | BEGIN { |
63 | BEGIN { |
64 | $VERSION = '0.1'; |
64 | $VERSION = '0.2'; |
65 | @ISA = qw(Exporter); |
65 | @ISA = qw(Exporter); |
66 | |
66 | |
67 | @EXPORT = qw(to_json from_json); |
67 | @EXPORT = qw(to_json from_json); |
68 | require Exporter; |
68 | require Exporter; |
69 | |
69 | |
… | |
… | |
114 | be chained: |
114 | be chained: |
115 | |
115 | |
116 | my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) |
116 | my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) |
117 | => {"a": [1, 2]} |
117 | => {"a": [1, 2]} |
118 | |
118 | |
119 | =item $json = $json->ascii ($enable) |
119 | =item $json = $json->ascii ([$enable]) |
120 | |
120 | |
121 | If C<$enable> is true, then the C<encode> method will not generate |
121 | If C<$enable> is true (or missing), then the C<encode> method will |
122 | characters outside the code range C<0..127>. Any unicode characters |
122 | not generate characters outside the code range C<0..127>. Any unicode |
123 | outside that range will be escaped using either a single \uXXXX (BMP |
123 | characters outside that range will be escaped using either a single |
124 | characters) or a double \uHHHH\uLLLLL escape sequence, as per RFC4627. |
124 | \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per |
|
|
125 | RFC4627. |
125 | |
126 | |
126 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
127 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
127 | characters unless necessary. |
128 | characters unless necessary. |
128 | |
129 | |
129 | JSON::XS->new->ascii (1)->encode (chr 0x10401) |
130 | JSON::XS->new->ascii (1)->encode (chr 0x10401) |
130 | => \ud801\udc01 |
131 | => \ud801\udc01 |
131 | |
132 | |
132 | =item $json = $json->utf8 ($enable) |
133 | =item $json = $json->utf8 ([$enable]) |
133 | |
134 | |
134 | If C<$enable> is true, then the C<encode> method will encode the JSON |
135 | If C<$enable> is true (or missing), then the C<encode> method will encode |
135 | string into UTF-8, as required by many protocols, while the C<decode> |
136 | the JSON string into UTF-8, as required by many protocols, while the |
136 | method expects to be handled an UTF-8-encoded string. Please note that |
137 | C<decode> method expects to be handled an UTF-8-encoded string. Please |
137 | UTF-8-encoded strings do not contain any characters outside the range |
138 | note that UTF-8-encoded strings do not contain any characters outside the |
138 | C<0..255>, they are thus useful for bytewise/binary I/O. |
139 | range C<0..255>, they are thus useful for bytewise/binary I/O. |
139 | |
140 | |
140 | If C<$enable> is false, then the C<encode> method will return the JSON |
141 | If C<$enable> is false, then the C<encode> method will return the JSON |
141 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
142 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
142 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
143 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
143 | to be done yourself, e.g. using the Encode module. |
144 | to be done yourself, e.g. using the Encode module. |
144 | |
145 | |
145 | =item $json = $json->pretty ($enable) |
146 | =item $json = $json->pretty ([$enable]) |
146 | |
147 | |
147 | This enables (or disables) all of the C<indent>, C<space_before> and |
148 | This enables (or disables) all of the C<indent>, C<space_before> and |
148 | C<space_after> (and in the future possibly more) flags in one call to |
149 | C<space_after> (and in the future possibly more) flags in one call to |
149 | generate the most readable (or most compact) form possible. |
150 | generate the most readable (or most compact) form possible. |
150 | |
151 | |
… | |
… | |
155 | 1, |
156 | 1, |
156 | 2 |
157 | 2 |
157 | ] |
158 | ] |
158 | } |
159 | } |
159 | |
160 | |
160 | =item $json = $json->indent ($enable) |
161 | =item $json = $json->indent ([$enable]) |
161 | |
162 | |
162 | If C<$enable> is true, then the C<encode> method will use a multiline |
163 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline |
163 | format as output, putting every array member or object/hash key-value pair |
164 | format as output, putting every array member or object/hash key-value pair |
164 | into its own line, identing them properly. |
165 | into its own line, identing them properly. |
165 | |
166 | |
166 | If C<$enable> is false, no newlines or indenting will be produced, and the |
167 | If C<$enable> is false, no newlines or indenting will be produced, and the |
167 | resulting JSON strings is guarenteed not to contain any C<newlines>. |
168 | resulting JSON strings is guarenteed not to contain any C<newlines>. |
168 | |
169 | |
169 | This setting has no effect when decoding JSON strings. |
170 | This setting has no effect when decoding JSON strings. |
170 | |
171 | |
171 | =item $json = $json->space_before ($enable) |
172 | =item $json = $json->space_before ([$enable]) |
172 | |
173 | |
173 | If C<$enable> is true, then the C<encode> method will add an extra |
174 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
174 | optional space before the C<:> separating keys from values in JSON objects. |
175 | optional space before the C<:> separating keys from values in JSON objects. |
175 | |
176 | |
176 | If C<$enable> is false, then the C<encode> method will not add any extra |
177 | If C<$enable> is false, then the C<encode> method will not add any extra |
177 | space at those places. |
178 | space at those places. |
178 | |
179 | |
179 | This setting has no effect when decoding JSON strings. You will also most |
180 | This setting has no effect when decoding JSON strings. You will also most |
180 | likely combine this setting with C<space_after>. |
181 | likely combine this setting with C<space_after>. |
181 | |
182 | |
182 | =item $json = $json->space_after ($enable) |
183 | =item $json = $json->space_after ([$enable]) |
183 | |
184 | |
184 | If C<$enable> is true, then the C<encode> method will add an extra |
185 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
185 | optional space after the C<:> separating keys from values in JSON objects |
186 | optional space after the C<:> separating keys from values in JSON objects |
186 | and extra whitespace after the C<,> separating key-value pairs and array |
187 | and extra whitespace after the C<,> separating key-value pairs and array |
187 | members. |
188 | members. |
188 | |
189 | |
189 | If C<$enable> is false, then the C<encode> method will not add any extra |
190 | If C<$enable> is false, then the C<encode> method will not add any extra |
190 | space at those places. |
191 | space at those places. |
191 | |
192 | |
192 | This setting has no effect when decoding JSON strings. |
193 | This setting has no effect when decoding JSON strings. |
193 | |
194 | |
194 | =item $json = $json->canonical ($enable) |
195 | =item $json = $json->canonical ([$enable]) |
195 | |
196 | |
196 | If C<$enable> is true, then the C<encode> method will output JSON objects |
197 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
197 | by sorting their keys. This is adding a comparatively high overhead. |
198 | by sorting their keys. This is adding a comparatively high overhead. |
198 | |
199 | |
199 | If C<$enable> is false, then the C<encode> method will output key-value |
200 | If C<$enable> is false, then the C<encode> method will output key-value |
200 | pairs in the order Perl stores them (which will likely change between runs |
201 | pairs in the order Perl stores them (which will likely change between runs |
201 | of the same script). |
202 | of the same script). |
… | |
… | |
205 | the same hash migh be encoded differently even if contains the same data, |
206 | the same hash migh be encoded differently even if contains the same data, |
206 | as key-value pairs have no inherent ordering in Perl. |
207 | as key-value pairs have no inherent ordering in Perl. |
207 | |
208 | |
208 | This setting has no effect when decoding JSON strings. |
209 | This setting has no effect when decoding JSON strings. |
209 | |
210 | |
210 | =item $json = $json->allow_nonref ($enable) |
211 | =item $json = $json->allow_nonref ([$enable]) |
211 | |
212 | |
212 | If C<$enable> is true, then the C<encode> method can convert a |
213 | If C<$enable> is true (or missing), then the C<encode> method can convert a |
213 | non-reference into its corresponding string, number or null JSON value, |
214 | non-reference into its corresponding string, number or null JSON value, |
214 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
215 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
215 | values instead of croaking. |
216 | values instead of croaking. |
216 | |
217 | |
217 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
218 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
218 | passed an arrayref or hashref, as JSON strings must either be an object |
219 | passed an arrayref or hashref, as JSON strings must either be an object |
219 | or array. Likewise, C<decode> will croak if given something that is not a |
220 | or array. Likewise, C<decode> will croak if given something that is not a |
220 | JSON object or array. |
221 | JSON object or array. |
|
|
222 | |
|
|
223 | =item $json = $json->shrink ([$enable]) |
|
|
224 | |
|
|
225 | Perl usually over-allocates memory a bit when allocating space for |
|
|
226 | strings. This flag optionally resizes strings generated by either |
|
|
227 | C<encode> or C<decode> to their minimum size possible. This can save |
|
|
228 | memory when your JSON strings are either very very long or you have many |
|
|
229 | short strings. |
|
|
230 | |
|
|
231 | If C<$enable> is true (or missing), the string returned by C<encode> will be shrunk-to-fit, |
|
|
232 | while all strings generated by C<decode> will also be shrunk-to-fit. |
|
|
233 | |
|
|
234 | If C<$enable> is false, then the normal perl allocation algorithms are used. |
|
|
235 | If you work with your data, then this is likely to be faster. |
|
|
236 | |
|
|
237 | In the future, this setting might control other things, such as converting |
|
|
238 | strings that look like integers or floats into integers or floats |
|
|
239 | internally (there is no difference on the Perl level), saving space. |
221 | |
240 | |
222 | =item $json_string = $json->encode ($perl_scalar) |
241 | =item $json_string = $json->encode ($perl_scalar) |
223 | |
242 | |
224 | Converts the given Perl data structure (a simple scalar or a reference |
243 | Converts the given Perl data structure (a simple scalar or a reference |
225 | to a hash or array) to its JSON representation. Simple scalars will be |
244 | to a hash or array) to its JSON representation. Simple scalars will be |
… | |
… | |
247 | followed by some benchmark values. JSON::XS was designed not to suffer |
266 | followed by some benchmark values. JSON::XS was designed not to suffer |
248 | from any of these problems or limitations. |
267 | from any of these problems or limitations. |
249 | |
268 | |
250 | =over 4 |
269 | =over 4 |
251 | |
270 | |
252 | =item JSON |
271 | =item JSON 1.07 |
253 | |
272 | |
254 | Slow (but very portable, as it is written in pure Perl). |
273 | Slow (but very portable, as it is written in pure Perl). |
255 | |
274 | |
256 | Undocumented/buggy Unicode handling (how JSON handles unicode values is |
275 | Undocumented/buggy Unicode handling (how JSON handles unicode values is |
257 | undocumented. One can get far by feeding it unicode strings and doing |
276 | undocumented. One can get far by feeding it unicode strings and doing |
… | |
… | |
259 | |
278 | |
260 | No roundtripping (strings get clobbered if they look like numbers, e.g. |
279 | No roundtripping (strings get clobbered if they look like numbers, e.g. |
261 | the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will |
280 | the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will |
262 | decode into the number 2. |
281 | decode into the number 2. |
263 | |
282 | |
264 | =item JSON::PC |
283 | =item JSON::PC 0.01 |
265 | |
284 | |
266 | Very fast. |
285 | Very fast. |
267 | |
286 | |
268 | Undocumented/buggy Unicode handling. |
287 | Undocumented/buggy Unicode handling. |
269 | |
288 | |
… | |
… | |
276 | which is not a valid JSON string. |
295 | which is not a valid JSON string. |
277 | |
296 | |
278 | Unmaintained (maintainer unresponsive for many months, bugs are not |
297 | Unmaintained (maintainer unresponsive for many months, bugs are not |
279 | getting fixed). |
298 | getting fixed). |
280 | |
299 | |
281 | =item JSON::Syck |
300 | =item JSON::Syck 0.21 |
282 | |
301 | |
283 | Very buggy (often crashes). |
302 | Very buggy (often crashes). |
284 | |
303 | |
285 | Very inflexible (no human-readable format supported, format pretty much |
304 | Very inflexible (no human-readable format supported, format pretty much |
286 | undocumented. I need at least a format for easy reading by humans and a |
305 | undocumented. I need at least a format for easy reading by humans and a |
… | |
… | |
305 | JSON. One bank might parse a given non-JSON request and deduct money, |
324 | JSON. One bank might parse a given non-JSON request and deduct money, |
306 | while the other might reject the transaction with a syntax error. While a |
325 | while the other might reject the transaction with a syntax error. While a |
307 | good protocol will at least recover, that is extra unnecessary work and |
326 | good protocol will at least recover, that is extra unnecessary work and |
308 | the transaction will still not succeed). |
327 | the transaction will still not succeed). |
309 | |
328 | |
310 | =item JSON::DWIW |
329 | =item JSON::DWIW 0.04 |
311 | |
330 | |
312 | Very fast. Very natural. Very nice. |
331 | Very fast. Very natural. Very nice. |
313 | |
332 | |
314 | Undocumented unicode handling (but the best of the pack. Unicode escapes |
333 | Undocumented unicode handling (but the best of the pack. Unicode escapes |
315 | still don't get parsed properly). |
334 | still don't get parsed properly). |