… | |
… | |
6 | |
6 | |
7 | use JSON::XS; |
7 | use JSON::XS; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
|
|
11 | This module converts Perl data structures to JSON and vice versa. Its |
|
|
12 | primary goal is to be I<correct> and its secondary goal is to be |
|
|
13 | I<fast>. To reach the latter goal it was written in C. |
|
|
14 | |
|
|
15 | As this is the n-th-something JSON module on CPAN, what was the reason |
|
|
16 | to write yet another JSON module? While it seems there are many JSON |
|
|
17 | modules, none of them correctly handle all corner cases, and in most cases |
|
|
18 | their maintainers are unresponsive, gone missing, or not listening to bug |
|
|
19 | reports for other reasons. |
|
|
20 | |
|
|
21 | See COMPARISON, below, for a comparison to some other JSON modules. |
|
|
22 | |
|
|
23 | =head2 FEATURES |
|
|
24 | |
11 | =over 4 |
25 | =over 4 |
12 | |
26 | |
|
|
27 | =item * correct handling of unicode issues |
|
|
28 | |
|
|
29 | This module knows how to handle Unicode, and even documents how it does so. |
|
|
30 | |
|
|
31 | =item * round-trip integrity |
|
|
32 | |
|
|
33 | When you serialise a perl data structure using only datatypes supported |
|
|
34 | by JSON, the deserialised data structure is identical on the Perl level. |
|
|
35 | (e.g. the string "2.0" doesn't suddenly become "2"). |
|
|
36 | |
|
|
37 | =item * strict checking of JSON correctness |
|
|
38 | |
|
|
39 | There is no guessing, no generating of illegal JSON strings by default, |
|
|
40 | and only JSON is accepted as input (the latter is a security feature). |
|
|
41 | |
|
|
42 | =item * fast |
|
|
43 | |
|
|
44 | compared to other JSON modules, this module compares favourably. |
|
|
45 | |
|
|
46 | =item * simple to use |
|
|
47 | |
|
|
48 | This module has both a simple functional interface as well as an OO |
|
|
49 | interface. |
|
|
50 | |
|
|
51 | =item * reasonably versatile output formats |
|
|
52 | |
|
|
53 | You can choose between the most compact format possible, a pure-ascii |
|
|
54 | format, or a pretty-printed format. Or you can combine those features in |
|
|
55 | whatever way you like. |
|
|
56 | |
|
|
57 | =back |
|
|
58 | |
13 | =cut |
59 | =cut |
14 | |
60 | |
15 | package JSON::XS; |
61 | package JSON::XS; |
16 | |
62 | |
17 | BEGIN { |
63 | BEGIN { |
18 | $VERSION = '0.1'; |
64 | $VERSION = '0.2'; |
19 | @ISA = qw(Exporter); |
65 | @ISA = qw(Exporter); |
20 | |
66 | |
|
|
67 | @EXPORT = qw(to_json from_json); |
21 | require Exporter; |
68 | require Exporter; |
22 | |
69 | |
23 | require XSLoader; |
70 | require XSLoader; |
24 | XSLoader::load JSON::XS::, $VERSION; |
71 | XSLoader::load JSON::XS::, $VERSION; |
25 | } |
72 | } |
26 | |
73 | |
27 | =item |
74 | =head1 FUNCTIONAL INTERFACE |
|
|
75 | |
|
|
76 | The following convinience methods are provided by this module. They are |
|
|
77 | exported by default: |
|
|
78 | |
|
|
79 | =over 4 |
|
|
80 | |
|
|
81 | =item $json_string = to_json $perl_scalar |
|
|
82 | |
|
|
83 | Converts the given Perl data structure (a simple scalar or a reference to |
|
|
84 | a hash or array) to a UTF-8 encoded, binary string (that is, the string contains |
|
|
85 | octets only). Croaks on error. |
|
|
86 | |
|
|
87 | This function call is functionally identical to C<< JSON::XS->new->utf8 |
|
|
88 | (1)->encode ($perl_scalar) >>. |
|
|
89 | |
|
|
90 | =item $perl_scalar = from_json $json_string |
|
|
91 | |
|
|
92 | The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to |
|
|
93 | parse that as an UTF-8 encoded JSON string, returning the resulting simple |
|
|
94 | scalar or reference. Croaks on error. |
|
|
95 | |
|
|
96 | This function call is functionally identical to C<< JSON::XS->new->utf8 |
|
|
97 | (1)->decode ($json_string) >>. |
|
|
98 | |
|
|
99 | =back |
|
|
100 | |
|
|
101 | =head1 OBJECT-ORIENTED INTERFACE |
|
|
102 | |
|
|
103 | The object oriented interface lets you configure your own encoding or |
|
|
104 | decoding style, within the limits of supported formats. |
|
|
105 | |
|
|
106 | =over 4 |
|
|
107 | |
|
|
108 | =item $json = new JSON::XS |
|
|
109 | |
|
|
110 | Creates a new JSON::XS object that can be used to de/encode JSON |
|
|
111 | strings. All boolean flags described below are by default I<disabled>. |
|
|
112 | |
|
|
113 | The mutators for flags all return the JSON object again and thus calls can |
|
|
114 | be chained: |
|
|
115 | |
|
|
116 | my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) |
|
|
117 | => {"a": [1, 2]} |
|
|
118 | |
|
|
119 | =item $json = $json->ascii ([$enable]) |
|
|
120 | |
|
|
121 | If C<$enable> is true (or missing), then the C<encode> method will |
|
|
122 | not generate characters outside the code range C<0..127>. Any unicode |
|
|
123 | characters outside that range will be escaped using either a single |
|
|
124 | \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per |
|
|
125 | RFC4627. |
|
|
126 | |
|
|
127 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
|
|
128 | characters unless necessary. |
|
|
129 | |
|
|
130 | JSON::XS->new->ascii (1)->encode (chr 0x10401) |
|
|
131 | => \ud801\udc01 |
|
|
132 | |
|
|
133 | =item $json = $json->utf8 ([$enable]) |
|
|
134 | |
|
|
135 | If C<$enable> is true (or missing), then the C<encode> method will encode |
|
|
136 | the JSON string into UTF-8, as required by many protocols, while the |
|
|
137 | C<decode> method expects to be handled an UTF-8-encoded string. Please |
|
|
138 | note that UTF-8-encoded strings do not contain any characters outside the |
|
|
139 | range C<0..255>, they are thus useful for bytewise/binary I/O. |
|
|
140 | |
|
|
141 | If C<$enable> is false, then the C<encode> method will return the JSON |
|
|
142 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
|
|
143 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
|
|
144 | to be done yourself, e.g. using the Encode module. |
|
|
145 | |
|
|
146 | =item $json = $json->pretty ([$enable]) |
|
|
147 | |
|
|
148 | This enables (or disables) all of the C<indent>, C<space_before> and |
|
|
149 | C<space_after> (and in the future possibly more) flags in one call to |
|
|
150 | generate the most readable (or most compact) form possible. |
|
|
151 | |
|
|
152 | my $json = JSON::XS->new->pretty(1)->encode ({a => [1,2]}) |
|
|
153 | => |
|
|
154 | { |
|
|
155 | "a" : [ |
|
|
156 | 1, |
|
|
157 | 2 |
|
|
158 | ] |
|
|
159 | } |
|
|
160 | |
|
|
161 | =item $json = $json->indent ([$enable]) |
|
|
162 | |
|
|
163 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline |
|
|
164 | format as output, putting every array member or object/hash key-value pair |
|
|
165 | into its own line, identing them properly. |
|
|
166 | |
|
|
167 | If C<$enable> is false, no newlines or indenting will be produced, and the |
|
|
168 | resulting JSON strings is guarenteed not to contain any C<newlines>. |
|
|
169 | |
|
|
170 | This setting has no effect when decoding JSON strings. |
|
|
171 | |
|
|
172 | =item $json = $json->space_before ([$enable]) |
|
|
173 | |
|
|
174 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
|
|
175 | optional space before the C<:> separating keys from values in JSON objects. |
|
|
176 | |
|
|
177 | If C<$enable> is false, then the C<encode> method will not add any extra |
|
|
178 | space at those places. |
|
|
179 | |
|
|
180 | This setting has no effect when decoding JSON strings. You will also most |
|
|
181 | likely combine this setting with C<space_after>. |
|
|
182 | |
|
|
183 | =item $json = $json->space_after ([$enable]) |
|
|
184 | |
|
|
185 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
|
|
186 | optional space after the C<:> separating keys from values in JSON objects |
|
|
187 | and extra whitespace after the C<,> separating key-value pairs and array |
|
|
188 | members. |
|
|
189 | |
|
|
190 | If C<$enable> is false, then the C<encode> method will not add any extra |
|
|
191 | space at those places. |
|
|
192 | |
|
|
193 | This setting has no effect when decoding JSON strings. |
|
|
194 | |
|
|
195 | =item $json = $json->canonical ([$enable]) |
|
|
196 | |
|
|
197 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
|
|
198 | by sorting their keys. This is adding a comparatively high overhead. |
|
|
199 | |
|
|
200 | If C<$enable> is false, then the C<encode> method will output key-value |
|
|
201 | pairs in the order Perl stores them (which will likely change between runs |
|
|
202 | of the same script). |
|
|
203 | |
|
|
204 | This option is useful if you want the same data structure to be encoded as |
|
|
205 | the same JSON string (given the same overall settings). If it is disabled, |
|
|
206 | the same hash migh be encoded differently even if contains the same data, |
|
|
207 | as key-value pairs have no inherent ordering in Perl. |
|
|
208 | |
|
|
209 | This setting has no effect when decoding JSON strings. |
|
|
210 | |
|
|
211 | =item $json = $json->allow_nonref ([$enable]) |
|
|
212 | |
|
|
213 | If C<$enable> is true (or missing), then the C<encode> method can convert a |
|
|
214 | non-reference into its corresponding string, number or null JSON value, |
|
|
215 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
|
|
216 | values instead of croaking. |
|
|
217 | |
|
|
218 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
|
|
219 | passed an arrayref or hashref, as JSON strings must either be an object |
|
|
220 | or array. Likewise, C<decode> will croak if given something that is not a |
|
|
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. |
|
|
240 | |
|
|
241 | =item $json_string = $json->encode ($perl_scalar) |
|
|
242 | |
|
|
243 | Converts the given Perl data structure (a simple scalar or a reference |
|
|
244 | to a hash or array) to its JSON representation. Simple scalars will be |
|
|
245 | converted into JSON string or number sequences, while references to arrays |
|
|
246 | become JSON arrays and references to hashes become JSON objects. Undefined |
|
|
247 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> |
|
|
248 | nor C<false> values will be generated. |
|
|
249 | |
|
|
250 | =item $perl_scalar = $json->decode ($json_string) |
|
|
251 | |
|
|
252 | The opposite of C<encode>: expects a JSON string and tries to parse it, |
|
|
253 | returning the resulting simple scalar or reference. Croaks on error. |
|
|
254 | |
|
|
255 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
|
|
256 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
|
|
257 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
|
|
258 | |
|
|
259 | =back |
|
|
260 | |
|
|
261 | =head1 COMPARISON |
|
|
262 | |
|
|
263 | As already mentioned, this module was created because none of the existing |
|
|
264 | JSON modules could be made to work correctly. First I will describe the |
|
|
265 | problems (or pleasures) I encountered with various existing JSON modules, |
|
|
266 | followed by some benchmark values. JSON::XS was designed not to suffer |
|
|
267 | from any of these problems or limitations. |
|
|
268 | |
|
|
269 | =over 4 |
|
|
270 | |
|
|
271 | =item JSON 1.07 |
|
|
272 | |
|
|
273 | Slow (but very portable, as it is written in pure Perl). |
|
|
274 | |
|
|
275 | Undocumented/buggy Unicode handling (how JSON handles unicode values is |
|
|
276 | undocumented. One can get far by feeding it unicode strings and doing |
|
|
277 | en-/decoding oneself, but unicode escapes are not working properly). |
|
|
278 | |
|
|
279 | No roundtripping (strings get clobbered if they look like numbers, e.g. |
|
|
280 | the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will |
|
|
281 | decode into the number 2. |
|
|
282 | |
|
|
283 | =item JSON::PC 0.01 |
|
|
284 | |
|
|
285 | Very fast. |
|
|
286 | |
|
|
287 | Undocumented/buggy Unicode handling. |
|
|
288 | |
|
|
289 | No roundtripping. |
|
|
290 | |
|
|
291 | Has problems handling many Perl values (e.g. regex results and other magic |
|
|
292 | values will make it croak). |
|
|
293 | |
|
|
294 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
|
|
295 | which is not a valid JSON string. |
|
|
296 | |
|
|
297 | Unmaintained (maintainer unresponsive for many months, bugs are not |
|
|
298 | getting fixed). |
|
|
299 | |
|
|
300 | =item JSON::Syck 0.21 |
|
|
301 | |
|
|
302 | Very buggy (often crashes). |
|
|
303 | |
|
|
304 | Very inflexible (no human-readable format supported, format pretty much |
|
|
305 | undocumented. I need at least a format for easy reading by humans and a |
|
|
306 | single-line compact format for use in a protocol, and preferably a way to |
|
|
307 | generate ASCII-only JSON strings). |
|
|
308 | |
|
|
309 | Completely broken (and confusingly documented) Unicode handling (unicode |
|
|
310 | escapes are not working properly, you need to set ImplicitUnicode to |
|
|
311 | I<different> values on en- and decoding to get symmetric behaviour). |
|
|
312 | |
|
|
313 | No roundtripping (simple cases work, but this depends on wether the scalar |
|
|
314 | value was used in a numeric context or not). |
|
|
315 | |
|
|
316 | Dumping hashes may skip hash values depending on iterator state. |
|
|
317 | |
|
|
318 | Unmaintained (maintainer unresponsive for many months, bugs are not |
|
|
319 | getting fixed). |
|
|
320 | |
|
|
321 | Does not check input for validity (i.e. will accept non-JSON input and |
|
|
322 | return "something" instead of raising an exception. This is a security |
|
|
323 | issue: imagine two banks transfering money between each other using |
|
|
324 | JSON. One bank might parse a given non-JSON request and deduct money, |
|
|
325 | while the other might reject the transaction with a syntax error. While a |
|
|
326 | good protocol will at least recover, that is extra unnecessary work and |
|
|
327 | the transaction will still not succeed). |
|
|
328 | |
|
|
329 | =item JSON::DWIW 0.04 |
|
|
330 | |
|
|
331 | Very fast. Very natural. Very nice. |
|
|
332 | |
|
|
333 | Undocumented unicode handling (but the best of the pack. Unicode escapes |
|
|
334 | still don't get parsed properly). |
|
|
335 | |
|
|
336 | Very inflexible. |
|
|
337 | |
|
|
338 | No roundtripping. |
|
|
339 | |
|
|
340 | Does not generate valid JSON (key strings are often unquoted, empty keys |
|
|
341 | result in nothing being output) |
|
|
342 | |
|
|
343 | Does not check input for validity. |
|
|
344 | |
|
|
345 | =back |
|
|
346 | |
|
|
347 | =head2 SPEED |
|
|
348 | |
|
|
349 | It seems that JSON::XS is surprisingly fast, as shown in the following |
|
|
350 | tables. They have been generated with the help of the C<eg/bench> program |
|
|
351 | in the JSON::XS distribution, to make it easy to compare on your own |
|
|
352 | system. |
|
|
353 | |
|
|
354 | First is a comparison between various modules using a very simple JSON |
|
|
355 | string, showing the number of encodes/decodes per second (JSON::XS is |
|
|
356 | the functional interface, while JSON::XS/2 is the OO interface with |
|
|
357 | pretty-printing and hashkey sorting enabled). |
|
|
358 | |
|
|
359 | module | encode | decode | |
|
|
360 | -----------|------------|------------| |
|
|
361 | JSON | 14006 | 6820 | |
|
|
362 | JSON::DWIW | 200937 | 120386 | |
|
|
363 | JSON::PC | 85065 | 129366 | |
|
|
364 | JSON::Syck | 59898 | 44232 | |
|
|
365 | JSON::XS | 1171478 | 342435 | |
|
|
366 | JSON::XS/2 | 730760 | 328714 | |
|
|
367 | -----------+------------+------------+ |
|
|
368 | |
|
|
369 | That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80 |
|
|
370 | times faster than JSON, even with pretty-printing and key sorting. |
|
|
371 | |
|
|
372 | Using a longer test string (roughly 8KB, generated from Yahoo! Locals |
|
|
373 | search API (http://nanoref.com/yahooapis/mgPdGg): |
|
|
374 | |
|
|
375 | module | encode | decode | |
|
|
376 | -----------|------------|------------| |
|
|
377 | JSON | 673 | 38 | |
|
|
378 | JSON::DWIW | 5271 | 770 | |
|
|
379 | JSON::PC | 9901 | 2491 | |
|
|
380 | JSON::Syck | 2360 | 786 | |
|
|
381 | JSON::XS | 37398 | 3202 | |
|
|
382 | JSON::XS/2 | 13765 | 3153 | |
|
|
383 | -----------+------------+------------+ |
|
|
384 | |
|
|
385 | Again, JSON::XS leads by far in the encoding case, while still beating |
|
|
386 | every other module in the decoding case. |
|
|
387 | |
|
|
388 | Last example is an almost 8MB large hash with many large binary values |
|
|
389 | (PNG files), resulting in a lot of escaping: |
|
|
390 | |
|
|
391 | =head1 BUGS |
|
|
392 | |
|
|
393 | While the goal of this module is to be correct, that unfortunately does |
|
|
394 | not mean its bug-free, only that I think its design is bug-free. It is |
|
|
395 | still very young and not well-tested. If you keep reporting bugs they will |
|
|
396 | be fixed swiftly, though. |
28 | |
397 | |
29 | =cut |
398 | =cut |
30 | |
399 | |
31 | use JSON::DWIW; |
|
|
32 | use Benchmark; |
|
|
33 | |
|
|
34 | use utf8; |
|
|
35 | #my $json = '{"ü":1,"a":[1,{"3":4},2],"b":5,"üü":2}'; |
|
|
36 | my $json = '{"test":9555555555555555555,"hu" : -1e+5, "arr" : [ 1,2,3,4,5]}'; |
|
|
37 | |
|
|
38 | my $js = JSON::XS->new; |
|
|
39 | warn $js->indent (0); |
|
|
40 | warn $js->canonical (0); |
|
|
41 | warn $js->ascii (0); |
|
|
42 | warn $js->space_after (0); |
|
|
43 | use Data::Dumper; |
|
|
44 | warn Dumper $js->decode ($json); |
|
|
45 | warn Dumper $js->encode ($js->decode ($json)); |
|
|
46 | #my $x = {"üü" => 2, "ü" => 1, "a" => [1,{3,4},2], b => 5}; |
|
|
47 | |
|
|
48 | #my $js2 = JSON::DWIW->new; |
|
|
49 | # |
|
|
50 | #timethese 200000, { |
|
|
51 | # a => sub { $js->encode ($x) }, |
|
|
52 | # b => sub { $js2->to_json ($x) }, |
|
|
53 | #}; |
|
|
54 | |
|
|
55 | 1; |
400 | 1; |
56 | |
|
|
57 | =back |
|
|
58 | |
401 | |
59 | =head1 AUTHOR |
402 | =head1 AUTHOR |
60 | |
403 | |
61 | Marc Lehmann <schmorp@schmorp.de> |
404 | Marc Lehmann <schmorp@schmorp.de> |
62 | http://home.schmorp.de/ |
405 | http://home.schmorp.de/ |