File Coverage

blib/lib/RDF/Trine/Iterator/JSONHandler.pm
Criterion Covered Total %
statement 19 57 33.3
branch 0 22 0.0
condition n/a
subroutine 7 9 77.7
pod 2 2 100.0
total 28 90 31.1


line stmt bran cond sub pod time code
1             # RDF::Trine::Iterator::JSONHandler
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Trine::Iterator::JSONHandler - JSON Handler for parsing SPARQL JSON Results format
7              
8             =head1 VERSION
9              
10             This document describes RDF::Trine::Iterator::JSONHandler version 1.017
11              
12             =head1 STATUS
13              
14             This module's API and functionality should be considered unstable.
15             In the future, this module may change in backwards-incompatible ways,
16             or be removed entirely. If you need functionality that this module provides,
17             please L<get in touch|http://www.perlrdf.org/>.
18              
19             =head1 SYNOPSIS
20              
21             use RDF::Trine::Iterator::JSONHandler;
22             my $handler = RDF::Trine::Iterator::JSONHandler->new();
23             my $iter = $handler->parse( $json );
24              
25             =head1 METHODS
26              
27             =over 4
28              
29             =cut
30              
31             package RDF::Trine::Iterator::JSONHandler;
32              
33 68     68   428 use strict;
  68         150  
  68         1722  
34 68     68   324 use warnings;
  68         144  
  68         1650  
35 68     68   328 use Scalar::Util qw(refaddr);
  68         143  
  68         2741  
36              
37 68     68   34152 use JSON;
  68         517544  
  68         393  
38 68     68   8649 use Data::Dumper;
  68         168  
  68         2768  
39 68     68   399 use RDF::Trine::VariableBindings;
  68         140  
  68         2240  
40              
41             our ($VERSION);
42             BEGIN {
43 68     68   21965 $VERSION = '1.017';
44             }
45              
46             =item C<< new >>
47              
48             Returns a new JSON SPARQL Results parser object.
49              
50             =cut
51              
52             sub new {
53 0     0 1   my $class = shift;
54 0 0         my %args = %{ shift || {} };
  0            
55 0           return bless(\%args, $class);
56             }
57              
58             =item C<< parse ( $json ) >>
59              
60             Returns a RDF::Trine::Iterator object containing the data from the supplied JSON
61             in JSON SPARQL Results format.
62              
63             =cut
64              
65             sub parse {
66 0     0 1   my $self = shift;
67 0           my $json = shift;
68 0           my $data = from_json($json, {utf8 => 1});
69 0           my $head = $data->{head};
70 0           my $vars = $head->{vars};
71 0           my $res = $data->{results};
72 0 0         if (defined(my $bool = $data->{boolean})) {
    0          
73 0 0         my $value = ($bool) ? 1 : 0;
74 0           return RDF::Trine::Iterator::Boolean->new([$value]);
75             } elsif (my $binds = $res->{bindings}) {
76 0           my @results;
77 0           foreach my $b (@$binds) {
78 0           my %data;
79 0           foreach my $v (@$vars) {
80 0 0         if (defined(my $value = $b->{ $v })) {
81 0           my $type = $value->{type};
82 0 0         if ($type eq 'uri') {
    0          
    0          
    0          
83 0           my $data = $value->{value};
84 0           $data{ $v } = RDF::Trine::Node::Resource->new( $data );
85             } elsif ($type eq 'bnode') {
86 0           my $data = $value->{value};
87 0           $data{ $v } = RDF::Trine::Node::Blank->new( $data );
88             } elsif ($type eq 'literal') {
89 0           my $data = $value->{value};
90 0 0         if (my $lang = $value->{'xml:lang'}) {
91 0           $data{ $v } = RDF::Trine::Node::Literal->new( $data, $lang );
92             } else {
93 0           $data{ $v } = RDF::Trine::Node::Literal->new( $data );
94             }
95             } elsif ($type eq 'typed-literal') {
96 0           my $data = $value->{value};
97 0           my $dt = $value->{datatype};
98 0 0         if ($self->{canonicalize}) {
99 0           $data = RDF::Trine::Node::Literal->canonicalize_literal_value( $data, $dt, 0 );
100             }
101 0           $data{ $v } = RDF::Trine::Node::Literal->new( $data, undef, $dt );
102             } else {
103 0           warn Dumper($data, $b);
104 0           throw RDF::Trine::Error -text => "Unknown node type $type during parsing of SPARQL JSON Results";
105             }
106             }
107             }
108 0           push(@results, RDF::Trine::VariableBindings->new( \%data ));
109             }
110 0           return RDF::Trine::Iterator::Bindings->new( \@results );
111             }
112 0           warn '*** ' . Dumper($data);
113             }
114              
115             1;
116              
117             __END__
118              
119             =back
120              
121             =head1 BUGS
122              
123             Please report any bugs or feature requests to through the GitHub web interface
124             at L<https://github.com/kasei/perlrdf/issues>.
125              
126             =head1 AUTHOR
127              
128             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
129              
130             =head1 COPYRIGHT
131              
132             Copyright (c) 2006-2012 Gregory Todd Williams. This
133             program is free software; you can redistribute it and/or modify it under
134             the same terms as Perl itself.
135              
136             =cut