line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Limper::SendJSON; |
2
|
|
|
|
|
|
|
$Limper::SendJSON::VERSION = '0.001'; |
3
|
2
|
|
|
2
|
|
16012
|
use base 'Limper'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1164
|
|
4
|
2
|
|
|
2
|
|
65852
|
use 5.10.0; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
59
|
|
5
|
2
|
|
|
2
|
|
17
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
45
|
|
6
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
76
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Limper; |
9
|
|
|
|
|
|
|
$Limper::VERSION = '0.001'; |
10
|
2
|
|
|
2
|
|
882
|
use JSON::MaybeXS; |
|
2
|
|
|
|
|
10582
|
|
|
2
|
|
|
|
|
127
|
|
11
|
2
|
|
|
2
|
|
1772
|
use Try::Tiny; |
|
2
|
|
|
|
|
2219
|
|
|
2
|
|
|
|
|
421
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
push @Limper::EXPORT, qw/send_json/; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub send_json { |
16
|
0
|
|
|
0
|
0
|
|
my ($data, @options) = @_; |
17
|
0
|
|
|
|
|
|
my @headers = headers; |
18
|
0
|
0
|
|
|
|
|
headers 'Content-Type' => 'application/json', headers unless grep { $_ eq 'Content-Type' } keys %{{&headers}}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
try { |
20
|
0
|
|
|
0
|
|
|
JSON::MaybeXS->new(@options)->encode($data); |
21
|
|
|
|
|
|
|
} catch { |
22
|
0
|
|
|
0
|
|
|
warning $_; |
23
|
0
|
|
|
|
|
|
headers @headers; |
24
|
0
|
|
|
|
|
|
status 500; |
25
|
0
|
|
|
|
|
|
'Internal Server Error'; |
26
|
0
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
1; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=for Pod::Coverage |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 NAME |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Limper::SendJSON - adds a send_json function to Limper |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 VERSION |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
version 0.001 |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 SYNOPSIS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
use Limper::SendJSON; |
44
|
|
|
|
|
|
|
use Limper; # this must come after all extensions |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# some other routes |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
get '/json' = sub { |
49
|
|
|
|
|
|
|
send_json { foo => 'bar' }; |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
get '/json-pretty' = sub { |
53
|
|
|
|
|
|
|
send_json { foo => 'bar' }, pretty => 1; |
54
|
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
limp; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 DESCRIPTION |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
C extends C to easily return JSON, with the proper Content-Type header. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 EXPORTS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
The following are all additionally exported by default: |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
send_json |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 FUNCTIONS |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 send_json |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Sends the C or C given as JSON. If B is not |
73
|
|
|
|
|
|
|
already set, it will be set to B. Returns B<500> if the |
74
|
|
|
|
|
|
|
scalar cannot be encoded by L. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Copyright (C) 2014 by Ashley Willis Eashley+perl@gitable.orgE |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
81
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.12.4 or, |
82
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 SEE ALSO |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
L |
87
|
|
|
|
|
|
|
L |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |