r1315@llin: dpavlin | 2007-08-23 22:52:10 +0200
[webpac2] / lib / WebPAC / Output / JSON.pm
index f395852..7cc4089 100644 (file)
@@ -6,8 +6,10 @@ use strict;
 use base qw/WebPAC::Common Class::Accessor/;
 __PACKAGE__->mk_accessors(qw(path));
 
-use Encode qw/from_to/;
+#use Encode qw/from_to/;
 use Data::Dump qw/dump/;
+use JSON;
+use File::Slurp;
 
 =head1 NAME
 
@@ -55,6 +57,8 @@ sub init {
 
        $log->debug('init');
 
+       $self->{_data} = [];
+
        return 1;
 }
 
@@ -78,6 +82,22 @@ sub add {
 
        $log->debug("id: $id ds = ",dump($ds));
 
+       my $item;
+
+       foreach my $t ( keys %$ds ) {
+               my $name = lc($t);
+               $name =~ s/\W+/_/g;
+
+               # FIXME get rid of non hash values in data_structure for consistency?
+               next unless ref($ds->{$t}) eq 'HASH';
+
+               if ( defined( $ds->{$t}->{display} ) ) {
+                       $item->{$name} = $ds->{$t}->{display};
+               }
+       }
+
+       push @{ $self->{_data} }, $item;
+
        return 1;
 }
 
@@ -92,7 +112,8 @@ sub finish {
 
        my $log = $self->_get_logger();
 
-       $log->info("finish");
+       $log->info("writing JSON output to ", $self->path);
+       write_file( $self->path, objToJson( { items => $self->{_data} } ) );
 
 }