1CGI::Application::PlugiUns:e:rJSCOoNn(t3r)ibuted Perl DoCcGuIm:e:nAtpaptliiocnation::Plugin::JSON(3)
2
3
4
6 CGI::Application::Plugin::JSON - easy manipulation of JSON headers
7
9 use CGI::Application::Plugin::JSON ':all';
10
11 # add_json_header() is cumulative
12 $self->add_json_header( foo => 'Lorem ipsum...');
13 $self->add_json_header( bar => [ 0, 2, 3, 4 ] );
14 $self->add_json_header( baz => { stuff => 1, more_stuff => 2 } );
15
16 # json_header() is not cumulative
17 $self->json_header( foo => 'Lorem ipsum...');
18
19 # in case we're printing our own headers
20 print "X-JSON: " . $self->json_header_string();
21
22 # clear out everything in the outgoing JSON headers
23 $self->clear_json_header();
24
25 # or send the JSON in the document body
26 $self->json_body( { foo => 'Lorem ipsum', bar => [ 0, 2, 3 ] } );
27
28 # send the JSON back in the document body, but execute it using a Javascript callback
29 $self->json_callback('alert', { foo => 'Lorem ipsum', bar => [ 0, 2, 3 ] } );
30
32 When communicating with client-side JavaScript, it is common to send
33 data in "X-JSON" HTTP headers or through the document body as content-
34 type "application/json".
35
36 This plugin adds a couple of convenience methods to make that just a
37 little bit easier.
38
40 json_header
41 This method takes name-value pairs and sets them to be used in the
42 outgoing JSON. It is not cummulative and works similarly to
43 "header_props". Use it only if you have all of the values up front. In
44 most cases add_json_header is probably what you want.
45
46 # only the 2nd call will actually set data that will be sent
47 $self->json_header( foo => 'Lorem ipsum...');
48 $self->json_header( bar => [ 0, 2, 3, 4 ] );
49
50 add_json_header
51 This method takes name-value pairs and sets them to be used in the
52 outgoing JSON. It is cummulative and works similarly to "header_add";
53 meaning multiple calls will add to the hash of outgoing values.
54
55 # both 'foo' and 'bar' will exist in the hash sent out
56 $self->json_header( foo => 'Lorem ipsum...');
57 $self->json_header( bar => [ 0, 2, 3, 4 ] );
58
59 clear_json_header
60 This method will remove anything that was previously set by both
61 json_header and add_json_header. This means that no "X-JSON" header
62 will be sent.
63
64 json_header_string
65 This method will create the actual HTTP header string that will be sent
66 to the browser. This plugin uses it internally to send the header, but
67 it might be useful to use directly if you are printing your own HTTP
68 headers (using a "header_type" of "none").
69
70 $self->header_type('none');
71 print $self->json_header_string();
72
73 json_header_value
74 This method will return the values being sent in the JSON header. If
75 you pass in the key of the value you want, you will get just that
76 value. Else all name-value pairs will be returned.
77
78 my $value = $self->json_header_value('foo');
79
80 my %values = $self->json_header_value();
81
83 json_body
84 This method will take the given Perl structure, turn it into JSON, set
85 the appropriate content-type, and then return the JSON.
86
87 return $self->json_body({ foo => 'stuff', bar => [0,1,2,3]} );
88
89 json_callback
90 This method will take the given Perl structure, turn it into JSON, set
91 the appropriate content-type, and then return a Javascript snippet
92 where the given callback is called with the resulting JSON.
93
94 return $self->json_callback('alert', { foo => 'stuff', bar => [0,1,2,3]} );
95
96 # would result in something like the following being sent to the client
97 alert({ foo => 'stuff', bar => [0,1,2,3]});
98
100 to_json
101 This method is just a convenient wrapper around JSON::Any's "encode".
102
103 from_json
104 This method is just a convenient wrapper around JSON::Any's "decode".
105
107 Michael Peters, "<mpeters@plusthree.com>"
108
110 Please report any bugs or feature requests to
111 "bug-cgi-application-plugin-viewsource@rt.cpan.org", or through the web
112 interface at
113 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-JSON>.
114 I will be notified, and then you'll automatically be notified of
115 progress on your bug as I make changes.
116
118 Copyright 2006 Michael Peters, All Rights Reserved.
119
120 This program is free software; you can redistribute it and/or modify it
121 under the same terms as Perl itself.
122
123
124
125perl v5.32.1 2021-01-26 CGI::Application::Plugin::JSON(3)