tokuhirom
11/28/2013 - 4:20 AM

JSON.pm

{
    package HTTP::Body::JSON;
    use JSON::XS;
    use parent qw(HTTP::Body);
    use HTTP::Body;
    use Encode qw(encode_utf8);

    $HTTP::Body::TYPES->{'application/json'} = __PACKAGE__;

    sub spin {
        my $self = shift;

        return unless $self->length == $self->content_length;

        my $dat = JSON::XS::decode_json($self->{buffer});
        while (my ($k, $v) = each %$dat) {
            $self->param(encode_utf8($k), encode_utf8($v));
        }

        $self->{buffer} = '';
        $self->{state} = 'done';
    }
}