Hello all,

I've found myself putting some very similar code into my base classes of late to
more easily deal with sending JSON to my Ajax applications. So of course I
thought I should put this out as a plugin. I haven't uploaded to CPAN yet cause
I wanted to see if anyone had any thoughts about the interface, etc.

NAME
    CGI::Application::Plugin::JSON - easy manipulation of JSON headers

SYNOPSIS
        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();

DESCRIPTION
    When communicating with client-side JavaScript, it is common to send
    data in "X-JSON" HTTP headers. This plugin adds a couple of convenience
    methods to make that just a little bit easier.

METHODS
  json_header
    This method takes name-value pairs and sets them to be used in the
    outgoing JSON. It is not cummulative and works similarly to
    "header_props". 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 ] );

  add_json_header
    This method takes name-value pairs and sets them to be used in the
    outgoing JSON. It is cummulative and works similarly to "header_add";
    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 ] );

  clear_json_header
    This method will remove anything that was previously set by both
    json_header and add_json_header. This means that no "X-JSON" header will
    be sent.

  json_header_string
    This method will create the actual HTTP 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 HTTP
    headers (using a "header_type" of "none").

        $self->header_type('none');
        print $self->json_header_string();

  json_header_value
    This method will return the values being sent in the JSOn 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();

-- 
Michael Peters
Developer
Plus Three, LP


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[email protected]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to