use JSON for serialization rather than rolling own
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading / WordForm.pm
index 8f519f9..6a6b56b 100644 (file)
@@ -1,5 +1,7 @@
 package Text::Tradition::Collation::Reading::WordForm;
 
+use Lingua::Features::Structure;
+use JSON ();
 use Moose;
 
 =head1 NAME
@@ -57,27 +59,44 @@ has 'lemma' => (
        
 has 'morphology' => (
        is => 'ro',
-       isa => 'ArrayRef',
+       isa => 'Lingua::Features::Structure',
        required => 1,
        );
        
 around BUILDARGS => sub {
        my $orig = shift;
        my $class = shift;
-       my %args = @_ == 1 ? %{$_[0]} : @_;
-       unless( ref( $args{'morphology'} ) ) {
-               my @morph = split( '', $args{'morphology'} );
-               $args{'morphology'} = \@morph;
+       my $args = @_ == 1 ? $_[0] : { @_ };
+       if( exists $args->{'JSON'} ) {
+               my $data = $args->{'JSON'};
+               my $morph = Lingua::Features::Structure->from_string( $data->{'morphology'} );
+               $data->{'morphology'} = $morph;
+               $args = $data;
        }
-       $class->$orig( %args );
+       $class->$orig( $args );
 };
+       
+=head2 to_string
+
+Returns a string combination of language/lemma/morphology that can be used
+in equivalence testing.
+
+=cut
 
-sub _stringify {
+sub to_string {
        my $self = shift;
-       return sprintf( "%s//%s//%s", $self->language, $self->lemma,
-               join( '|', @{$self->morphology} ) );
+       return JSON->new->convert_blessed(1)->encode( $self );
 }
 
+sub TO_JSON {
+       my $self = shift;
+       return { 
+               'language' => $self->language,
+               'lemma' => $self->lemma,
+               'morphology' => $self->morphology->to_string
+       };
+}
+       
 no Moose;
 __PACKAGE__->meta->make_immutable;