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