| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Sledge::Plugin::JSON::XS; | 
| 2 | 2 |  |  | 2 |  | 61533 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 68 |  | 
| 3 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 4 | 2 |  |  | 2 |  | 41 | use 5.00800; | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 2 |  |  |  |  | 79 |  | 
| 5 | 2 |  |  | 2 |  | 2356 | use JSON::XS; | 
|  | 2 |  |  |  |  | 18327 |  | 
|  | 2 |  |  |  |  | 137 |  | 
| 6 | 2 |  |  | 2 |  | 1673 | use Encode (); | 
|  | 2 |  |  |  |  | 16791 |  | 
|  | 2 |  |  |  |  | 1647 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 9 |  |  |  |  |  |  | our $ConformToRFC4627 = 0; | 
| 10 |  |  |  |  |  |  | our $ENCODING = 'utf-8'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub import { | 
| 13 | 2 |  |  | 2 |  | 27 | my $pkg = caller(0); | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 2 |  |  | 2 |  | 24 | no strict 'refs'; ## no critic. | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 2447 |  | 
| 16 | 2 |  |  |  |  | 8 | *{"$pkg\::output_json_xs"} = \&_output_json_xs; | 
|  | 2 |  |  |  |  | 6088 |  | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub _output_json_xs { | 
| 20 | 9 |  |  | 9 |  | 6457 | my ($self, $src) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 9 | 100 | 100 |  |  | 23 | my $content_type = | 
| 23 |  |  |  |  |  |  | ( $self->debug_level && $self->r->param('debug') ) | 
| 24 |  |  |  |  |  |  | ? "text/plain; charset=$ENCODING" | 
| 25 |  |  |  |  |  |  | : _content_type($self, $ENCODING ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 9 |  |  |  |  | 70 | my $json = encode_json($src); | 
| 28 | 9 |  | 100 |  |  | 30 | my $output = _add_callback($self, _validate_callback_param($self, ($self->r->param('callback') || '')), $json ); | 
| 29 | 9 |  |  |  |  | 27 | $self->r->content_type($content_type); | 
| 30 | 9 |  |  |  |  | 154 | $self->set_content_length(length $output); | 
| 31 | 9 |  |  |  |  | 90 | $self->send_http_header; | 
| 32 | 9 |  |  |  |  | 74 | $self->r->print($output); | 
| 33 | 9 |  |  |  |  | 133 | $self->invoke_hook('AFTER_OUTPUT'); | 
| 34 | 9 |  |  |  |  | 31 | $self->finished(1); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub _content_type { | 
| 38 | 8 |  |  | 8 |  | 66 | my ($self, $encoding) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 8 |  |  |  |  | 20 | my $user_agent = $self->r->header_in('User-Agent'); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 8 | 100 | 100 |  |  | 135 | if ( $ConformToRFC4627 ) { | 
|  |  | 100 |  |  |  |  |  | 
| 43 | 1 |  |  |  |  | 4 | return "application/json; charset=$encoding"; | 
| 44 |  |  |  |  |  |  | } elsif (($user_agent || '') =~ /Opera/) { | 
| 45 | 1 |  |  |  |  | 5 | return "application/x-javascript; charset=$encoding"; | 
| 46 |  |  |  |  |  |  | } else { | 
| 47 | 6 |  |  |  |  | 19 | return "application/javascript; charset=$encoding"; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub _add_callback { | 
| 52 | 9 |  |  | 9 |  | 12 | my ($self, $cb, $json) = @_; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 9 | 100 |  |  |  | 36 | if (Encode::is_utf8($cb)) { | 
| 55 | 1 |  |  |  |  | 5 | $cb = Encode::encode('utf8', $cb); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 9 |  |  |  |  | 34 | my $output; | 
| 59 | 9 | 100 |  |  |  | 21 | $output .= "$cb(" if $cb; | 
| 60 | 9 |  |  |  |  | 10 | $output .= $json; | 
| 61 | 9 | 100 |  |  |  | 17 | $output .= ");"   if $cb; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 9 |  |  |  |  | 28 | return $output; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub _validate_callback_param { | 
| 67 | 9 |  |  | 9 |  | 118 | my ($self, $param) = @_; | 
| 68 | 9 | 100 |  |  |  | 40 | return ( $param =~ /^[a-zA-Z0-9\.\_\[\]]+$/ ) ? $param : undef; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | 1; | 
| 72 |  |  |  |  |  |  | __END__ |