From: Tara L Andrews Date: Sat, 10 Mar 2012 21:37:40 +0000 (+0100) Subject: add JSON export function X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f025e3032e82c3134f2fb4f787db40163ac851b4;p=scpubgit%2Fstemmatology.git add JSON export function --- diff --git a/lib/Text/Tradition/Witness.pm b/lib/Text/Tradition/Witness.pm index 776cebe..f1dd5be 100644 --- a/lib/Text/Tradition/Witness.pm +++ b/lib/Text/Tradition/Witness.pm @@ -185,6 +185,66 @@ sub BUILD { } } +=head2 export_as_json + +Exports the witness as a JSON structure, with the following keys: + +=over 4 + +=item * id - The witness sigil + +=item * name - The witness identifier + +=item * tokens - An array of hashes of the form { "t":"WORD" } + +=back + +=begin testing + +use Text::Tradition; + +my @text = qw( This is a line of text ); +my $wit = Text::Tradition::Witness->new( + 'sigil' => 'A', + 'text' => \@text, + 'identifier' => 'test witness', + ); +my $jsonstruct = $wit->export_as_json; +is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" ); +is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" ); +is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" ); +foreach my $idx ( 0 .. $#text ) { + is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" ); +} + +my @ctext = qw( when april with his showers sweet with fruit the drought of march + has pierced unto the root ); +my $trad = Text::Tradition->new( + 'input' => 'CollateX', + 'file' => 't/data/Collatex-16.xml' ); + +$jsonstruct = $trad->witness('A')->export_as_json; +is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" ); +is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" ); +is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" ); +foreach my $idx ( 0 .. $#ctext ) { + is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" ); +} + +=end testing + +=cut + +sub export_as_json { + my $self = shift; + my @wordlist = map { { 't' => $_ || '' } } @{$self->text}; + return { + 'id' => $self->sigil, + 'tokens' => \@wordlist, + 'name' => $self->identifier, + }; +} + no Moose; __PACKAGE__->meta->make_immutable; diff --git a/t/text_tradition_witness.t b/t/text_tradition_witness.t index bd507c3..3a2518c 100644 --- a/t/text_tradition_witness.t +++ b/t/text_tradition_witness.t @@ -24,5 +24,40 @@ if( $wit ) { +# =begin testing +{ +use Text::Tradition; + +my @text = qw( This is a line of text ); +my $wit = Text::Tradition::Witness->new( + 'sigil' => 'A', + 'text' => \@text, + 'identifier' => 'test witness', + ); +my $jsonstruct = $wit->export_as_json; +is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" ); +is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" ); +is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" ); +foreach my $idx ( 0 .. $#text ) { + is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" ); +} + +my @ctext = qw( when april with his showers sweet with fruit the drought of march + has pierced unto the root ); +my $trad = Text::Tradition->new( + 'input' => 'CollateX', + 'file' => 't/data/Collatex-16.xml' ); + +$jsonstruct = $trad->witness('A')->export_as_json; +is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" ); +is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" ); +is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" ); +foreach my $idx ( 0 .. $#ctext ) { + is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" ); +} +} + + + 1;