| 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.019 | ||||||||||||||||||||||
| 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 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 34 | 68 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 35 | 68 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 68 | ▶ | use JSON; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 38 | 68 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 39 | 68 | ▶ | use RDF::Trine::VariableBindings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | 68 | our ($VERSION); | |||||||||||||||||||||
| 42 | 68 | ▶ | BEGIN { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 43 | 68 | $VERSION = '1.019'; | |||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | =item C<< new >> | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | Returns a new JSON SPARQL Results parser object. | ||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 53 | 0 | my $class = shift; | |||||||||||||||||||||
| 54 | 0 | ▶ | my %args = %{ shift || {} }; | ||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||
| 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 | 0 | ▶ | sub parse { | ||||||||||||||||||||
|
Subroutine
parse: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||
| 66 | 0 | 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 | ▶ | if (defined(my $bool = $data->{boolean})) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 73 | 0 | ▶ | my $value = ($bool) ? 1 : 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | if (defined(my $value = $b->{ $v })) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 81 | 0 | my $type = $value->{type}; | |||||||||||||||||||||
| 82 | 0 | ▶ | if ($type eq 'uri') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 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 | ▶ | if (my $lang = $value->{'xml:lang'}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | if ($self->{canonicalize}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | 68 | 1; | |||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||