[Dancer-users] Follow-up: extending the serializer
Mr. Puneet Kishor
punk.kish at gmail.com
Wed Jun 8 01:03:44 CEST 2011
On Jun 7, 2011, at 4:51 PM, Mr. Puneet Kishor wrote:
> I am trying to write a new serializer plugin called `Dancer::Serializer::Tgz` modeled after Data::Dumper, JSON, etc. The idea is that, in conjunction with `Dancer::Plugin::REST` and `Dancer::Serializer::Mutable`, the user would be able to request data in .tgz format on demand like so --
>
> $ curl http://example.com/employees.tgz
>
> Here is my baby code, and it gives me a rather unhelpful "an internal error occurred". What am I doing wrong?
After much fiddling, I was able to see the following errors --
in my Apache error log, I get
Deep recursion on subroutine "Dancer::Error::render" at ../Dancer.pm line 312.
Deep recursion on subroutine "Dancer::Error::_render_serialized" at ../Dancer/Error.pm line 167.
Deep recursion on subroutine "Dancer::Serializer::Tgz::serialize" at ../Dancer/Error.pm line 177.
Deep recursion on subroutine "Dancer::_send_file" ../Dancer.pm line 140.
If I uncomment the `sub content_type {'application/gzip'}` I also get
Subroutine content_type redefined at ../Dancer/Serializer/Tgz.pm line 43.
Also, if I comment the `send_file` command in serialize(), I actually do get a result.tgz. If I untar that result.tgz, I see the following text
{
"error" : "Serializer (Dancer::Serializer::Tgz) failed at serializing Dancer::Error=HASH(0x100a93008):\nencountered object 'Dancer::Error=HASH(0x100a93008)', but neither allow_blessed nor convert_blessed settings are enabled at (eval 71) line 153.\n"
}
What am I doing wrong?
> Note: in this case, only the serializer (downloading data) makes sense; the deserializer doesn't make any sense.
>
> -----
>
> package Dancer::Serializer::Tgz;
>
> use strict;
> use warnings;
> use Carp;
> use Dancer ':syntax';
> use Archive::Tar;
> use base 'Dancer::Serializer::Abstract';
>
> # helpers
>
> sub from_tgz {
> my ($string) = @_;
> my $s = Dancer::Serializer::Tgz->new;
> $s->deserialize($string);
> }
>
> sub to_tgz {
> my ($data) = @_;
> my $s = Dancer::Serializer::Tgz->new;
> $s->serialize($data);
> }
>
> sub serialize {
> my ($self, $entity) = @_;
>
> my $tar = Archive::Tar->new;
> $tar->add_data('result', to_json($entity));
>
> my $file = '/tmp/result.tgz';
> $tar->write($file, COMPRESS_GZIP);
> send_file($file, content_type => 'application/gzip', system_path => 1);
> }
>
> sub deserialize {
> my ($self, $content) = @_;
> croak "not yet implemented";
> }
>
> sub content_type {'application/gzip'}
>
> 1;
More information about the Dancer-users
mailing list