line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Limper::SendJSON; |
2
|
|
|
|
|
|
|
$Limper::SendJSON::VERSION = '0.003'; |
3
|
2
|
|
|
2
|
|
15659
|
use base 'Limper'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
994
|
|
4
|
2
|
|
|
2
|
|
40823
|
use 5.10.0; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
5
|
2
|
|
|
2
|
|
16
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
6
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
62
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package # newline because Dist::Zilla::Plugin::PkgVersion and PAUSE indexer |
9
|
|
|
|
|
|
|
Limper; |
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
924
|
use JSON::MaybeXS; |
|
2
|
|
|
|
|
10509
|
|
|
2
|
|
|
|
|
107
|
|
12
|
2
|
|
|
2
|
|
1625
|
use Try::Tiny; |
|
2
|
|
|
|
|
2209
|
|
|
2
|
|
|
|
|
339
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
push @Limper::EXPORT, qw/send_json/; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub send_json { |
17
|
0
|
|
|
0
|
0
|
|
my ($data, @options) = @_; |
18
|
0
|
|
0
|
|
|
|
response->{headers}{'Content-Type'} //= 'application/json'; |
19
|
|
|
|
|
|
|
try { |
20
|
0
|
|
|
0
|
|
|
JSON::MaybeXS->new(@options)->encode($data); |
21
|
|
|
|
|
|
|
} catch { |
22
|
0
|
|
|
0
|
|
|
warning $_; |
23
|
0
|
|
|
|
|
|
headers 'Content-Type' => 'text/plain'; |
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.003 |
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
|
|
|
|
|
|
|
B extends L 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 B or B 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
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
L |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
L |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |