Easy manipulation of json headers
use CGI::Application::Plugin::JSON ':all'; # add_json_header() is cumulative $self->add_json_header( foo => 'Lorem ipsum...'); $self->add_json_header( bar => [ 0, 2, 3, 4 ] ); $self->add_json_header( baz => { stuff => 1, more_stuff => 2 } ); # json_header() is not cumulative $self->json_header( foo => 'Lorem ipsum...'); # in case we're printing our own headers print "X-JSON: " . $self->json_header_string(); # clear out everything in the outgoing JSON headers $self->clear_json_header(); # or send the JSON in the document body $self->json_body( { foo => 'Lorem ipsum', bar => [ 0, 2, 3 ] } ); # send the JSON back in the document body, but execute it using a Javascript callback $self->json_callback('alert', { foo => 'Lorem ipsum', bar => [ 0, 2, 3 ] } );
When communicating with client-side JavaScript, it is common to send data in \*(C`X-JSON\*(C' \s-1HTTP\s0 headers or through the document body as content-type \*(C`application/json\*(C'.
This plugin adds a couple of convenience methods to make that just a little bit easier.
This method takes name-value pairs and sets them to be used in the outgoing \s-1JSON\s0. It is not cummulative and works similarly to \*(C`header_props\*(C'. Use it only if you have all of the values up front. In most cases add_json_header is probably what you want.
# only the 2nd call will actually set data that will be sent $self->json_header( foo => 'Lorem ipsum...'); $self->json_header( bar => [ 0, 2, 3, 4 ] );
This method takes name-value pairs and sets them to be used in the outgoing \s-1JSON\s0. It is cummulative and works similarly to \*(C`header_add\*(C'; meaning multiple calls will add to the hash of outgoing values.
# both 'foo' and 'bar' will exist in the hash sent out $self->json_header( foo => 'Lorem ipsum...'); $self->json_header( bar => [ 0, 2, 3, 4 ] );
This method will remove anything that was previously set by both json_header and add_json_header. This means that no \*(C`X-JSON\*(C' header will be sent.
This method will create the actual \s-1HTTP\s0 header string that will be sent to the browser. This plugin uses it internally to send the header, but it might be useful to use directly if you are printing your own \s-1HTTP\s0 headers (using a \*(C`header_type\*(C' of \*(C`none\*(C').
$self->header_type('none'); print $self->json_header_string();
This method will return the values being sent in the \s-1JSON\s0 header. If you pass in the key of the value you want, you will get just that value. Else all name-value pairs will be returned.
my $value = $self->json_header_value('foo');
my %values = $self->json_header_value();
This method will take the given Perl structure, turn it into \s-1JSON\s0, set the appropriate content-type, and then return the \s-1JSON\s0.
return $self->json_body({ foo => 'stuff', bar => [0,1,2,3]} );
This method will take the given Perl structure, turn it into \s-1JSON\s0, set the appropriate content-type, and then return a Javascript snippet where the given callback is called with the resulting \s-1JSON\s0.
return $self->json_callback('alert', { foo => 'stuff', bar => [0,1,2,3]} );
# would result in something like the following being sent to the client alert({ foo => 'stuff', bar => [0,1,2,3]});
This method is just a convenient wrapper around JSON::Any's \*(C`encode\*(C'.
This method is just a convenient wrapper around JSON::Any's \*(C`decode\*(C'.
Michael Peters, \*(C`<[email protected]>\*(C'
Please report any bugs or feature requests to \*(C`[email protected]\*(C', or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-JSON <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-JSON>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
Copyright 2006 Michael Peters, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.