Module-Generic
view release on metacpan or search on metacpan
lib/Module/Generic/JSON.pm view on Meta::CPAN
instead of:
my $j = Module::Generic::JSON->new;
$j = $j->utf8->pretty->canonical->relaxed->allow_nonref;
=item * No fatal exception that would kill your process inadvertently.
This is important in a web application where you do not want some module killing your process, but rather you want the exception to be handled gracefully.
Thus, instead of having to do:
local $@;
my $ref = eval{ $j->decode( $payload ) };
if( $@ )
{
# Like returning a 500 or maybe 400 HTTP error
bailout_gracefully( $@ );
}
you can simply do:
my $ref = $j->decode( $payload ) || bailout_gracefully( $j->error );
=item * Upon error, it returns an L<exception object|Module::Generic::Exception>
=item * All methods calls are passed through to L<JSON>, and any exception is caught, and handled properly for you.
=back
For L<class functions|/"CLASS FUNCTIONS"> too, you can execute them safely and catch error, if any, by calling C<< Module::Generic::JSON->error >>, so for example:
decode_json( $some_data ) || die( Module::Generic::JSON->error );
=head1 CONSTRUCTOR
=head2 new
This takes an hash or hash reference of options and returns a new L<Module::Generic::JSON> object. The options must be supported by L<JSON>. On error, sets an L<error object|Module::Generic::Exception> and returns C<undef> in scalar context or an emp...
my $j = Module::Generic::JSON->new( utf8 => 1, pretty => 1 ) ||
die( Module::Generic::JSON->error );
=head1 METHODS
See the documentation for the module L<JSON> for more information, but below are the known methods supported by L<JSON>
=for Pod::Coverage allow_blessed
=for Pod::Coverage allow_nonref
=for Pod::Coverage allow_tags
=for Pod::Coverage allow_unknown
=for Pod::Coverage ascii
=for Pod::Coverage backend
=for Pod::Coverage boolean
=for Pod::Coverage boolean_values
=for Pod::Coverage canonical
=for Pod::Coverage convert_blessed
=for Pod::Coverage decode
Decodes a JSON string and returns the resulting Perl data structure. On error, sets an L<error object|Module::Generic::Exception> and returns C<undef> in scalar context or an empty list in list context:
my $data = $j->decode( '{"a":1}' ) || die( $j->error );
=for Pod::Coverage decode_prefix
=head2 encode
Encodes a Perl data structure into a JSON string. On error, sets an L<error object|Module::Generic::Exception> and returns C<undef> in scalar context or an empty list in list context:
my $json_str = $j->encode( { a => 1 } ) || die( $j->error );
=for Pod::Coverage filter_json_object
=for Pod::Coverage filter_json_single_key_object
=for Pod::Coverage indent
=for Pod::Coverage is_pp
=for Pod::Coverage is_xs
=for Pod::Coverage latin1
=for Pod::Coverage max_depth
=for Pod::Coverage max_size
=for Pod::Coverage pretty
=for Pod::Coverage property
=for Pod::Coverage relaxed
=for Pod::Coverage space_after
=for Pod::Coverage space_before
=for Pod::Coverage utf8
=head1 CLASS FUNCTIONS
=head2 decode_json
Decodes a C<JSON> string and returns the resulting Perl data structure. On error, sets an L<error object|Module::Generic::Exception> and returns C<undef> in scalar context or an empty list in list context:
my $data = decode_json( '{"a":1}' ) || die( Module::Generic::JSON->error );
=head2 encode_json
Encodes a Perl data structure into a C<JSON> string. On error, sets an L<error object|Module::Generic::Exception> and returns C<undef> in scalar context or an empty list in list context:
my $json_str = encode_json( { a => 1 } ) || die( Module::Generic::JSON->error );
( run in 1.062 second using v1.01-cache-2.11-cpan-744e820c463 )