| 1 | package LWP::JSON::Tiny; | ||||||||||||||||||||
| 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 HTTP::Request::JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use HTTP::Response::JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 6 | ▶ | use LWP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 6 | ▶ | use LWP::UserAgent::JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 6 | our $VERSION = '0.012'; | |||||||||||||||||||
| 14 | 6 | $VERSION = eval $VERSION; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | LWP::JSON::Tiny - use JSON natively with LWP objects | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | This is version 0.012. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | my $user_agent = LWP::UserAgent::JSON->new; | ||||||||||||||||||||
| 27 | my $request = HTTP::Request::JSON->new(POST => "$url_prefix/upload_dance"); | ||||||||||||||||||||
| 28 | $request->json_content({ contents => [qw(badger mushroom snake)] }); | ||||||||||||||||||||
| 29 | my $response = $user_agent->request($request); | ||||||||||||||||||||
| 30 | if (my $upload_id = $response->json_content->{upload}{id}) { | ||||||||||||||||||||
| 31 | print "Uploaded Weebl rip-off: $upload_id\n"; | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | my $other_response = $some_other_object->do_stuff(...); | ||||||||||||||||||||
| 35 | if (LWP::UserAgent::JSON->rebless_maybe($other_response)) { | ||||||||||||||||||||
| 36 | do_something($other_response->json_content); | ||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | A lot of RESTful API integration involves pointless busy work with setting | ||||||||||||||||||||
| 42 | accept and content-type headers, remembering how Unicode is supposed to work | ||||||||||||||||||||
| 43 | and so on. This is a very simple wrapper around HTTP::Request and | ||||||||||||||||||||
| 44 | HTTP::Response that handles all of that for you. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | There are four classes in this distribution: | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =over | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =item LWP::JSON::Tiny | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Pulls in the other classes, and implements a L</"json_object"> method which | ||||||||||||||||||||
| 53 | returns a JSON object, suitable for parsing and emitting JSON. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =item HTTP::Request::JSON | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | A subclass of HTTP::Request. It automatically sets the Accept header to | ||||||||||||||||||||
| 58 | C<application/json>, and implements a | ||||||||||||||||||||
| 59 | L<json_content|HTTP::Request::JSON/json_content> method | ||||||||||||||||||||
| 60 | which takes a JSONable data structure and sets the content-type. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =item HTTP::Response::JSON | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | A subclass of HTTP::Response. It implements a | ||||||||||||||||||||
| 65 | L<json_content|HTTP::Response::JSON/json_content> method which | ||||||||||||||||||||
| 66 | decodes the JSON contents into a Perl data structure. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =item LWP::UserAgent::JSON | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | A subclass of LWP::UserAgent. It does only one thing: is a response has | ||||||||||||||||||||
| 71 | content-type JSON, it reblesses it into a HTTP::Response::JSON object. | ||||||||||||||||||||
| 72 | It exposes this method L<rebless_maybe|LWP::UserAgent::JSON/rebless_maybe> | ||||||||||||||||||||
| 73 | for convenience, if you ever get an HTTP::Response object back from some | ||||||||||||||||||||
| 74 | other class. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | As befits a ::Tiny distribution, sensible defaults are applied. If you really | ||||||||||||||||||||
| 79 | need to tweak this stuff (e.g. you really care about the very slight | ||||||||||||||||||||
| 80 | performance impact of sorting all hash keys), look at the individual | ||||||||||||||||||||
| 81 | modules' documentation for how you can subclass behaviour. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head2 Class methods | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head3 json_object | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Out: $json | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | Returns a JSON object, as per JSON::MaybeXS->new. Cached across multiple | ||||||||||||||||||||
| 90 | calls for speed. | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | Note that the JSON object has the utf8 option disabled. I<This is deliberate>. | ||||||||||||||||||||
| 93 | The documentation for JSON::XS is very clear that the utf8 option means both | ||||||||||||||||||||
| 94 | that it should spit out JSON in UTF8, and that it should expect strings | ||||||||||||||||||||
| 95 | passed to it to be in UTF8 encoding. This latter part is daft, and violates | ||||||||||||||||||||
| 96 | the best practice that character encoding should be dealt with at the | ||||||||||||||||||||
| 97 | outermost layer. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | { | ||||||||||||||||||||
| 102 | 6 | my %json_by_class; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub json_object { | ||||||||||||||||||||
| 105 | 33 | ▶ | my ($invocant) = @_; | ||||||||||||||||||
|
Subroutine
json_object: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 33 | ▶ | my $class = ref($invocant) || $invocant; | ||||||||||||||||||
|
Condition33%1/3
ref $invocant || $invocant
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant
$invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
| |||||||||||||||||||||
| 108 | 33 | ▶ | my $json ||= $json_by_class{$class}; | ||||||||||||||||||
|
Condition66%2/3
my $json ||= $json_by_class{$class}
MC/DC50%1/2
my $json ||= $json_by_class{$class}
my $json
$json_by_class{$class}
Truth table66%2/3
A ||= B
A= my $jsonB= $json_by_class{$class}
| |||||||||||||||||||||
| 109 | 33 | ▶ | return $json if defined $json; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 6 | $json = JSON::MaybeXS->new($class->default_json_arguments); | |||||||||||||||||||
| 111 | 6 | return $json_by_class{$class} = $json; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head3 default_json_arguments | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Out: %default_json_arguments | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | The default arguments to pass to JSON::MaybeXS->new. This is what you'd | ||||||||||||||||||||
| 120 | subclass if you wanted to change how LWP::JSON::Tiny encoded JSON. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 6 | sub default_json_arguments { | |||||||||||||||||||
| 125 | return ( | ||||||||||||||||||||
| 126 | 6 | ▶ | utf8 => 0, | ||||||||||||||||||
|
Subroutine
default_json_arguments: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 127 | allow_nonref => 1, | ||||||||||||||||||||
| 128 | allow_unknown => 0, | ||||||||||||||||||||
| 129 | allow_blessed => 0, | ||||||||||||||||||||
| 130 | convert_blessed => 0, | ||||||||||||||||||||
| 131 | canonical => 1, | ||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head1 SEE ALSO | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | L<JSON::API> handles authentication and common URL prefixes, but (a) | ||||||||||||||||||||
| 138 | doesn't support PATCH routes, and (b) makes you use a wrapper object | ||||||||||||||||||||
| 139 | rather than LWP directly. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | L<WWW::JSON> handles authentication (including favours of OAuth), common URL | ||||||||||||||||||||
| 142 | prefixes, response data structure transformations, but has the same | ||||||||||||||||||||
| 143 | limitations as JSON::API, as well as being potentially unwieldy. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | L<LWP::Simple::REST> decodes JSON but makes you use a wrapper object, and | ||||||||||||||||||||
| 146 | looks like a half-hearted attempt that never went anywhere. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head1 AUTHOR | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Sam Kington <skington@cpan.org> | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | The source code for this module is hosted on GitHub | ||||||||||||||||||||
| 153 | L<https://github.com/skington/lwp-json-tiny> - this is probably the | ||||||||||||||||||||
| 154 | best place to look for suggestions and feedback. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head1 COPYRIGHT | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Copyright (c) 2015 Sam Kington. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head1 LICENSE | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | This library is free software and may be distributed under the same terms as | ||||||||||||||||||||
| 163 | perl itself. | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 6 | 1; | |||||||||||||||||||