X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FCGI%2FEx%2FJSONDump.pm;h=07ce9e99996e2f93e3ff82af7ad20a233440c70a;hb=febed4ec71f803b083c3e61b82b9464e9bfb0992;hp=fe7c562b2fad2efaeb9e1cf475a6aecddb0e017f;hpb=d710d6cd21be21c0ab2df3566c2bd61d9015cac6;p=chaz%2Fp5-CGI-Ex
diff --git a/lib/CGI/Ex/JSONDump.pm b/lib/CGI/Ex/JSONDump.pm
index fe7c562..07ce9e9 100644
--- a/lib/CGI/Ex/JSONDump.pm
+++ b/lib/CGI/Ex/JSONDump.pm
@@ -7,7 +7,7 @@ CGI::Ex::JSONDump - Comprehensive data to JSON dump.
=cut
###----------------------------------------------------------------###
-# Copyright 2006 - Paul Seamons #
+# Copyright 2007 - Paul Seamons #
# Distributed under the Perl Artistic License without warranty #
###----------------------------------------------------------------###
@@ -17,7 +17,7 @@ use strict;
use base qw(Exporter);
BEGIN {
- $VERSION = '2.04';
+ $VERSION = '2.27';
@EXPORT = qw(JSONDump);
@EXPORT_OK = @EXPORT;
@@ -75,7 +75,7 @@ sub _dump {
return "{}" if ! @keys;
return "{$self->{hash_nl}${prefix}$self->{indent}"
. join(",$self->{hash_nl}${prefix}$self->{indent}",
- map { $self->js_escape($_, "${prefix}$self->{indent}")
+ map { $self->js_escape($_, "${prefix}$self->{indent}", 1)
. $self->{'hash_sep'}
. $self->_dump($data->{$_}, "${prefix}$self->{indent}") }
@keys)
@@ -99,17 +99,17 @@ sub _dump {
}
sub js_escape {
- my ($self, $str, $prefix) = @_;
+ my ($self, $str, $prefix, $no_num) = @_;
return 'null' if ! defined $str;
### allow things that look like numbers to show up as numbers (and those that aren't quite to not)
- return $str if $str =~ /^ -? (?: \d{0,13} \. \d* [1-9] | \d{1,13}) $/x;
+ return $str if ! $no_num && $str =~ /^ -? (?: [1-9][0-9]{0,12} | 0) (?: \. \d* [1-9])? $/x;
my $quote = $self->{'single_quote'} ? "'" : '"';
$str =~ s/\\/\\\\/g;
- $str =~ s/\r/\\\r/g;
- $str =~ s/\t/\\\t/g;
+ $str =~ s/\r/\\r/g;
+ $str =~ s/\t/\\t/g;
$self->{'single_quote'} ? $str =~ s/\'/\\\'/g : $str =~ s/\"/\\\"/g;
### allow for really odd chars
@@ -117,7 +117,8 @@ sub js_escape {
utf8::decode($str) if $self->{'utf8'} && &utf8::decode;
### escape and tags in the text
- $str =~ s{(? (?: htm | scrip | !-))}{$1$quote+$quote}gx;
+ $str =~ s{(? (?: htm | scrip | !-) | --(?=>) )}{$1$quote+$quote}gx
+ if ! $self->{'no_tag_splitting'};
### add nice newlines (unless pretty is off)
if ($self->{'str_nl'} && length($str) > 80) {
@@ -141,11 +142,11 @@ __END__
use CGI::Ex::JSONDump;
- my $js = JSONDump(\%complex_data, {pretty => 0});
+ my $js = JSONDump(\%complex_data, {pretty => 1});
### OR
- my $js = CGI::Ex::JSONDump->new({pretty => 0})->dump(\%complex_data);
+ my $js = CGI::Ex::JSONDump->new({pretty => 1})->dump(\%complex_data);
=head1 DESCRIPTION
@@ -228,7 +229,7 @@ include whitespace to make them more readable.
{
"a" : [
1,
- 2,
+ 2
]
}
@@ -238,7 +239,7 @@ include whitespace to make them more readable.
with single quotes. Otherwise values are quoted with double quotes.
JSONDump("a", {single_quote => 0});
- JSONDump('a', {single_quote => 0});
+ JSONDump("a", {single_quote => 1});
Would print
@@ -249,7 +250,7 @@ with single quotes. Otherwise values are quoted with double quotes.
0 or 1. Default 1 (true)
-If true, then key/value pairs of hashrefs will be sorted will be output in sorted order.
+If true, then key/value pairs of hashrefs will be output in sorted order.
=item play_coderefs
@@ -294,9 +295,10 @@ with unknown types will not be included in the javascript output.
=item skip_keys
-Should contain an arrayref of keys or a hashref whose keys are the keys to skip. Default
-is unset. Any keys of hashrefs that are in the skip_keys item will not be included in
-the javascript output.
+Should contain an arrayref of keys or a hashref whose keys are the
+keys to skip. Default is unset. Any keys of hashrefs (including
+nested hashrefs) that are listed in the skip_keys item will not be included
+in the javascript output.
JSONDump({a => 1, b => 1}, {skip_keys => ['a'], pretty => 0});
@@ -306,8 +308,9 @@ the javascript output.
=item skip_keys_qr
-Similar to skip_keys but should contain a regex. Any keys of hashrefs that match the
-skip_keys_qr regex will not be included in the javascript output.
+Similar to skip_keys but should contain a regex. Any keys of hashrefs
+(including nested hashrefs) that match the skip_keys_qr regex will not
+be included in the javascript output.
JSONDump({a => 1, _b => 1}, {skip_keys_qr => qr/^_/, pretty => 0});
@@ -317,7 +320,7 @@ skip_keys_qr regex will not be included in the javascript output.
=item indent
-The level to indent each nested data structure level if pretty is true. Default is " ".
+The level to indent each nested data structure level if pretty is true. Default is " " (two spaces).
=item hash_nl
@@ -334,11 +337,11 @@ The whitespace to add after each arrayref entry if pretty is true. Default is "
=item str_nl
The whitespace to add in between newline separated strings if pretty is true or the output line is
-greater than 80 characters. Default is "\n".
+greater than 80 characters. Default is "\n" (if pretty is true).
JSONDump("This is a long string\n"
."with plenty of embedded newlines\n"
- ."and is greater than 80 characters.\n", {pretty => 1, str_nl => "\n"});
+ ."and is greater than 80 characters.\n", {pretty => 1});
Would print
@@ -346,13 +349,50 @@ greater than 80 characters. Default is "\n".
+"with plenty of embedded newlines\n"
+"and is greater than 80 characters.\n"
-If the string is less than 80 characters, or if str_nl is set to '', then the escaped
-string will be contained on a single line.
+ JSONDump("This is a long string\n"
+ ."with plenty of embedded newlines\n"
+ ."and is greater than 80 characters.\n", {pretty => 1, str_nl => ""});
+
+ Would print
+
+ "This is a long string\nwith plenty of embedded newlines\nand is greater than 80 characters.\n"
+
+If the string is less than 80 characters, or if str_nl is set to "", then the escaped
+string will be contained on a single line. Setting pretty to 0 effectively sets str_nl equal to "".
+
+=item no_tag_splitting
+
+Default off. If JSON is embedded in an HTML document and the JSON contains C<< >>,
+C<< >>, C<< >>, C<< >> tags, they are
+split apart with a quote, a +, and a quote. This allows the embedded tags to not affect
+the currently playing JavaScript.
+
+However, if the JSON that is output is intended for deserialization by another non-javascript-engine
+JSON parser, this splitting behavior may cause errors when the JSON is imported. To avoid the splitting
+behavior in these cases you can use the no_tag_splitting flag to turn off the behavior.
+
+ JSONDump("");
+
+ Would print
+
+ ""
+
+ With the flag
+
+ JSONDump("", {no_tag_splitting => 1});
+
+ Would print
+
+ ""
=back
+=head1 LICENSE
+
+This module may distributed under the same terms as Perl itself.
+
=head1 AUTHORS
-Paul Seamons
+Paul Seamons
=cut