… | |
… | |
496 | callback. |
496 | callback. |
497 | |
497 | |
498 | This callback will only be called on TLS shutdowns, not when the |
498 | This callback will only be called on TLS shutdowns, not when the |
499 | underlying handle signals EOF. |
499 | underlying handle signals EOF. |
500 | |
500 | |
501 | =item json => L<JSON> or L<JSON::XS> object |
501 | =item json => L<JSON>, L<JSON::PP> or L<JSON::XS> object |
502 | |
502 | |
503 | This is the json coder object used by the C<json> read and write types. |
503 | This is the json coder object used by the C<json> read and write types. |
504 | |
504 | |
505 | If you don't supply it, then AnyEvent::Handle will create and use a |
505 | If you don't supply it, then AnyEvent::Handle will create and use a |
506 | suitable one (on demand), which will write and expect UTF-8 encoded JSON |
506 | suitable one (on demand), which will write and expect UTF-8 encoded JSON |
507 | texts. |
507 | texts. |
508 | |
|
|
509 | Note that you are responsible to depend on the L<JSON> module if you want |
|
|
510 | to use this functionality, as AnyEvent does not have a dependency on it |
|
|
511 | itself. |
|
|
512 | |
508 | |
513 | =item cbor => L<CBOR::XS> object |
509 | =item cbor => L<CBOR::XS> object |
514 | |
510 | |
515 | This is the cbor coder object used by the C<cbor> read and write types. |
511 | This is the cbor coder object used by the C<cbor> read and write types. |
516 | |
512 | |
… | |
… | |
1106 | |
1102 | |
1107 | =cut |
1103 | =cut |
1108 | |
1104 | |
1109 | sub json_coder() { |
1105 | sub json_coder() { |
1110 | eval { require JSON::XS; JSON::XS->new->utf8 } |
1106 | eval { require JSON::XS; JSON::XS->new->utf8 } |
1111 | || do { require JSON; JSON->new->utf8 } |
1107 | || do { require JSON::PP; JSON::PP->new->utf8 } |
1112 | } |
1108 | } |
1113 | |
1109 | |
1114 | register_write_type json => sub { |
1110 | register_write_type json => sub { |
1115 | my ($self, $ref) = @_; |
1111 | my ($self, $ref) = @_; |
1116 | |
1112 | |
… | |
… | |
1708 | =item json => $cb->($handle, $hash_or_arrayref) |
1704 | =item json => $cb->($handle, $hash_or_arrayref) |
1709 | |
1705 | |
1710 | Reads a JSON object or array, decodes it and passes it to the |
1706 | Reads a JSON object or array, decodes it and passes it to the |
1711 | callback. When a parse error occurs, an C<EBADMSG> error will be raised. |
1707 | callback. When a parse error occurs, an C<EBADMSG> error will be raised. |
1712 | |
1708 | |
1713 | If a C<json> object was passed to the constructor, then that will be used |
1709 | If a C<json> object was passed to the constructor, then that will be |
1714 | for the final decode, otherwise it will create a JSON coder expecting UTF-8. |
1710 | used for the final decode, otherwise it will create a L<JSON::XS> or |
|
|
1711 | L<JSON::PP> coder object expecting UTF-8. |
1715 | |
1712 | |
1716 | This read type uses the incremental parser available with JSON version |
1713 | This read type uses the incremental parser available with JSON version |
1717 | 2.09 (and JSON::XS version 2.2) and above. You have to provide a |
1714 | 2.09 (and JSON::XS version 2.2) and above. |
1718 | dependency on your own: this module will load the JSON module, but |
|
|
1719 | AnyEvent does not depend on it itself. |
|
|
1720 | |
1715 | |
1721 | Since JSON texts are fully self-delimiting, the C<json> read and write |
1716 | Since JSON texts are fully self-delimiting, the C<json> read and write |
1722 | types are an ideal simple RPC protocol: just exchange JSON datagrams. See |
1717 | types are an ideal simple RPC protocol: just exchange JSON datagrams. See |
1723 | the C<json> write type description, above, for an actual example. |
1718 | the C<json> write type description, above, for an actual example. |
1724 | |
1719 | |