| 1 | package HTTP::Response::JSON; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 6 | ▶ | use LWP::JSON::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | ▶ | use parent 'HTTP::Message::JSON', 'HTTP::Response'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 6 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 6 | our $VERSION = $LWP::JSON::Tiny::VERSION; | |||||||||
| 13 | |||||||||||
| 14 | =head1 NAME | ||||||||||
| 15 | |||||||||||
| 16 | HTTP::Response::JSON - a subclass of HTTP::Response that understands JSON | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | if ($response->isa('HTTP::Response::JSON')) { | ||||||||||
| 21 | Your::Own::Code::do_something($response->json_content); | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||
| 25 | |||||||||||
| 26 | This is a simple subclass of HTTP::Response that implements a method | ||||||||||
| 27 | L</json_content> which returns the JSON-decoded contents of the response. | ||||||||||
| 28 | |||||||||||
| 29 | =head2 json_content | ||||||||||
| 30 | |||||||||||
| 31 | Out: $perl_data | ||||||||||
| 32 | |||||||||||
| 33 | Returns the Perl data structure corresponding to the contents of this | ||||||||||
| 34 | response. | ||||||||||
| 35 | |||||||||||
| 36 | Will throw an exception if the contents look like JSON but cannot be converted | ||||||||||
| 37 | to JSON. Will return undef if the contents don't look like JSON. | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | sub json_content { | ||||||||||
| 42 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
json_content: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 43 | |||||||||||
| 44 | 5 | ▶ | return if $self->content_type !~ m{^ application/json }x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 4 | ▶ | if ($self->decoded_content !~ /\S/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | return; | |||||||||
| 47 | } else { | ||||||||||
| 48 | 4 | my $json = LWP::JSON::Tiny->json_object; | |||||||||
| 49 | 4 | return $json->decode($self->decoded_content); | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | =head1 AUTHOR | ||||||||||
| 54 | |||||||||||
| 55 | Sam Kington <skington@cpan.org> | ||||||||||
| 56 | |||||||||||
| 57 | The source code for this module is hosted on GitHub | ||||||||||
| 58 | L<https://github.com/skington/lwp-json-tiny> - this is probably the | ||||||||||
| 59 | best place to look for suggestions and feedback. | ||||||||||
| 60 | |||||||||||
| 61 | =head1 COPYRIGHT | ||||||||||
| 62 | |||||||||||
| 63 | Copyright (c) 2015 Sam Kington. | ||||||||||
| 64 | |||||||||||
| 65 | =head1 LICENSE | ||||||||||
| 66 | |||||||||||
| 67 | This library is free software and may be distributed under the same terms as | ||||||||||
| 68 | perl itself. | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | 6 | 1; | |||||||||