Koha/C4/Output/JSONStream.pm
Lars Wirzenius 7279f55b60 Fix FSF address in directory C4/
Signed-off-by: Galen Charlton <gmcharlt@gmail.com>
2010-03-16 20:17:56 -04:00

74 lines
1.6 KiB
Perl

package C4::Output::JSONStream;
#
# Copyright 2008 LibLime
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=head1 NAME
C4::Output::JSONStream - progressively build JSON data
=head1 SYNOPSIS
my $json = new C4::Output::JSONStream;
$json->param( issues => [ 'yes!', 'please', 'no', { emphasis = 'NO' } ] );
$json->param( stuff => 'realia' );
print $json->output;
=head1 DESCRIPTION
This module allows you to build JSON incrementally.
=cut
use strict;
use warnings;
use JSON;
sub new {
my $class = shift;
my $self = {
data => {},
options => {}
};
bless $self, $class;
return $self;
}
sub param {
my $self = shift;
if ( @_ % 2 != 0 ) {
die 'param() received odd number of arguments (should be called with param => "value" pairs)';
}
for ( my $i = 0; $i < $#_; $i += 2 ) {
$self->{data}->{$_[$i]} = $_[$i + 1];
}
}
sub output {
my $self = shift;
return to_json( $self->{data} );
}
1;