line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Egg::View::JSON; |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt> |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# $Id: JSON.pm 189 2007-08-08 01:43:47Z lushe $ |
6
|
|
|
|
|
|
|
# |
7
|
2
|
|
|
2
|
|
11039
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
73
|
|
8
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
70
|
|
9
|
2
|
|
|
2
|
|
11
|
use Carp qw/croak/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
136
|
|
10
|
2
|
|
|
2
|
|
10
|
use base qw/Egg::View/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1229
|
|
11
|
2
|
|
|
2
|
|
25
|
use JSON; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
19
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Egg::Release::JSON - JSON for Egg::View. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Configuration. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
VIEW => [ |
24
|
|
|
|
|
|
|
..... |
25
|
|
|
|
|
|
|
[ JSON => { |
26
|
|
|
|
|
|
|
content_type => 'text/javascript+json', |
27
|
|
|
|
|
|
|
charset => 'UTF-8', |
28
|
|
|
|
|
|
|
option => { pretty => 1, indent => 2 }, |
29
|
|
|
|
|
|
|
} ], |
30
|
|
|
|
|
|
|
], |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Example code. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$e->default_view('JSON')->obj({ |
35
|
|
|
|
|
|
|
hoge=> 'boo', |
36
|
|
|
|
|
|
|
baaa=> 'wii', |
37
|
|
|
|
|
|
|
}); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
* It leaves it to the operation of Egg now. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
It is VIEW to output JSON. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
JSON is output by the 'objToJson' function of L<JSON > module. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
see L<JSON>. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 CONFIGURATION |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Please add JSON to the setting of VIEW. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
VIEW => [ |
54
|
|
|
|
|
|
|
...... |
55
|
|
|
|
|
|
|
... |
56
|
|
|
|
|
|
|
[ JSON => { |
57
|
|
|
|
|
|
|
....... |
58
|
|
|
|
|
|
|
... |
59
|
|
|
|
|
|
|
} ], |
60
|
|
|
|
|
|
|
], |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 content_type |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Contents type when JSON is output. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Default is 'text/javascript+json'. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 charset |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Character set when JSON is output. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Default is 'UTF-8'. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 option |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Option to pass to objToJson function of L<JSON> module. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
option=> { pretty => 1, indent => 2 }, |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
see L<JSON>; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
* When following 'x_json' is made effective, the inconvenience is generated |
83
|
|
|
|
|
|
|
because the JSON code is molded when option is set. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 x_json |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
When an effective value is set, it comes always to output the JSON data to |
88
|
|
|
|
|
|
|
'X-JSON' of the response header. |
89
|
|
|
|
|
|
|
When the JSON data is treated with Prototype.js, this is convenient. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
* This value invalidates and is good at the thing individually made effective |
92
|
|
|
|
|
|
|
by the 'x_json' method. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Default is 0. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 METHODS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 obj ( {[HASH or ARRAY or etc.]} ) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
The data to give it to the objToJson function of L<JSON> module is maintained. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
It is necessary to define some values like being undefined the first stage. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
The value set to call it without giving anything is returned. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# ARRAY is defined. |
107
|
|
|
|
|
|
|
my $array= $e->view('JSON')->obj([]); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# HASH is defined. |
110
|
|
|
|
|
|
|
my $hash= $e->view('JSON')->obj({}); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 x_json ( [BOOL] ) |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Response header (X-JSON) contains the JSON code. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
* Please refer to 'x_json' of CONFIGURATION. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$e->view('JSON')->x_json(1); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Output response header. |
121
|
|
|
|
|
|
|
Content-Type: text/javascript+json; charset=utf-8 |
122
|
|
|
|
|
|
|
X-JSON: ({"hoge":11111,"boo":22222}) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
When 'x_json' is effective, the contents header comes to be output it always |
125
|
|
|
|
|
|
|
followed. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
<div>JSON sees in the response header.</div> |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/obj x_json/); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub _setup { |
133
|
0
|
|
|
0
|
|
|
my($class, $e, $conf)= @_; |
134
|
0
|
|
0
|
|
|
|
$conf->{content_type} ||= 'text/javascript+json'; |
135
|
0
|
|
0
|
|
|
|
$conf->{charset} ||= 'utf-8'; |
136
|
0
|
|
0
|
|
|
|
$conf->{option} ||= {}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 render |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
The result of the objToJson function of L<JSON> module is returned. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $view= $e->view('JSON'); |
144
|
|
|
|
|
|
|
my $obj= $view->obj({}); |
145
|
|
|
|
|
|
|
$obj->{hoge}= '11111'; |
146
|
|
|
|
|
|
|
$obj->{booo}= '22222'; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $json_js= $view->render($obj); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
sub render { |
152
|
0
|
|
|
0
|
|
|
my $view= shift; |
153
|
0
|
|
0
|
|
|
|
my $obj = shift || return(undef); |
154
|
0
|
|
|
|
|
|
objToJson($obj, $view->config->{option}); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 output |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
It is not necessary to call it from the project code because it is called by |
160
|
|
|
|
|
|
|
the operation of Egg usually. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
sub output { |
164
|
0
|
|
|
0
|
|
|
my $view= shift; |
165
|
0
|
|
0
|
|
|
|
my $obj= shift || $view->obj || croak q{ I want json data. }; |
166
|
0
|
|
|
|
|
|
my($e, $c)= ($view->e, $view->config); |
167
|
0
|
|
|
|
|
|
$e->res->content_type |
168
|
|
|
|
|
|
|
("$c->{content_type}; charset=$c->{charset}"); |
169
|
0
|
|
|
|
|
|
my $json= $view->render($obj, @_); |
170
|
0
|
0
|
0
|
|
|
|
if ($view->x_json or $c->{x_json}) { |
171
|
0
|
|
|
|
|
|
$e->res->headers->header('X-JSON'=> "($json)"); |
172
|
0
|
|
|
|
|
|
$json= "<div>JSON sees in the response header.</div>"; |
173
|
|
|
|
|
|
|
} |
174
|
0
|
|
|
|
|
|
$e->response->body(\$json); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 SEE ALSO |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
L<JSON>, |
180
|
|
|
|
|
|
|
L<Egg::Release>, |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 AUTHOR |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt> |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 COPYRIGHT |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Copyright (C) 2007 by Bee Flag, Corp. E<lt>http://egg.bomcity.com/E<gt>, All Rights Reserved. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
191
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.6 or, |
192
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |