198
|
1 package IMPL::Web::Handler::JSONView;
|
|
2 use strict;
|
|
3 use JSON;
|
|
4
|
|
5 use IMPL::declare {
|
233
|
6 require => {
|
|
7 HttpResponse => 'IMPL::Web::HttpResponse'
|
|
8 },
|
|
9 base => [
|
198
|
10 'IMPL::Object' => undef,
|
|
11 'IMPL::Object::Serializable' => undef,
|
|
12 'IMPL::Object::Autofill' => '@_'
|
233
|
13 ]
|
198
|
14 };
|
|
15
|
206
|
16 sub contentType {
|
|
17 'application/json'
|
|
18 }
|
|
19
|
198
|
20 sub Invoke {
|
|
21 my ($this,$action,$next) = @_;
|
|
22
|
206
|
23 my $result = $next ? $next->($action) : undef;
|
198
|
24 $result = [$result] unless ref $result;
|
|
25
|
206
|
26 $action->response->contentType($this->contentType);
|
198
|
27
|
233
|
28 return HttpResponse->new({
|
|
29 type => $this->contentType,
|
|
30 charset => 'utf-8',
|
|
31 body => JSON->new->utf8->pretty->encode($result)
|
|
32 });
|
198
|
33 }
|
|
34
|
|
35 1;
|
|
36
|
|
37 __END__
|
|
38
|
|
39 =pod
|
|
40
|
|
41 =head1
|
|
42
|
|
43 =cut |