line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Egg::Plugin::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
|
|
1780074
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
85
|
|
8
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
64
|
|
9
|
2
|
|
|
2
|
|
11468
|
use FileHandle; |
|
2
|
|
|
|
|
25161
|
|
|
2
|
|
|
|
|
13
|
|
10
|
2
|
|
|
2
|
|
1888
|
use JSON; |
|
2
|
|
|
|
|
323022
|
|
|
2
|
|
|
|
|
15
|
|
11
|
2
|
|
|
2
|
|
311
|
use Carp qw/croak/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
996
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Egg::Release::JSON - JSON for Egg::Plugin. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Controller. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use Egg qw/ JSON /; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Example code. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $json_data = { |
28
|
|
|
|
|
|
|
aaaaa => 'bbbbb', |
29
|
|
|
|
|
|
|
ccccc => 'ddddd', |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# |
33
|
|
|
|
|
|
|
# Mutual conversion of JSON data. |
34
|
|
|
|
|
|
|
# |
35
|
|
|
|
|
|
|
my $json_js = $e->obj2json($json_data); |
36
|
|
|
|
|
|
|
my $json_hash = $e->json2obj($json_js); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# |
39
|
|
|
|
|
|
|
# The JSON module object is acquired. |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
my $json= $e->json; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
It is a plugin to treat JSON. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
L<JSON> module is used. |
48
|
|
|
|
|
|
|
Please refer to the document of L<JSON> for details. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 METHODS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 obj2json ( [JSON_DATA] ) |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
It is wraper to the 'objToJson' function of L<JSON> module. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
HASH and ARRAY are given to JSON_DATA. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $js= $e->obj2json($local_data); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
0
|
|
|
0
|
1
|
|
sub json2obj { shift; JSON::jsonToObj(@_) } |
|
0
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 json2obj ( [JSON_JS] ) |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
It is wraper to the 'jsonToObj' function of L<JSON > module. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
The JSON data is given to JSON_JS. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $local_data= $e->json2obj($json_js); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
0
|
|
|
0
|
1
|
|
sub obj2json { shift; JSON::objToJson(@_) } |
|
0
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 json |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
The object of L<JSON> module is returned. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $json= $e->json; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
0
|
|
0
|
0
|
1
|
|
sub json { shift->{json_handler} ||= JSON->new(@_) } |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 get_json ( [FILE_PATH] || [REQUEST_METHOD], [URL], [LWP_OPTION]) |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
The JSON code is acquired by the file and URL and the Egg::Plugin::JSON::Result |
86
|
|
|
|
|
|
|
object is returned. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
The occurrence of the error can be confirmed by is_success and the is_error |
89
|
|
|
|
|
|
|
method of the returned object. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
* When URL is specified, the thing that L<Egg::Plugin::LWP> can be used. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $result= $e->get_json( GET=> 'http://domain/json_code' ); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $json_obj; |
96
|
|
|
|
|
|
|
if ($result->is_success and $json_obj= $result->obj) { |
97
|
|
|
|
|
|
|
$e->view->param('json_text', $json_obj->{message}); |
98
|
|
|
|
|
|
|
} else { |
99
|
|
|
|
|
|
|
$e->debug_out('JSON ERROR : '. $result->is_error); |
100
|
|
|
|
|
|
|
$e->finished(500); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
sub get_json { |
105
|
0
|
|
|
0
|
1
|
|
my $e = shift; |
106
|
0
|
|
0
|
|
|
|
my $sc= shift || croak q{ I want argument. }; |
107
|
0
|
|
|
|
|
|
my $result_class= 'Egg::Plugin::JSON::Result'; |
108
|
0
|
|
|
|
|
|
my $data; |
109
|
0
|
0
|
|
|
|
|
if (my $url= shift) { |
110
|
0
|
|
|
|
|
|
my $res= $e->ua->request($sc, $url, @_); |
111
|
0
|
0
|
|
|
|
|
if ($res->is_success) { |
112
|
0
|
|
|
|
|
|
return $result_class->new(1, $e->json2obj($res->content)); |
113
|
|
|
|
|
|
|
} else { |
114
|
|
|
|
|
|
|
my $error= $res |
115
|
0
|
0
|
|
|
|
|
? do { $res->status_line || 'Internal error(1).' } |
|
0
|
0
|
|
|
|
|
|
116
|
|
|
|
|
|
|
: 'Internal error(2).'; |
117
|
0
|
|
|
|
|
|
return $result_class->new(0, $error); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} else { |
120
|
0
|
|
0
|
|
|
|
my $fh= FileHandle->new($sc) |
121
|
|
|
|
|
|
|
|| return $result_class->new(0, "$! - $sc"); |
122
|
0
|
|
|
|
|
|
my $js_code= join '', $fh->getlines; |
123
|
0
|
|
|
|
|
|
$fh->close; |
124
|
0
|
|
|
|
|
|
return $result_class->new(1, $e->json2obj($js_code)); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
package Egg::Plugin::JSON::Result; |
129
|
2
|
|
|
2
|
|
15
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
89
|
|
130
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
85
|
|
131
|
2
|
|
|
2
|
|
12
|
use base qw/Class::Accessor::Fast/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2188
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 RESULT METHODS |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
It is a method of Egg::Plugin::JSON::Result that get_json returns. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/is_success is_error obj/); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub new { |
142
|
0
|
|
|
0
|
|
|
my $class = shift; |
143
|
0
|
|
0
|
|
|
|
my $success= shift || 0; |
144
|
0
|
|
0
|
|
|
|
my $obj= shift || do { $success= 0; 'There is no data.' }; |
145
|
0
|
0
|
|
|
|
|
bless { |
146
|
|
|
|
|
|
|
is_success=> $success, |
147
|
0
|
|
|
|
|
|
%{ $success ? { obj => $obj }: { is_error => $obj } }, |
148
|
|
|
|
|
|
|
}, $class; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 new |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Constructor |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 is_success |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
When the data conversion of the obtained JSON code succeeds, true is restored. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 obj |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Data returns when is_success is true. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 is_error |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
The error message returns when is_success is false. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SEE ALSO |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
L<JSON>, |
170
|
|
|
|
|
|
|
L<Egg::Release>, |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 COPYRIGHT |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Copyright (C) 2007 by Bee Flag, Corp. E<lt>http://egg.bomcity.com/E<gt>, All Rights Reserved. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
181
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.6 or, |
182
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |