texlive[45723] Master/tlpkg/TeXLive: add home-made json conversion

commits+preining at tug.org commits+preining at tug.org
Thu Nov 9 04:24:09 CET 2017


Revision: 45723
          http://tug.org/svn/texlive?view=revision&revision=45723
Author:   preining
Date:     2017-11-09 04:24:09 +0100 (Thu, 09 Nov 2017)
Log Message:
-----------
add home-made json conversion

Modified Paths:
--------------
    trunk/Master/tlpkg/TeXLive/TLPOBJ.pm
    trunk/Master/tlpkg/TeXLive/TLUtils.pm

Modified: trunk/Master/tlpkg/TeXLive/TLPOBJ.pm
===================================================================
--- trunk/Master/tlpkg/TeXLive/TLPOBJ.pm	2017-11-09 01:32:04 UTC (rev 45722)
+++ trunk/Master/tlpkg/TeXLive/TLPOBJ.pm	2017-11-09 03:24:09 UTC (rev 45723)
@@ -468,6 +468,11 @@
   }
   $foo{'docfiles'} = [ @newdocf ];
   delete($foo{'docfiledata'});
+  #
+  # my home-made solution is a bit faster then JSON::PP
+  # but we still prefer that one for security ;-)
+  # If JSON::XS is installed, that is the fastest.
+  #my $utf8_encoded_json_text = TeXLive::TLUtils::hash_to_json(\%foo);
   my $utf8_encoded_json_text = JSON::encode_json(\%foo);
   return $utf8_encoded_json_text;
 }

Modified: trunk/Master/tlpkg/TeXLive/TLUtils.pm
===================================================================
--- trunk/Master/tlpkg/TeXLive/TLUtils.pm	2017-11-09 01:32:04 UTC (rev 45722)
+++ trunk/Master/tlpkg/TeXLive/TLUtils.pm	2017-11-09 03:24:09 UTC (rev 45723)
@@ -101,6 +101,13 @@
   TeXLive::TLUtils::prepend_own_path();
   TeXLive::TLUtils::repository_to_array($str);
 
+=head2 JSON
+
+  TeXLive::TLUtils::simple_hash_to_json($hashref);
+  TeXLive::TLUtils::scalar_to_json($hashref);
+  TeXLive::TLUtils::hash_to_json($hashref);
+  TeXLive::TLUtils::array_to_json($hashref);
+
 =head1 DESCRIPTION
 
 =cut
@@ -188,6 +195,7 @@
     &get_full_line
     &sort_archs
     &repository_to_array
+    &simple_hash_to_json
   );
   @EXPORT = qw(setup_programs download_file process_logging_options
                tldie tlwarn info log debug ddebug dddebug debug_hash
@@ -4131,9 +4139,6 @@
 }
 
 
-=back
-=cut
-
 sub repository_to_array {
   my $r = shift;
   my %r;
@@ -4159,7 +4164,93 @@
   return %r;
 }
 
+=item C<simple_hash_tpo_json($hashref)>
 
+Returns the JSON representation of C<$hashref>. Only simple key/value
+hashes are supported, and all entries are treated as strings.
+
+=cut
+
+sub simple_hash_to_json {
+  my $hr = shift;
+  return 
+    "{" . join (",", map { "\"$_\":\"" . $hr->{$_} . "\"" } keys(%$hr)) . "}"
+}
+
+sub scalar_to_json {
+  my $val = shift;
+  my %esc = (
+    "\n" => '\n',
+    "\r" => '\r',
+    "\t" => '\t',
+    "\f" => '\f',
+    "\b" => '\b',
+    "\"" => '\"',
+    "\\" => '\\\\',
+    "\'" => '\\\'',
+  );
+  $val =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g;
+  return($val);
+}
+
+sub hash_to_json {
+  my $hr = shift;
+  my $ret = "{";
+  my @keystr;
+  for my $k (keys(%$hr)) {
+    my $val = $hr->{$k};
+    if (!$val) {
+      push @keystr, "\"$k\":null";
+    } elsif (ref($val) eq "") {
+      push @keystr, "\"$k\":\"" . scalar_to_json($val) . "\"";
+    } elsif (ref($val) eq 'SCALAR') {
+      push @keystr, "\"$k\":\"" . scalar_to_json($$val) . "\"";
+    } elsif (ref($val) eq 'ARRAY') {
+      push @keystr, "\"$k\":" . array_to_json($val);
+    } elsif (ref($val) eq 'HASH') {
+      push @keystr, "\"$k\":" . hash_to_json($val);
+    } elsif (Scalar::Util::blessed($val)) {
+      if (ref($val) eq "JSON::PP::Boolean") {
+        push @keystr, "\"$k\":" . ($val == $JSON::true ? "true" : "false");
+      } else {
+        die "Unsupported blessed object!";
+      }
+    } else {
+      die("Unsupported reference in hash_to_json: " . ref($val));
+    }
+  }
+  $ret .= join(",", at keystr) . "}";
+  return($ret);
+}
+
+sub array_to_json {
+  my $hr = shift;
+  my $ret = "[";
+  my @keystr;
+  for my $val (@$hr) {
+    if (!$val) {
+      push @keystr, "null";
+    } elsif (ref($val) eq "") {
+      push @keystr, "\"" . scalar_to_json($val) . "\"";
+    } elsif (ref($val) eq 'SCALAR') {
+      push @keystr, "\"" . scalar_to_json($$val) . "\"";
+    } elsif (ref($val) eq 'ARRAY') {
+      push @keystr, array_to_json($val);
+    } elsif (ref($val) eq 'HASH') {
+      push @keystr, hash_to_json($val);
+    } else {
+      die("Unsupported reference in array_to_json");
+    }
+  }
+  $ret .= join(",", at keystr) . "]";
+  return($ret);
+}
+
+
+
+=back
+=cut
+
 1;
 __END__
 



More information about the tex-live-commits mailing list