4 use warnings FATAL => 'all';
7 our $VERSION = '1.001000';
14 if (eval { require Cpanel::JSON::XS; 1; }) {
15 $JSON_Class = 'Cpanel::JSON::XS';
17 push @err, "Error loading Cpanel::JSON::XS: $@";
18 if (eval { require JSON::PP; 1; }) {
19 $JSON_Class = 'JSON::PP';
21 push @err, "Error loading JSON::PP: $@";
24 unless ($JSON_Class) {
25 die join("\n", "Couldn't load a JSON module:", @err);
27 $JSON_Class->import(qw(encode_json decode_json));
30 our @EXPORT = qw(encode_json decode_json JSON);
32 sub JSON () { our $JSON_Class }
36 my %args = @_ == 1 ? %{$_[0]} : @_;
37 my $new = (our $JSON_Class)->new;
38 $new->$_($args{$_}) for keys %args;
46 JSON::MaybeXS - use L<Cpanel::JSON::XS> with a fallback to L<JSON::PP>
52 my $data_structure = decode_json($json_input);
54 my $json_output = encode_json($data_structure);
58 my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 }
62 This module tries to load L<Cpanel::JSON::XS>, and if that fails instead
63 tries to load L<JSON::PP>. If neither is available, an exception will be
66 It then exports the C<encode_json> and C<decode_json> functions from the
67 loaded module, along with a C<JSON> constant that returns the class name
68 for calling C<new> on.
70 If you're writing fresh code rather than replacing JSON.pm usage, you might
71 want to pass options as constructor args rather than calling mutators, so
72 we provide our own C<new> method that supports that.
76 All of C<encode_json>, C<decode_json> and C<JSON> are exported by default.
78 To import only some symbols, specify them on the C<use> line:
80 use JSON::MaybeXS qw(encode_json decode_json); # functions only
82 use JSON::MaybeXS qw(JSON); # JSON constant only
86 This is the C<encode_json> function provided by the selected implementation
87 module, and takes a perl data stucture which is serialised to JSON text.
89 my $json_text = encode_json($data_structure);
93 This is the C<decode_json> function provided by the selected implementation
94 module, and takes a string of JSON text to deserialise to a perl data structure.
96 my $data_structure = decode_json($json_text);
100 The C<JSON> constant returns the selected implementation module's name for
101 use as a class name - so:
103 my $json_obj = JSON->new; # returns a Cpanel::JSON::XS or JSON::PP object
105 and that object can then be used normally:
107 my $data_structure = $json_obj->decode($json_text); # etc.
113 With L<JSON::PP> and L<Cpanel::JSON::XS> you are required to call mutators
116 my $json = $class->new->utf8(1)->pretty(1);
118 Since this is a trifle irritating and noticeably un-perlish, we also offer:
120 my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
122 which works equivalently to the above (and in the usual tradition will accept
123 a hashref instead of a hash, should you so desire).
127 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
131 None yet. Well volunteered? :)
135 Copyright (c) 2013 the C<JSON::MaybeXS> L</AUTHOR> and L</CONTRIBUTORS>
140 This library is free software and may be distributed under the same terms