]> Dogcows Code - chaz/p5-CGI-Ex/blobdiff - lib/CGI/Ex/JSONDump.pm
CGI::Ex 2.32
[chaz/p5-CGI-Ex] / lib / CGI / Ex / JSONDump.pm
index 03ddce970d94e995e67ece8e7d3eb15fa5a66877..b45b11bd67b1ae1ceee203ac4a381a06219ed770 100644 (file)
@@ -17,7 +17,7 @@ use strict;
 use base qw(Exporter);
 
 BEGIN {
-    $VERSION  = '2.07';
+    $VERSION  = '2.32';
 
     @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,11 +99,11 @@ 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 =~ /^ -? (?: [0-9]{0,13} \. \d* [1-9] | [1-9][0-9]{0,12}) $/x;
+    return $str if ! $no_num && $str =~ /^ -? (?: [1-9][0-9]{0,12} | 0) (?: \. \d* [1-9])? $/x;
 
     my $quote = $self->{'single_quote'} ? "'" : '"';
 
@@ -117,7 +117,8 @@ sub js_escape {
     utf8::decode($str) if $self->{'utf8'} && &utf8::decode;
 
     ### escape <html> and </html> 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
 
@@ -359,10 +360,39 @@ greater than 80 characters.  Default is "\n" (if pretty is true).
 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<< <html> >>,
+C<< </html> >>, C<< <script> >>, C<< </script> >>, C<< <!-- >>, or , 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("<html><!-- comment --><script></script></html>");
+
+    Would print
+
+    "<htm"+"l><!-"+"- comment --"+"><scrip"+"t></scrip"+"t></htm"+"l>"
+
+    With the flag
+
+    JSONDump("<html><!-- comment --><script></script></html>", {no_tag_splitting => 1});
+
+    Would print
+
+    "<html><!-- comment --><script></script></html>"
+
 =back
 
+=head1 LICENSE
+
+This module may distributed under the same terms as Perl itself.
+
 =head1 AUTHORS
 
-Paul Seamons <paul at seamons dot com>
+Paul Seamons <perl at seamons dot com>
 
 =cut
This page took 0.023391 seconds and 4 git commands to generate.