]> Dogcows Code - chaz/p5-CGI-Ex/blobdiff - lib/CGI/Ex/JSONDump.pm
CGI::Ex 2.27
[chaz/p5-CGI-Ex] / lib / CGI / Ex / JSONDump.pm
index c08f496c98c2a7eb4e34229c6b9a26a0b8b47590..07ce9e99996e2f93e3ff82af7ad20a233440c70a 100644 (file)
@@ -17,7 +17,7 @@ use strict;
 use base qw(Exporter);
 
 BEGIN {
-    $VERSION  = '2.24';
+    $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,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 =~ /^ -? (?: [1-9][0-9]{0,12} | 0) (?: \. \d* [1-9])? $/x;
+    return $str if ! $no_num && $str =~ /^ -? (?: [1-9][0-9]{0,12} | 0) (?: \. \d* [1-9])? $/x;
 
     my $quote = $self->{'single_quote'} ? "'" : '"';
 
@@ -142,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
 
This page took 0.021594 seconds and 4 git commands to generate.