ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.109 by root, Wed Jan 14 02:03:43 2009 UTC vs.
Revision 1.114 by root, Wed Jan 21 06:06:22 2009 UTC

1144 } 1144 }
1145}; 1145};
1146 1146
1147=item json => $cb->($handle, $hash_or_arrayref) 1147=item json => $cb->($handle, $hash_or_arrayref)
1148 1148
1149Reads a JSON object or array, decodes it and passes it to the callback. 1149Reads a JSON object or array, decodes it and passes it to the
1150callback. When a parse error occurs, an C<EBADMSG> error will be raised.
1150 1151
1151If a C<json> object was passed to the constructor, then that will be used 1152If a C<json> object was passed to the constructor, then that will be used
1152for the final decode, otherwise it will create a JSON coder expecting UTF-8. 1153for the final decode, otherwise it will create a JSON coder expecting UTF-8.
1153 1154
1154This read type uses the incremental parser available with JSON version 1155This read type uses the incremental parser available with JSON version
1171 my $rbuf = \$self->{rbuf}; 1172 my $rbuf = \$self->{rbuf};
1172 1173
1173 my $json = $self->{json} ||= JSON->new->utf8; 1174 my $json = $self->{json} ||= JSON->new->utf8;
1174 1175
1175 sub { 1176 sub {
1176 my $ref = $json->incr_parse ($self->{rbuf}); 1177 my $ref = eval { $json->incr_parse ($self->{rbuf}) };
1177 1178
1178 if ($ref) { 1179 if ($ref) {
1179 $self->{rbuf} = $json->incr_text; 1180 $self->{rbuf} = $json->incr_text;
1180 $json->incr_text = ""; 1181 $json->incr_text = "";
1181 $cb->($self, $ref); 1182 $cb->($self, $ref);
1182 1183
1183 1 1184 1
1185 } elsif ($@) {
1186 # error case
1187 $json->incr_skip;
1188
1189 $self->{rbuf} = $json->incr_text;
1190 $json->incr_text = "";
1191
1192 $self->_error (&Errno::EBADMSG);
1193
1194 ()
1184 } else { 1195 } else {
1185 $self->{rbuf} = ""; 1196 $self->{rbuf} = "";
1197
1186 () 1198 ()
1187 } 1199 }
1188 } 1200 }
1189}; 1201};
1190 1202

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines