Release commit for 1.004001
[p5sagit/JSON-MaybeXS.git] / lib / JSON / MaybeXS.pm
CommitLineData
3be1e192 1package JSON::MaybeXS;
2
3use strict;
4use warnings FATAL => 'all';
5use base qw(Exporter);
6
e59cd500 7our $VERSION = '1.004001';
25af6828 8$VERSION =~ tr/_//d;
44459f01 9
16205c4a 10sub _choose_json_module {
11 return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
34e2d247 12 return 'JSON::XS' if $INC{'JSON/XS.pm'} && eval { JSON::XS->VERSION(3.0); 1 };
3be1e192 13
16205c4a 14 my @err;
3be1e192 15
16205c4a 16 return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };
3be1e192 17 push @err, "Error loading Cpanel::JSON::XS: $@";
16205c4a 18
48d383c7 19 return 'JSON::XS' if eval { require JSON::XS; JSON::XS->VERSION(3.0); 1; };
16205c4a 20 push @err, "Error loading JSON::XS: $@";
21
22 return 'JSON::PP' if eval { require JSON::PP; 1 };
23 push @err, "Error loading JSON::PP: $@";
24
25 die join( "\n", "Couldn't load a JSON module:", @err );
26
27}
28
29BEGIN {
30 our $JSON_Class = _choose_json_module();
31 $JSON_Class->import(qw(encode_json decode_json));
048d1726 32 no strict 'refs';
33 *$_ = $JSON_Class->can($_)
34 for qw(true false);
3be1e192 35}
36
37our @EXPORT = qw(encode_json decode_json JSON);
c397f194 38my @EXPORT_ALL = qw(is_bool);
ebf1e433 39our @EXPORT_OK = qw(is_bool to_json from_json);
c397f194 40our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_ALL ],
41 legacy => [ @EXPORT, @EXPORT_OK ],
42 );
3be1e192 43
44sub JSON () { our $JSON_Class }
45
939f9a29 46sub new {
47 shift;
48 my %args = @_ == 1 ? %{$_[0]} : @_;
49 my $new = (our $JSON_Class)->new;
50 $new->$_($args{$_}) for keys %args;
51 return $new;
52}
53
8e911b7e 54use Scalar::Util ();
1ca3b561 55
56sub is_bool {
57 die 'is_bool is not a method' if $_[1];
58
8e911b7e 59 Scalar::Util::blessed($_[0])
60 and ($_[0]->isa('JSON::XS::Boolean')
4879506d 61 or $_[0]->isa('Cpanel::JSON::XS::Boolean')
8e911b7e 62 or $_[0]->isa('JSON::PP::Boolean'));
1ca3b561 63}
64
c397f194 65# (mostly) CopyPasta from JSON.pm version 2.90
66use Carp ();
ebf1e433 67
68sub from_json ($@) {
c397f194 69 if ( ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/ ) {
ebf1e433 70 Carp::croak "from_json should not be called as a method.";
71 }
c397f194 72 my $json = JSON()->new;
ebf1e433 73
74 if (@_ == 2 and ref $_[1] eq 'HASH') {
75 my $opt = $_[1];
76 for my $method (keys %$opt) {
77 $json->$method( $opt->{$method} );
78 }
79 }
80
81 return $json->decode( $_[0] );
82}
83
84sub to_json ($@) {
85 if (
c397f194 86 ref($_[0]) =~ /^JSON/
87 or (@_ > 2 and $_[0] =~ /^JSON/)
ebf1e433 88 ) {
89 Carp::croak "to_json should not be called as a method.";
90 }
c397f194 91 my $json = JSON()->new;
ebf1e433 92
93 if (@_ == 2 and ref $_[1] eq 'HASH') {
94 my $opt = $_[1];
95 for my $method (keys %$opt) {
96 $json->$method( $opt->{$method} );
97 }
98 }
99
100 $json->encode($_[0]);
101}
102
3be1e192 1031;
44459f01 104
105=head1 NAME
106
c95d7d54 107JSON::MaybeXS - Use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP>
44459f01 108
109=head1 SYNOPSIS
110
111 use JSON::MaybeXS;
112
113 my $data_structure = decode_json($json_input);
114
115 my $json_output = encode_json($data_structure);
116
20f884e2 117 my $json = JSON()->new;
44459f01 118
939f9a29 119 my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 }
120
44459f01 121=head1 DESCRIPTION
122
16205c4a 123This module first checks to see if either L<Cpanel::JSON::XS> or
124L<JSON::XS> is already loaded, in which case it uses that module. Otherwise
125it tries to load L<Cpanel::JSON::XS>, then L<JSON::XS>, then L<JSON::PP>
126in order, and either uses the first module it finds or throws an error.
44459f01 127
128It then exports the C<encode_json> and C<decode_json> functions from the
129loaded module, along with a C<JSON> constant that returns the class name
130for calling C<new> on.
131
5c581075 132If you're writing fresh code rather than replacing L<JSON.pm|JSON> usage, you might
939f9a29 133want to pass options as constructor args rather than calling mutators, so
134we provide our own C<new> method that supports that.
135
44459f01 136=head1 EXPORTS
137
1ca3b561 138C<encode_json>, C<decode_json> and C<JSON> are exported by default; C<is_bool>
139is exported on request.
44459f01 140
141To import only some symbols, specify them on the C<use> line:
142
1ca3b561 143 use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only
44459f01 144
145 use JSON::MaybeXS qw(JSON); # JSON constant only
146
c397f194 147To import all available sensible symbols (C<encode_json>, C<decode_json>, and
148C<is_bool>), use C<:all>:
bf8dbbe1 149
150 use JSON::MaybeXS ':all';
151
c397f194 152To import all symbols including those needed by legacy apps that use L<JSON::PP>:
ebf1e433 153
154 use JSON::MaybeXS ':legacy';
155
c397f194 156This imports the C<to_json> and C<from_json> symbols as well as everything in
157C<:all>. NOTE: This is to support legacy code that makes extensive
158use of C<to_json> and C<from_json> which you are not yet in a position to
ebf1e433 159refactor. DO NOT use this import tag in new code, in order to avoid
78464170 160the crawling horrors of getting UTF-8 support subtly wrong. See the
ebf1e433 161documentation for L<JSON> for further details.
162
44459f01 163=head2 encode_json
164
165This is the C<encode_json> function provided by the selected implementation
0629a8af 166module, and takes a perl data structure which is serialised to JSON text.
44459f01 167
168 my $json_text = encode_json($data_structure);
169
170=head2 decode_json
171
172This is the C<decode_json> function provided by the selected implementation
173module, and takes a string of JSON text to deserialise to a perl data structure.
174
175 my $data_structure = decode_json($json_text);
176
ebf1e433 177=head2 to_json, from_json
178
c397f194 179See L<JSON> for details. These are included to support legacy code
ebf1e433 180B<only>.
181
44459f01 182=head2 JSON
183
184The C<JSON> constant returns the selected implementation module's name for
185use as a class name - so:
186
20f884e2 187 my $json_obj = JSON()->new; # returns a Cpanel::JSON::XS or JSON::PP object
44459f01 188
189and that object can then be used normally:
190
191 my $data_structure = $json_obj->decode($json_text); # etc.
192
20f884e2 193The use of parentheses here is optional, and only used as a hint to the reader
194that this use of C<JSON> is a I<subroutine> call, I<not> a class name.
195
1ca3b561 196=head2 is_bool
197
198 $is_boolean = is_bool($scalar)
199
200Returns true if the passed scalar represents either C<true> or
201C<false>, two constants that act like C<1> and C<0>, respectively
202and are used to represent JSON C<true> and C<false> values in Perl.
203
204Since this is a bare sub in the various backend classes, it cannot be called as
205a class method like the other interfaces; it must be called as a function, with
206no invocant. It supports the representation used in all JSON backends.
207
28a8a7a9 208Available since version 1.002004.
209
939f9a29 210=head1 CONSTRUCTOR
211
212=head2 new
213
16205c4a 214With L<JSON::PP>, L<JSON::XS> and L<Cpanel::JSON::XS> you are required to call
3c38e105 215mutators to set options, such as:
939f9a29 216
217 my $json = $class->new->utf8(1)->pretty(1);
218
219Since this is a trifle irritating and noticeably un-perlish, we also offer:
220
221 my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
222
223which works equivalently to the above (and in the usual tradition will accept
224a hashref instead of a hash, should you so desire).
225
590077bd 226The resulting object is blessed into the underlying backend, which offers (at
227least) the methods C<encode> and C<decode>.
228
32af371c 229=head1 BOOLEANS
230
231To include JSON-aware booleans (C<true>, C<false>) in your data, just do:
232
233 use JSON::MaybeXS;
20f884e2 234 my $true = JSON()->true;
235 my $false = JSON()->false;
32af371c 236
048d1726 237The booleans are also available as subs or methods on JSON::MaybeXS.
238
239 use JSON::MaybeXS ();
240 my $true = JSON::MaybeXS::true;
241 my $true = JSON::MaybeXS->true;
242 my $false = JSON::MaybeXS::false;
243 my $false = JSON::MaybeXS->false;
244
61f129a5 245=head1 CONVERTING FROM JSON::Any
246
247L<JSON::Any> used to be the favoured compatibility layer above the various
248JSON backends, but over time has grown a lot of extra code to deal with legacy
249backends (e.g. L<JSON::Syck>) that are no longer needed. This is a rough guide of translating such code:
250
251Change code from:
252
253 use JSON::Any;
254 my $json = JSON::Any->new->objToJson($data); # or to_json($data), or Dump($data)
255
256to:
257
258 use JSON::MaybeXS;
259 my $json = encode_json($data);
260
261
262Change code from:
263
264 use JSON::Any;
265 my $data = JSON::Any->new->jsonToObj($json); # or from_json($json), or Load($json)
266
267to:
268
269 use JSON::MaybeXS;
270 my $json = decode_json($data);
271
06551ef5 272=head1 CAVEATS
273
274The C<new()> method in this module is technically a factory, not a
275constructor, because the objects it returns will I<NOT> be blessed into the
276C<JSON::MaybeXS> class.
277
278If you are using an object returned by this module as a Moo(se) attribute,
279this type constraint code:
280
281 is 'json' => ( isa => 'JSON::MaybeXS' );
282
283will I<NOT> do what you expect. Instead, either rely on the C<JSON> class
284constant described above, as so:
285
286 is 'json' => ( isa => JSON::MaybeXS::JSON() );
287
288Alternatively, you can use duck typing:
289
5bbc5b59 290 use Moose::Util::TypeConstraints 'duck_type';
06551ef5 291 is 'json' => ( isa => Object , duck_type([qw/ encode decode /]));
292
50a44e81 293=head1 INSTALLATION
294
295At installation time, F<Makefile.PL> will attempt to determine if you have a
296working compiler available, and therefore whether you are able to run XS code.
297If so, L<Cpanel::JSON::XS> will be added to the prerequisite list, unless
298L<JSON::XS> is already installed at a high enough version. L<JSON::XS> may
299also be upgraded to fix any incompatibility issues.
300
301Because running XS code is not mandatory and L<JSON::PP> (which is in perl
302core) is used as a fallback backend, this module is safe to be used in a suite
303of code that is fatpacked or installed into a restricted-resource environment.
304
305You can also prevent any XS dependencies from being installed by setting
306C<PUREPERL_ONLY=1> in F<Makefile.PL> options (or in the C<PERL_MM_OPT>
307environment variable), or using the C<--pp> or C<--pureperl> flags with the
308L<cpanminus client|cpanm>.
309
44459f01 310=head1 AUTHOR
311
312mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
313
314=head1 CONTRIBUTORS
315
0c45c84c 316=over 4
317
318=item * Clinton Gormley <drtech@cpan.org>
319
320=item * Karen Etheridge <ether@cpan.org>
321
c397f194 322=item * Kieren Diment <diment@gmail.com>
323
0c45c84c 324=back
44459f01 325
326=head1 COPYRIGHT
327
328Copyright (c) 2013 the C<JSON::MaybeXS> L</AUTHOR> and L</CONTRIBUTORS>
329as listed above.
330
331=head1 LICENSE
332
333This library is free software and may be distributed under the same terms
334as perl itself.
335
336=cut