Skip to content

Commit

Permalink
initial import of CGI::Application::Plugin::JSON to git
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeters committed Mar 1, 2009
0 parents commit e04fbed
Show file tree
Hide file tree
Showing 12 changed files with 655 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Build.PL
@@ -0,0 +1,22 @@
use strict;
use warnings;
use Module::Build;
use lib './t/lib';

my $builder = Module::Build->new(
module_name => 'CGI::Application::Plugin::JSON',
license => 'perl',
dist_author => 'Michael Peters <mpeters@plusthree.com>',
requires => {
'Test::More' => 0,
'CGI::Application' => 4.00,
'JSON::Any' => 1.14,
},
build_requires => {
JSON => '2.02',
},
create_makefile_pl => 'passthrough',
create_readme => 1,
);

$builder->create_build_script();
21 changes: 21 additions & 0 deletions Changes
@@ -0,0 +1,21 @@
Revision history for CGI::Application::Plugin::JSON

1.01 Jan 03, 2007
- Functionally the same as 1.00, but made some changes to Build.PL
so that it installs even if there are no JSON modules but JSON::Any
is installed

1.00 Dec 31, 2007
- using JSON::Any instead of just JSON so I don't have to worry about
API changes and you can use faster JSON implementations (JSON::Syck, JSON::XS)
if you have them installed

0.03 Sept 17, 2007
- added json_callback() method

0.02 Sept 23, 2006
- Tidied up docs and released to CPAN

0.01 Sept 14, 2006
- First version, released on an unsuspecting world.

12 changes: 12 additions & 0 deletions MANIFEST
@@ -0,0 +1,12 @@
Build.PL
Changes
lib/CGI/Application/Plugin/JSON.pm
Makefile.PL
MANIFEST This list of files
META.yml
README
t/00-pod.t
t/01-pod-coverage.t
t/02-json.t
t/lib/MyApp.pm
TODO
23 changes: 23 additions & 0 deletions META.yml
@@ -0,0 +1,23 @@
---
name: CGI-Application-Plugin-JSON
version: 1.01
author:
- 'Michael Peters <mpeters@plusthree.com>'
abstract: easy manipulation of JSON headers
license: perl
resources:
license: http://dev.perl.org/licenses/
requires:
CGI::Application: 4
JSON::Any: 1.14
Test::More: 0
build_requires:
JSON: 2.02
provides:
CGI::Application::Plugin::JSON:
file: lib/CGI/Application/Plugin/JSON.pm
version: 1.01
generated_by: Module::Build version 0.2808
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.2.html
version: 1.2
32 changes: 32 additions & 0 deletions Makefile.PL
@@ -0,0 +1,32 @@
# Note: this file was auto-generated by Module::Build::Compat version 0.03

unless (eval "use Module::Build::Compat 0.02; 1" ) {
print "This module requires Module::Build to install itself.\n";

require ExtUtils::MakeMaker;
my $yn = ExtUtils::MakeMaker::prompt
(' Install Module::Build now from CPAN?', 'y');

unless ($yn =~ /^y/i) {
die " *** Cannot install without Module::Build. Exiting ...\n";
}

require Cwd;
require File::Spec;
require CPAN;

# Save this 'cause CPAN will chdir all over the place.
my $cwd = Cwd::cwd();

CPAN::Shell->install('Module::Build::Compat');
CPAN::Shell->expand("Module", "Module::Build::Compat")->uptodate
or die "Couldn't install Module::Build, giving up.\n";

chdir $cwd or die "Cannot chdir() back to $cwd: $!";
}
eval "use Module::Build::Compat 0.02; 1" or die $@;

Module::Build::Compat->run_build_pl(args => \@ARGV);
my $build_pkg = eval { require Module::Build }
? 'Module::Build' : 'Module::Build';
Module::Build::Compat->write_makefile(build_class => $build_pkg)
117 changes: 117 additions & 0 deletions README
@@ -0,0 +1,117 @@
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();

# 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 ] } );

DESCRIPTION
When communicating with client-side JavaScript, it is common to send
data in "X-JSON" HTTP headers or through the document body as
content-type "text/x-json".

This plugin adds a couple of convenience methods to make that just a
little bit easier.

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

BODY METHODS
json_body
This method will take the given Perl structure, turn it into JSON, set
the appropriate content-type, and then return the JSON.

return $self->json_body({ foo => 'stuff', bar => [0,1,2,3]} );

json_callback
This method will take the given Perl structure, turn it into JSON, set
the appropriate content-type, and then return a Javascript snippet where
the given callback is called with the resulting JSON.

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]});

to_json
This method is just a convenient wrapper around JSON::Any's "encode".

from_json
This method is just a convenient wrapper around JSON::Any's "decode".

AUTHOR
Michael Peters, "<mpeters@plusthree.com>"

BUGS
Please report any bugs or feature requests to
"bug-cgi-application-plugin-viewsource@rt.cpan.org", or through the web
interface at
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-J
SON>. I will be notified, and then you'll automatically be notified of
progress on your bug as I make changes.

COPYRIGHT & LICENSE
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.

Empty file added TODO
Empty file.

0 comments on commit e04fbed

Please sign in to comment.