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.7 by root, Fri Mar 23 15:10:55 2007 UTC vs.
Revision 1.11 by root, Fri Mar 23 17:48:59 2007 UTC

18their maintainers are unresponsive, gone missing, or not listening to bug 18their maintainers are unresponsive, gone missing, or not listening to bug
19reports for other reasons. 19reports for other reasons.
20 20
21See COMPARISON, below, for a comparison to some other JSON modules. 21See COMPARISON, below, for a comparison to some other JSON modules.
22 22
23See MAPPING, below, on how JSON::XS maps perl values to JSON values and
24vice versa.
25
23=head2 FEATURES 26=head2 FEATURES
24 27
25=over 4 28=over 4
26 29
27=item * correct handling of unicode issues 30=item * correct handling of unicode issues
28 31
29This module knows how to handle Unicode, and even documents how it does so. 32This module knows how to handle Unicode, and even documents how and when
33it does so.
30 34
31=item * round-trip integrity 35=item * round-trip integrity
32 36
33When you serialise a perl data structure using only datatypes supported 37When you serialise a perl data structure using only datatypes supported
34by JSON, the deserialised data structure is identical on the Perl level. 38by JSON, the deserialised data structure is identical on the Perl level.
35(e.g. the string "2.0" doesn't suddenly become "2"). 39(e.g. the string "2.0" doesn't suddenly become "2").
36 40
37=item * strict checking of JSON correctness 41=item * strict checking of JSON correctness
38 42
39There is no guessing, no generating of illegal JSON strings by default, 43There is no guessing, no generating of illegal JSON strings by default,
40and only JSON is accepted as input (the latter is a security feature). 44and only JSON is accepted as input by default (the latter is a security
45feature).
41 46
42=item * fast 47=item * fast
43 48
44compared to other JSON modules, this module compares favourably. 49Compared to other JSON modules, this module compares favourably in terms
50of speed, too.
45 51
46=item * simple to use 52=item * simple to use
47 53
48This module has both a simple functional interface as well as an OO 54This module has both a simple functional interface as well as an OO
49interface. 55interface.
50 56
51=item * reasonably versatile output formats 57=item * reasonably versatile output formats
52 58
53You can choose between the most compact format possible, a pure-ascii 59You can choose between the most compact guarenteed single-line format
54format, or a pretty-printed format. Or you can combine those features in 60possible (nice for simple line-based protocols), a pure-ascii format (for
61when your transport is not 8-bit clean), or a pretty-printed format (for
62when you want to read that stuff). Or you can combine those features in
55whatever way you like. 63whatever way you like.
56 64
57=back 65=back
58 66
59=cut 67=cut
60 68
61package JSON::XS; 69package JSON::XS;
62 70
63BEGIN { 71BEGIN {
64 $VERSION = '0.2'; 72 $VERSION = '0.3';
65 @ISA = qw(Exporter); 73 @ISA = qw(Exporter);
66 74
67 @EXPORT = qw(to_json from_json); 75 @EXPORT = qw(to_json from_json);
68 require Exporter; 76 require Exporter;
69 77
82 90
83Converts the given Perl data structure (a simple scalar or a reference to 91Converts 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 92a hash or array) to a UTF-8 encoded, binary string (that is, the string contains
85octets only). Croaks on error. 93octets only). Croaks on error.
86 94
87This function call is functionally identical to C<< JSON::XS->new->utf8 95This function call is functionally identical to C<< JSON::XS->new->utf8->encode ($perl_scalar) >>.
88(1)->encode ($perl_scalar) >>.
89 96
90=item $perl_scalar = from_json $json_string 97=item $perl_scalar = from_json $json_string
91 98
92The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to 99The 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 100parse that as an UTF-8 encoded JSON string, returning the resulting simple
94scalar or reference. Croaks on error. 101scalar or reference. Croaks on error.
95 102
96This function call is functionally identical to C<< JSON::XS->new->utf8 103This function call is functionally identical to C<< JSON::XS->new->utf8->decode ($json_string) >>.
97(1)->decode ($json_string) >>.
98 104
99=back 105=back
100 106
101=head1 OBJECT-ORIENTED INTERFACE 107=head1 OBJECT-ORIENTED INTERFACE
102 108
224 230
225Perl usually over-allocates memory a bit when allocating space for 231Perl usually over-allocates memory a bit when allocating space for
226strings. This flag optionally resizes strings generated by either 232strings. This flag optionally resizes strings generated by either
227C<encode> or C<decode> to their minimum size possible. This can save 233C<encode> or C<decode> to their minimum size possible. This can save
228memory when your JSON strings are either very very long or you have many 234memory when your JSON strings are either very very long or you have many
229short strings. 235short strings. It will also try to downgrade any strings to octet-form
236if possible: perl stores strings internally either in an encoding called
237UTF-X or in octet-form. The latter cannot store everything but uses less
238space in general.
230 239
231If C<$enable> is true (or missing), the string returned by C<encode> will be shrunk-to-fit, 240If C<$enable> is true (or missing), the string returned by C<encode> will be shrunk-to-fit,
232while all strings generated by C<decode> will also be shrunk-to-fit. 241while all strings generated by C<decode> will also be shrunk-to-fit.
233 242
234If C<$enable> is false, then the normal perl allocation algorithms are used. 243If C<$enable> is false, then the normal perl allocation algorithms are used.
253returning the resulting simple scalar or reference. Croaks on error. 262returning the resulting simple scalar or reference. Croaks on error.
254 263
255JSON numbers and strings become simple Perl scalars. JSON arrays become 264JSON numbers and strings become simple Perl scalars. JSON arrays become
256Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes 265Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes
257C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. 266C<1>, C<false> becomes C<0> and C<null> becomes C<undef>.
267
268=back
269
270=head1 MAPPING
271
272This section describes how JSON::XS maps Perl values to JSON values and
273vice versa. These mappings are designed to "do the right thing" in most
274circumstances automatically, preserving round-tripping characteristics
275(what you put in comes out as something equivalent).
276
277For the more enlightened: note that in the following descriptions,
278lowercase I<perl> refers to the Perl interpreter, while uppcercase I<Perl>
279refers to the abstract Perl language itself.
280
281=head2 JSON -> PERL
282
283=over 4
284
285=item object
286
287A JSON object becomes a reference to a hash in Perl. No ordering of object
288keys is preserved.
289
290=item array
291
292A JSON array becomes a reference to an array in Perl.
293
294=item string
295
296A JSON string becomes a string scalar in Perl - Unicode codepoints in JSON
297are represented by the same codepoints in the Perl string, so no manual
298decoding is necessary.
299
300=item number
301
302A JSON number becomes either an integer or numeric (floating point)
303scalar in perl, depending on its range and any fractional parts. On the
304Perl level, there is no difference between those as Perl handles all the
305conversion details, but an integer may take slightly less memory and might
306represent more values exactly than (floating point) numbers.
307
308=item true, false
309
310These JSON atoms become C<0>, C<1>, respectively. Information is lost in
311this process. Future versions might represent those values differently,
312but they will be guarenteed to act like these integers would normally in
313Perl.
314
315=item null
316
317A JSON null atom becomes C<undef> in Perl.
318
319=back
320
321=head2 PERL -> JSON
322
323The mapping from Perl to JSON is slightly more difficult, as Perl is a
324truly typeless language, so we can only guess which JSON type is meant by
325a Perl value.
326
327=over 4
328
329=item hash references
330
331Perl hash references become JSON objects. As there is no inherent ordering
332in hash keys, they will usually be encoded in a pseudo-random order that
333can change between runs of the same program but stays generally the same
334within the single run of a program. JSON::XS can optionally sort the hash
335keys (determined by the I<canonical> flag), so the same datastructure
336will serialise to the same JSON text (given same settings and version of
337JSON::XS), but this incurs a runtime overhead.
338
339=item array references
340
341Perl array references become JSON arrays.
342
343=item blessed objects
344
345Blessed objects are not allowed. JSON::XS currently tries to encode their
346underlying representation (hash- or arrayref), but this behaviour might
347change in future versions.
348
349=item simple scalars
350
351Simple Perl scalars (any scalar that is not a reference) are the most
352difficult objects to encode: JSON::XS will encode undefined scalars as
353JSON null value, scalars that have last been used in a string context
354before encoding as JSON strings and anything else as number value:
355
356 # dump as number
357 to_json [2] # yields [2]
358 to_json [-3.0e17] # yields [-3e+17]
359 my $value = 5; to_json [$value] # yields [5]
360
361 # used as string, so dump as string
362 print $value;
363 to_json [$value] # yields ["5"]
364
365 # undef becomes null
366 to_json [undef] # yields [null]
367
368You can force the type to be a string by stringifying it:
369
370 my $x = 3.1; # some variable containing a number
371 "$x"; # stringified
372 $x .= ""; # another, more awkward way to stringify
373 print $x; # perl does it for you, too, quite often
374
375You can force the type to be a number by numifying it:
376
377 my $x = "3"; # some variable containing a string
378 $x += 0; # numify it, ensuring it will be dumped as a number
379 $x *= 1; # same thing, the choise is yours.
380
381You can not currently output JSON booleans or force the type in other,
382less obscure, ways. Tell me if you need this capability.
383
384=item circular data structures
385
386Those will be encoded until memory or stackspace runs out.
258 387
259=back 388=back
260 389
261=head1 COMPARISON 390=head1 COMPARISON
262 391
386every other module in the decoding case. 515every other module in the decoding case.
387 516
388Last example is an almost 8MB large hash with many large binary values 517Last example is an almost 8MB large hash with many large binary values
389(PNG files), resulting in a lot of escaping: 518(PNG files), resulting in a lot of escaping:
390 519
520=head1 RESOURCE LIMITS
521
522JSON::XS does not impose any limits on the size of JSON texts or Perl
523values they represent - if your machine cna handle it, JSON::XS will
524encode or decode it. Future versions might optionally impose structure
525depth and memory use resource limits.
526
391=head1 BUGS 527=head1 BUGS
392 528
393While the goal of this module is to be correct, that unfortunately does 529While the goal of this module is to be correct, that unfortunately does
394not mean its bug-free, only that I think its design is bug-free. It is 530not mean its bug-free, only that I think its design is bug-free. It is
395still very young and not well-tested. If you keep reporting bugs they will 531still very young and not well-tested. If you keep reporting bugs they will

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines