/[webpac2]/trunk/lib/WebPAC/Output/JSON.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/WebPAC/Output/JSON.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 883 - (show annotations)
Thu Aug 23 20:56:59 2007 UTC (16 years, 8 months ago) by dpavlin
File size: 1885 byte(s)
 r1315@llin:  dpavlin | 2007-08-23 22:52:10 +0200
 create on-disk JSON file

1 package WebPAC::Output::JSON;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(path));
8
9 #use Encode qw/from_to/;
10 use Data::Dump qw/dump/;
11 use JSON;
12 use File::Slurp;
13
14 =head1 NAME
15
16 WebPAC::Output::JSON - Create JSON output
17
18 =head1 VERSION
19
20 Version 0.00
21
22 =cut
23
24 our $VERSION = '0.00';
25
26 =head1 SYNOPSIS
27
28 Create JSON output for export into other sistems
29
30 =head1 FUNCTIONS
31
32 =head2 new
33
34 my $output = new WebPAC::Output::JSON({
35 path => '/path/to/file.js',
36 });
37
38 Options are:
39
40 =over 4
41
42 =item path
43
44 path to JSON file
45
46 =back
47
48 =head2 init
49
50 $output->init;
51
52 =cut
53
54 sub init {
55 my $self = shift;
56 my $log = $self->_get_logger;
57
58 $log->debug('init');
59
60 $self->{_data} = [];
61
62 return 1;
63 }
64
65
66 =head2 add
67
68 Adds one entry to database.
69
70 $est->add( 42, $ds );
71
72 =cut
73
74 sub add {
75 my $self = shift;
76
77 my ( $id, $ds ) = @_;
78
79 my $log = $self->_get_logger;
80 $log->logdie("need id") unless defined $id;
81 $log->logdie("need ds") unless $ds;
82
83 $log->debug("id: $id ds = ",dump($ds));
84
85 my $item;
86
87 foreach my $t ( keys %$ds ) {
88 my $name = lc($t);
89 $name =~ s/\W+/_/g;
90
91 # FIXME get rid of non hash values in data_structure for consistency?
92 next unless ref($ds->{$t}) eq 'HASH';
93
94 if ( defined( $ds->{$t}->{display} ) ) {
95 $item->{$name} = $ds->{$t}->{display};
96 }
97 }
98
99 push @{ $self->{_data} }, $item;
100
101 return 1;
102 }
103
104 =head2 finish
105
106 $output->finish;
107
108 =cut
109
110 sub finish {
111 my $self = shift;
112
113 my $log = $self->_get_logger();
114
115 $log->info("writing JSON output to ", $self->path);
116 write_file( $self->path, objToJson( { items => $self->{_data} } ) );
117
118 }
119
120 =head1 AUTHOR
121
122 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
123
124 =head1 COPYRIGHT & LICENSE
125
126 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
127
128 This program is free software; you can redistribute it and/or modify it
129 under the same terms as Perl itself.
130
131 =cut
132
133 1; # End of WebPAC::Output::JSON

  ViewVC Help
Powered by ViewVC 1.1.26