WebService::Simple::Parser::JSON - Parse JSON content


WebService-Simple documentation Contained in the WebService-Simple distribution.

Index


Code Index:

NAME

Top

WebService::Simple::Parser::JSON - Parse JSON content

SYNOPSIS

Top

  my $service = WebService::Simple->new(
    base_url => ...,
    response_parser => 'JSON',
  );
  my $res = $service->get(...);
  my $json = $res->parse_response();

METHODS

Top

new

parse_response


WebService-Simple documentation Contained in the WebService-Simple distribution.

# $Id: JSON.pm 26994 2008-12-18 03:11:11Z drry $

package WebService::Simple::Parser::JSON;
use strict;
use warnings;
use base qw(WebService::Simple::Parser);
use JSON 2.0;

sub new
{
    my $class = shift;
    my %args  = @_;

    my $json  = delete $args{json} || JSON->new;
    my $self  = $class->SUPER::new(%args);
    $self->{json} = $json;
    return $self;
}

sub parse_response
{
    my $self = shift;
    my $content =$_[0]->content;
    # JSONP to pure JSON
    $content =~ s/[a-zA-Z_\$][a-zA-Z0-9_\$]*\s*\((.+)\)\s*;?\s*$/$1/;
    $self->{json}->decode( $content );
}

1;

__END__