ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.pm
(Generate patch)

Comparing JSON-XS/XS.pm (file contents):
Revision 1.1 by root, Thu Mar 22 16:40:16 2007 UTC vs.
Revision 1.3 by root, Thu Mar 22 18:10:29 2007 UTC

6 6
7 use JSON::XS; 7 use JSON::XS;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module converts Perl data structures to JSON and vice versa. Its
12primary goal is to be I<correct> and its secondary goal is to be
13I<fast>. To reach the latter goal it was written in C.
14
15As this is the n-th-something JSON module on CPAN, what was the reason
16to write yet another JSON module? While it seems there are many JSON
17modules, none of them correctly handle all corner cases, and in most cases
18their maintainers are unresponsive, gone missing, or not listening to bug
19reports for other reasons.
20
21See COMPARISON, below, for a comparison to some other JSON modules.
22
23=head2 FEATURES
24
11=over 4 25=over 4
26
27=item * correct handling of unicode issues
28
29This module knows how to handle Unicode, and even documents how it does so.
30
31=item * round-trip integrity
32
33When you serialise a perl data structure using only datatypes supported
34by 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
39There is no guessing, no generating of illegal JSON strings by default,
40and only JSON is accepted as input (the latter is a security feature).
41
42=item * fast
43
44compared to other JSON modules, this module compares favourably.
45
46=item * simple to use
47
48This module has both a simple functional interface as well as an OO
49interface.
50
51=item * reasonably versatile output formats
52
53You can choose between the most compact format possible, a pure-ascii
54format, or a pretty-printed format. Or you can combine those features in
55whatever way you like.
56
57=back
12 58
13=cut 59=cut
14 60
15package JSON::XS; 61package JSON::XS;
16 62
17BEGIN { 63BEGIN {
18 $VERSION = '0.1'; 64 $VERSION = '0.1';
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
76The following convinience methods are provided by this module. They are
77exported by default:
78
79=over 4
80
81=item $json_string = to_json $perl_scalar
82
83Converts the given Perl data structure (a simple scalar or a reference to
84a hash or array) to a UTF-8 encoded, binary string (that is, the string contains
85octets only). Croaks on error.
86
87This 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
92The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to
93parse that as an UTF-8 encoded JSON string, returning the resulting simple
94scalar or reference. Croaks on error.
95
96This 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
103The object oriented interface lets you configure your own encoding or
104decoding style, within the limits of supported formats.
105
106=over 4
107
108=item $json = new JSON::XS
109
110Creates a new JSON::XS object that can be used to de/encode JSON
111strings. All boolean flags described below are by default I<disabled>.
112
113The mutators for flags all return the JSON object again and thus calls can
114be 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
121If C<$enable> is true, then the C<encode> method will not generate
122characters outside the code range C<0..127>. Any unicode characters
123outside that range will be escaped using either a single \uXXXX (BMP
124characters) or a double \uHHHH\uLLLLL escape sequence, as per RFC4627.
125
126If C<$enable> is false, then the C<encode> method will not escape Unicode
127characters unless necessary.
128
129 JSON::XS->new->ascii (1)->encode (chr 0x10401)
130 => \ud801\udc01
131
132=item $json = $json->utf8 ($enable)
133
134If C<$enable> is true, then the C<encode> method will encode the JSON
135string into UTF-8, as required by many protocols, while the C<decode>
136method expects to be handled an UTF-8-encoded string. Please note that
137UTF-8-encoded strings do not contain any characters outside the range
138C<0..255>, they are thus useful for bytewise/binary I/O.
139
140If C<$enable> is false, then the C<encode> method will return the JSON
141string as a (non-encoded) unicode string, while C<decode> expects thus a
142unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs
143to be done yourself, e.g. using the Encode module.
144
145=item $json = $json->pretty ($enable)
146
147This enables (or disables) all of the C<indent>, C<space_before> and
148C<space_after> (and in the future possibly more) flags in one call to
149generate the most readable (or most compact) form possible.
150
151 my $json = JSON::XS->new->pretty(1)->encode ({a => [1,2]})
152 =>
153 {
154 "a" : [
155 1,
156 2
157 ]
158 }
159
160=item $json = $json->indent ($enable)
161
162If C<$enable> is true, then the C<encode> method will use a multiline
163format as output, putting every array member or object/hash key-value pair
164into its own line, identing them properly.
165
166If C<$enable> is false, no newlines or indenting will be produced, and the
167resulting JSON strings is guarenteed not to contain any C<newlines>.
168
169This setting has no effect when decoding JSON strings.
170
171=item $json = $json->space_before ($enable)
172
173If C<$enable> is true, then the C<encode> method will add an extra
174optional space before the C<:> separating keys from values in JSON objects.
175
176If C<$enable> is false, then the C<encode> method will not add any extra
177space at those places.
178
179This setting has no effect when decoding JSON strings. You will also most
180likely combine this setting with C<space_after>.
181
182=item $json = $json->space_after ($enable)
183
184If C<$enable> is true, then the C<encode> method will add an extra
185optional space after the C<:> separating keys from values in JSON objects
186and extra whitespace after the C<,> separating key-value pairs and array
187members.
188
189If C<$enable> is false, then the C<encode> method will not add any extra
190space at those places.
191
192This setting has no effect when decoding JSON strings.
193
194=item $json = $json->canonical ($enable)
195
196If C<$enable> is true, then the C<encode> method will output JSON objects
197by sorting their keys. This is adding a comparatively high overhead.
198
199If C<$enable> is false, then the C<encode> method will output key-value
200pairs in the order Perl stores them (which will likely change between runs
201of the same script).
202
203This option is useful if you want the same data structure to be encoded as
204the same JSON string (given the same overall settings). If it is disabled,
205the same hash migh be encoded differently even if contains the same data,
206as key-value pairs have no inherent ordering in Perl.
207
208This setting has no effect when decoding JSON strings.
209
210=item $json = $json->allow_nonref ($enable)
211
212If C<$enable> is true, then the C<encode> method can convert a
213non-reference into its corresponding string, number or null JSON value,
214which is an extension to RFC4627. Likewise, C<decode> will accept those JSON
215values instead of croaking.
216
217If C<$enable> is false, then the C<encode> method will croak if it isn't
218passed an arrayref or hashref, as JSON strings must either be an object
219or array. Likewise, C<decode> will croak if given something that is not a
220JSON object or array.
221
222=item $json_string = $json->encode ($perl_scalar)
223
224Converts the given Perl data structure (a simple scalar or a reference
225to a hash or array) to its JSON representation. Simple scalars will be
226converted into JSON string or number sequences, while references to arrays
227become JSON arrays and references to hashes become JSON objects. Undefined
228Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true>
229nor C<false> values will be generated.
230
231=item $perl_scalar = $json->decode ($json_string)
232
233The opposite of C<encode>: expects a JSON string and tries to parse it,
234returning the resulting simple scalar or reference. Croaks on error.
235
236JSON numbers and strings become simple Perl scalars. JSON arrays become
237Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes
238C<1>, C<false> becomes C<0> and C<null> becomes C<undef>.
239
240=back
241
242=head1 COMPARISON
243
244As already mentioned, this module was created because none of the existing
245JSON modules could be made to work correctly. First I will describe the
246problems (or pleasures) I encountered with various existing JSON modules,
247followed by some benchmark values.
248
249=over 4
250
251=item JSON
252
253Slow (but very portable, as it is written in pure Perl).
254
255Undocumented/buggy Unicode handling (how JSON handles unicode values is
256undocumented. One can get far by feeding it unicode strings and doing
257en-/decoding oneself, but unicode escapes are not working properly).
258
259No roundtripping (strings get clobbered if they look like numbers, e.g.
260the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will
261decode into the number 2.
262
263=item JSON::PC
264
265Very fast.
266
267Very inflexible (no human-readable format supported, format pretty much
268undocumented. I need at least a format for easy reading by humans and a
269single-line compact format for use in a protocol, and preferably a way to
270generate ASCII-only JSON strings).
271
272Undocumented/buggy Unicode handling.
273
274No roundtripping.
275
276Has problems handling many Perl values.
277
278Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}>
279which is not a valid JSON string.
280
281Unmaintained (maintainer unresponsive for many months, bugs are not
282getting fixed).
283
284=item JSON::Syck
285
286Very buggy (often crashes).
287
288Very inflexible.
289
290Completely broken (and confusingly documented) Unicode handling (unicode
291escapes are not working properly, you need to set ImplicitUnicode to
292I<different> values on en- and decoding to get symmetric behaviour).
293
294No roundtripping (simple cases work, but this depends on wether the scalar
295value was used in a numeric context or not).
296
297Dumping hashes may skip hash values depending on iterator state.
298
299Unmaintained (maintainer unresponsive for many months, bugs are not
300getting fixed).
301
302Does not check input for validity (i.e. will accept non-JSON input and
303return "something" instead of raising an exception. This is a security
304issue: imagine two banks transfering money between each other using
305JSON. One bank might parse a given non-JSON request and deduct money,
306while the other might reject the transaction with a syntax error. While a
307good protocol will at least recover, that is extra unnecessary work and
308the transaction will still not succeed).
309
310=item JSON::DWIW
311
312Very fast. Very natural. Very nice.
313
314Undocumented unicode handling (but the best of the pack. Unicode escapes
315still don't get parsed properly).
316
317Very inflexible.
318
319No roundtripping.
320
321Does not check input for validity.
322
323=back
324
325=head2 SPEED
28 326
29=cut 327=cut
30 328
31use JSON::DWIW;
32use Benchmark;
33
34use utf8;
35#my $json = '{"ü":1,"a":[1,{"3":4},2],"b":5,"üü":2}';
36my $json = '{"test":9555555555555555555,"hu" : -1e+5, "arr" : [ 1,2,3,4,5]}';
37
38my $js = JSON::XS->new;
39warn $js->indent (0);
40warn $js->canonical (0);
41warn $js->ascii (0);
42warn $js->space_after (0);
43use Data::Dumper;
44warn Dumper $js->decode ($json);
45warn 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
551; 3291;
56
57=back
58 330
59=head1 AUTHOR 331=head1 AUTHOR
60 332
61 Marc Lehmann <schmorp@schmorp.de> 333 Marc Lehmann <schmorp@schmorp.de>
62 http://home.schmorp.de/ 334 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines