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.110 by root, Wed Jan 21 05:36:45 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 {
1177 eval {
1176 my $ref = $json->incr_parse ($self->{rbuf}); 1178 my $ref = $json->incr_parse ($self->{rbuf});
1177 1179
1178 if ($ref) { 1180 if ($ref) {
1181 $self->{rbuf} = $json->incr_text;
1182 $json->incr_text = "";
1183 $cb->($self, $ref);
1184
1185 1
1186 } else {
1187 $self->{rbuf} = "";
1188 ()
1189 }
1190
1191 1
1192 } or do {
1193 $json->incr_skip;
1194
1179 $self->{rbuf} = $json->incr_text; 1195 $self->{rbuf} = $json->incr_text;
1180 $json->incr_text = ""; 1196 $json->incr_text = "";
1181 $cb->($self, $ref);
1182 1197
1183 1 1198 $self->_error (&Errno::EBADMSG);
1184 } else {
1185 $self->{rbuf} = "";
1186 ()
1187 } 1199 };
1200
1188 } 1201 }
1189}; 1202};
1190 1203
1191=item storable => $cb->($handle, $ref) 1204=item storable => $cb->($handle, $ref)
1192 1205

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines