… | |
… | |
878 | Other languages could read single lines terminated by a newline and pass |
878 | Other languages could read single lines terminated by a newline and pass |
879 | this line into their JSON decoder of choice. |
879 | this line into their JSON decoder of choice. |
880 | |
880 | |
881 | =cut |
881 | =cut |
882 | |
882 | |
|
|
883 | sub json_coder() { |
|
|
884 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
885 | || do { require JSON; JSON->new->utf8 } |
|
|
886 | } |
|
|
887 | |
883 | register_write_type json => sub { |
888 | register_write_type json => sub { |
884 | my ($self, $ref) = @_; |
889 | my ($self, $ref) = @_; |
885 | |
890 | |
886 | require JSON; |
891 | my $json = $self->{json} ||= json_coder; |
887 | |
892 | |
888 | $self->{json} ? $self->{json}->encode ($ref) |
893 | $json->encode ($ref) |
889 | : JSON::encode_json ($ref) |
|
|
890 | }; |
894 | }; |
891 | |
895 | |
892 | =item storable => $reference |
896 | =item storable => $reference |
893 | |
897 | |
894 | Freezes the given reference using L<Storable> and writes it to the |
898 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
1448 | =cut |
1452 | =cut |
1449 | |
1453 | |
1450 | register_read_type json => sub { |
1454 | register_read_type json => sub { |
1451 | my ($self, $cb) = @_; |
1455 | my ($self, $cb) = @_; |
1452 | |
1456 | |
1453 | my $json = $self->{json} ||= |
1457 | my $json = $self->{json} ||= json_coder; |
1454 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
1455 | || do { require JSON; JSON->new->utf8 }; |
|
|
1456 | |
1458 | |
1457 | my $data; |
1459 | my $data; |
1458 | my $rbuf = \$self->{rbuf}; |
1460 | my $rbuf = \$self->{rbuf}; |
1459 | |
1461 | |
1460 | sub { |
1462 | sub { |