From: Tara L Andrews Date: Sun, 15 Apr 2012 21:29:22 +0000 (+0200) Subject: get JSON witness parsing to work X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=65ed66b94aa5b05c0f64f6040642d41a980e95eb;hp=fae52efdaeb4b67f58e47ec8ebe110537f2535ea;p=scpubgit%2Fstemmatology.git get JSON witness parsing to work --- diff --git a/lib/Text/Tradition.pm b/lib/Text/Tradition.pm index 5f8ad83..fed22a3 100644 --- a/lib/Text/Tradition.pm +++ b/lib/Text/Tradition.pm @@ -1,6 +1,6 @@ package Text::Tradition; -use JSON qw / decode_json /; +use JSON qw / from_json /; use Module::Load; use Moose; use Text::Tradition::Collation; @@ -294,8 +294,8 @@ each element therein. sub add_json_witnesses { my( $self, $jsonstr, $extraopts ) = @_; - my $witarray = decode_json( $jsonstr ); - foreach my $witspec ( @$witarray ) { + my $witarray = from_json( $jsonstr ); + foreach my $witspec ( @{$witarray->{witnesses}} ) { my $opts = $extraopts || {}; $opts->{'sourcetype'} = 'json'; $opts->{'object'} = $witspec; diff --git a/lib/Text/Tradition/Collation.pm b/lib/Text/Tradition/Collation.pm index 4f7f571..721b385 100644 --- a/lib/Text/Tradition/Collation.pm +++ b/lib/Text/Tradition/Collation.pm @@ -1390,7 +1390,6 @@ sub make_witness_path { # Add start and end if necessary unshift( @chain, $self->start ) unless $chain[0] eq $self->start; push( @chain, $self->end ) unless $chain[-1] eq $self->end; - $DB::single = 1; foreach my $idx ( 0 .. $#chain-1 ) { $self->add_path( $chain[$idx], $chain[$idx+1], $sig ); } diff --git a/lib/Text/Tradition/Witness.pm b/lib/Text/Tradition/Witness.pm index 4103651..8c5c87e 100644 --- a/lib/Text/Tradition/Witness.pm +++ b/lib/Text/Tradition/Witness.pm @@ -143,17 +143,17 @@ if( $ptwit ) { is( $c->path_text( $ptwit->sigil ), $str, "Witness has correct text" ); } -# # Test some JSON witnesses via object -# open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input"; -# binmode( JSIN, ':encoding(UTF-8)' ); -# my @lines = ; -# close JSIN; -# $trad->add_json_witnesses( join( '', @lines ) ); -# is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness', -# "Found first JSON witness" ); -# is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness', -# "Found second JSON witness" ); -# +# Test some JSON witnesses via object +open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input"; +binmode( JSIN, ':encoding(UTF-8)' ); +my @lines = ; +close JSIN; +$trad->add_json_witnesses( join( '', @lines ) ); +is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness', + "Found first JSON witness" ); +is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness', + "Found second JSON witness" ); + # # Test an XML witness via file # my $xmlwit = $trad->add_witness( 'sourcetype' => 'xmldesc', # 'file' => 't/data/witnesses/teiwit.xml' ); @@ -392,7 +392,8 @@ sub _init_from_xmldesc { if( $descnode->hasAttribute('xml:id') ) { $self->_set_sigil( $descnode->getAttribute('xml:id') ); } elsif( !$self->has_sigil ) { - throw( 'Could not find xml:id witness sigil' ); + throw( ident => 'missing sigil', + message => 'Could not find xml:id witness sigil' ); } } else { throw( ident => "bad source", @@ -603,11 +604,26 @@ sub _init_from_json { my $wit; if( $self->has_object ) { $wit = $self->object; - } else { - + } elsif( $self->has_string ) { + $wit = from_json( $self->string ); + } elsif( $self->has_file ) { + my $ok = open( INPUT, $self->file ); + unless( $ok ) { + throw( ident => "bad source", + message => 'Could not open ' . $self->file . ' for reading' ); + } + binmode( INPUT, ':encoding(UTF-8)' ); + my @lines = ; + close INPUT; + $wit = from_json( join( '', @lines ) ); } - $self->sigil( $wit->{'id'} ); + if( exists $wit->{'id'} ) { + $self->_set_sigil( $wit->{'id'} ); + } elsif( !$self->has_sigil ) { + throw( ident => 'missing sigil', + message => 'Could not find witness sigil (id) in JSON spec' ); + } $self->identifier( $wit->{'name'} ); my @words; my @layerwords; @@ -619,14 +635,14 @@ sub _init_from_json { my $ctr = 0; foreach my $token ( @{$wit->{'tokens'}} ) { my $w_obj = $self->tradition->collation->add_reading({ - 'text' => $token, 'id' => $self->sigil . 'r' . $ctr++ }); + 'text' => $token->{'t'}, 'id' => $self->sigil . 'r' . $ctr++ }); push( @words, $w_obj ); } ## TODO rethink this JSOn mechanism if( exists $wit->{'layertokens'} ) { foreach my $token ( @{$wit->{'layertokens'}} ) { my $w_obj = $self->tradition->collation->add_reading({ - 'text' => $token, 'id' => $self->sigil . 'r' . $ctr++ }); + 'text' => $token->{'t'}, 'id' => $self->sigil . 'r' . $ctr++ }); push( @layerwords, $w_obj ); } } diff --git a/t/text_tradition_witness.t b/t/text_tradition_witness.t index d2406e7..20e8f22 100644 --- a/t/text_tradition_witness.t +++ b/t/text_tradition_witness.t @@ -25,17 +25,17 @@ if( $ptwit ) { is( $c->path_text( $ptwit->sigil ), $str, "Witness has correct text" ); } -# # Test some JSON witnesses via object -# open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input"; -# binmode( JSIN, ':encoding(UTF-8)' ); -# my @lines = ; -# close JSIN; -# $trad->add_json_witnesses( join( '', @lines ) ); -# is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness', -# "Found first JSON witness" ); -# is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness', -# "Found second JSON witness" ); -# +# Test some JSON witnesses via object +open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input"; +binmode( JSIN, ':encoding(UTF-8)' ); +my @lines = ; +close JSIN; +$trad->add_json_witnesses( join( '', @lines ) ); +is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness', + "Found first JSON witness" ); +is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness', + "Found second JSON witness" ); + # # Test an XML witness via file # my $xmlwit = $trad->add_witness( 'sourcetype' => 'xmldesc', # 'file' => 't/data/witnesses/teiwit.xml' );