4 use warnings FATAL => 'all';
7 our $VERSION = '1.002000';
9 sub _choose_json_module {
10 return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
11 return 'JSON::XS' if $INC{'JSON/XS.pm'};
15 return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };
16 push @err, "Error loading Cpanel::JSON::XS: $@";
18 return 'JSON::XS' if eval { require JSON::XS; 1; };
19 push @err, "Error loading JSON::XS: $@";
21 return 'JSON::PP' if eval { require JSON::PP; 1 };
22 push @err, "Error loading JSON::PP: $@";
24 die join( "\n", "Couldn't load a JSON module:", @err );
29 our $JSON_Class = _choose_json_module();
30 $JSON_Class->import(qw(encode_json decode_json));
33 our @EXPORT = qw(encode_json decode_json JSON);
35 sub JSON () { our $JSON_Class }
39 my %args = @_ == 1 ? %{$_[0]} : @_;
40 my $new = (our $JSON_Class)->new;
41 $new->$_($args{$_}) for keys %args;
49 JSON::MaybeXS - use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP>
55 my $data_structure = decode_json($json_input);
57 my $json_output = encode_json($data_structure);
61 my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 }
65 This module first checks to see if either L<Cpanel::JSON::XS> or
66 L<JSON::XS> is already loaded, in which case it uses that module. Otherwise
67 it tries to load L<Cpanel::JSON::XS>, then L<JSON::XS>, then L<JSON::PP>
68 in order, and either uses the first module it finds or throws an error.
70 It then exports the C<encode_json> and C<decode_json> functions from the
71 loaded module, along with a C<JSON> constant that returns the class name
72 for calling C<new> on.
74 If you're writing fresh code rather than replacing JSON.pm usage, you might
75 want to pass options as constructor args rather than calling mutators, so
76 we provide our own C<new> method that supports that.
80 All of C<encode_json>, C<decode_json> and C<JSON> are exported by default.
82 To import only some symbols, specify them on the C<use> line:
84 use JSON::MaybeXS qw(encode_json decode_json); # functions only
86 use JSON::MaybeXS qw(JSON); # JSON constant only
90 This is the C<encode_json> function provided by the selected implementation
91 module, and takes a perl data structure which is serialised to JSON text.
93 my $json_text = encode_json($data_structure);
97 This is the C<decode_json> function provided by the selected implementation
98 module, and takes a string of JSON text to deserialise to a perl data structure.
100 my $data_structure = decode_json($json_text);
104 The C<JSON> constant returns the selected implementation module's name for
105 use as a class name - so:
107 my $json_obj = JSON->new; # returns a Cpanel::JSON::XS or JSON::PP object
109 and that object can then be used normally:
111 my $data_structure = $json_obj->decode($json_text); # etc.
117 With L<JSON::PP>, L<JSON::XS> and L<Cpanel::JSON::XS> you are required to call
118 mutators to set options, i.e.
120 my $json = $class->new->utf8(1)->pretty(1);
122 Since this is a trifle irritating and noticeably un-perlish, we also offer:
124 my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
126 which works equivalently to the above (and in the usual tradition will accept
127 a hashref instead of a hash, should you so desire).
131 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
135 None yet. Well volunteered? :)
139 Copyright (c) 2013 the C<JSON::MaybeXS> L</AUTHOR> and L</CONTRIBUTORS>
144 This library is free software and may be distributed under the same terms