line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yeb::Plugin::JSON; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
759
|
$Yeb::Plugin::JSON::AUTHORITY = 'cpan:GETTY'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Yeb Plugin for JSON response |
6
|
|
|
|
|
|
|
$Yeb::Plugin::JSON::VERSION = '0.101'; |
7
|
1
|
|
|
1
|
|
902
|
use Moo; |
|
1
|
|
|
|
|
17530
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
3531
|
use JSON::MaybeXS; |
|
1
|
|
|
|
|
598
|
|
|
1
|
|
|
|
|
586
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has app => ( is => 'ro', required => 1, weak_ref => 1 ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my @json_attrs_untouched = qw( |
13
|
|
|
|
|
|
|
ascii latin1 binary pretty indent space_before space_after |
14
|
|
|
|
|
|
|
relaxed canonical filter_json_object filter_json_single_key_object |
15
|
|
|
|
|
|
|
shrink max_depth max_size |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
for (@json_attrs_untouched) { |
19
|
|
|
|
|
|
|
has $_ => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
predicate => 'has_'.$_, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my @json_attrs_enabled = qw( |
26
|
|
|
|
|
|
|
utf8 allow_nonref convert_blessed allow_blessed |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
for (@json_attrs_enabled) { |
30
|
|
|
|
|
|
|
has $_ => ( |
31
|
|
|
|
|
|
|
is => 'ro', |
32
|
|
|
|
|
|
|
default => sub { 1 }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my @json_attrs = ( @json_attrs_untouched, @json_attrs_enabled ); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has json => ( |
39
|
|
|
|
|
|
|
is => 'ro', |
40
|
|
|
|
|
|
|
lazy => 1, |
41
|
|
|
|
|
|
|
default => sub { |
42
|
|
|
|
|
|
|
my ( $self ) = @_; |
43
|
|
|
|
|
|
|
my $json = JSON::MaybeXS->new; |
44
|
|
|
|
|
|
|
for my $attr (@json_attrs) { |
45
|
|
|
|
|
|
|
my $has_attr = 'has_'.$attr; |
46
|
|
|
|
|
|
|
if (!$self->can($has_attr) || $self->$has_attr) { |
47
|
|
|
|
|
|
|
$json->$attr($self->$attr); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
return $json; |
51
|
|
|
|
|
|
|
}, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has json_class => ( |
55
|
|
|
|
|
|
|
is => 'ro', |
56
|
|
|
|
|
|
|
lazy => 1, |
57
|
|
|
|
|
|
|
default => sub { (ref $_[0]->json) }, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has true => ( |
61
|
|
|
|
|
|
|
is => 'ro', |
62
|
|
|
|
|
|
|
lazy => 1, |
63
|
|
|
|
|
|
|
default => sub { $_[0]->json_class->true }, |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
has false => ( |
67
|
|
|
|
|
|
|
is => 'ro', |
68
|
|
|
|
|
|
|
lazy => 1, |
69
|
|
|
|
|
|
|
default => sub { $_[0]->json_class->false }, |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub get_vars { |
73
|
0
|
|
|
0
|
0
|
|
my ( $self, $user_vars ) = @_; |
74
|
0
|
|
|
|
|
|
my %stash = %{$self->app->cc->stash}; |
|
0
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
my %user = defined $user_vars ? %{$user_vars} : (); |
|
0
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return $self->app->merge_hashs( |
77
|
|
|
|
|
|
|
$self->app->cc->export, |
78
|
|
|
|
|
|
|
\%user |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub BUILD { |
83
|
0
|
|
|
0
|
0
|
|
my ( $self ) = @_; |
84
|
|
|
|
|
|
|
$self->app->register_function('json',sub { |
85
|
0
|
|
|
0
|
|
|
my $user_vars = shift; |
86
|
0
|
|
|
|
|
|
my $vars = $self->get_vars($user_vars); |
87
|
0
|
|
|
|
|
|
$self->app->cc->content_type('application/json'); |
88
|
0
|
|
|
|
|
|
$self->app->cc->body($self->json->encode($vars)); |
89
|
0
|
|
|
|
|
|
$self->app->cc->response; |
90
|
0
|
|
|
|
|
|
}); |
91
|
0
|
|
|
0
|
|
|
$self->app->register_function('true',sub { $self->true }); |
|
0
|
|
|
|
|
|
|
92
|
0
|
|
|
0
|
|
|
$self->app->register_function('false',sub { $self->false }); |
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |