Koha/C4/Output/JSONStream.pm
Pianohacker b04f432857 New framework for AJAX services
This adds two new C4 modules, C4::Service and ::Output::JSONStream, and
makes important modifications to C4::Output. The first two are a basic
framework for JSON-based AJAX services and a simple JSON output wrapper,
respectively. C4::Output has been slightly refactored, with a new
function, output_with_http_headers, that supports different
content-types. output_html_with_http_headers still exists, and the three
pages affected by this change have been refactored to support it.

Signed-off-by: Galen Charlton <galen.charlton@liblime.com>
2009-05-14 07:29:18 -05: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., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 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;