[pve-devel] [PATCH common] properly encode YAML via YAML::XS

Fabian Grünbichler f.gruenbichler at proxmox.com
Thu Sep 17 13:16:58 CEST 2020


otherwise we get strange errors when formatting data that was originally
JSON, and can thus contain JSON::true/JSON::false.

one example is the QMP query-blockstats command, which gets called (and
the resulting values returned) by /nodes/NODE/qemu/VMID/status/current

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---

Notes:
    alternatives include:
    - dropping --output-format yaml altogether
    - manually recursively mapping JSON::true/false to some sensible value before dumping
    - outputting JSON instead of YAML, since the former is a subset of the latter (thanks Dominik ;))
    
    https://forum.proxmox.com/threads/yaml-issues-with-pvesh.76017/

 debian/control          | 1 +
 src/PVE/CLIFormatter.pm | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/debian/control b/debian/control
index 4aa95ed..d7508f5 100644
--- a/debian/control
+++ b/debian/control
@@ -34,6 +34,7 @@ Depends: libclone-perl,
          libtimedate-perl,
          liburi-perl,
          libwww-perl,
+         libyaml-libyaml-perl,
          ${misc:Depends},
          ${perl:Depends},
 Breaks: ifupdown2 (<< 2.0.1-1+pve5),
diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
index 4f18fa9..ccecfc3 100644
--- a/src/PVE/CLIFormatter.pm
+++ b/src/PVE/CLIFormatter.pm
@@ -5,7 +5,8 @@ use warnings;
 
 use I18N::Langinfo;
 use POSIX qw(strftime);
-use CPAN::Meta::YAML; # comes with perl-modules
+use YAML::XS; # supports Dumping JSON::PP::Boolean
+$YAML::XS::Boolean = "JSON::PP";
 
 use PVE::JSONSchema;
 use PVE::PTY;
@@ -87,7 +88,7 @@ PVE::JSONSchema::register_renderer('bytes', \&render_bytes);
 sub render_yaml {
     my ($value) = @_;
 
-    my $data = CPAN::Meta::YAML::Dump($value);
+    my $data = YAML::XS::Dump($value);
     $data =~ s/^---[\n\s]//; # remove yaml marker
 
     return $data;
@@ -440,7 +441,7 @@ sub print_api_result {
     }
 
     if ($format eq 'yaml') {
-	print encode('UTF-8', CPAN::Meta::YAML::Dump($data));
+	print encode('UTF-8', YAML::XS::Dump($data));
     } elsif ($format eq 'json') {
 	# Note: we always use utf8 encoding for json format
 	print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
-- 
2.20.1






More information about the pve-devel mailing list