From 734c62d8185e721a576750ce5aa1182f04d1de9e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 14 Aug 2009 20:41:54 +0100 Subject: [PATCH] website copes with older JSON.pm --- yarrg/CommodsWeb.pm | 15 +++++++++++++-- yarrg/web/routetextstring | 6 +++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/yarrg/CommodsWeb.pm b/yarrg/CommodsWeb.pm index 5f85511..d423a29 100644 --- a/yarrg/CommodsWeb.pm +++ b/yarrg/CommodsWeb.pm @@ -52,7 +52,7 @@ BEGIN { $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(&dbw_connect &ocean_list $sourcebasedir - &to_json &to_json_protecttags + &to_json_shim &to_json_protecttags &set_ctype_utf8); %EXPORT_TAGS = ( ); @@ -103,9 +103,20 @@ sub dbw_connect ($) { return dbr_connect($datadir, $ocean); } +sub to_json_shim ($) { + my ($obj) = @_; + # In JSON.pm 2.x, jsonToObj prints a warning to stderr which + # our callers don't like at all. + if ($JSON::VERSION >= 2.0) { + return to_json($obj); + } else { + return objToJson($obj); + } +} + sub to_json_protecttags ($) { my ($v) = @_; - my $j= to_json($v); + my $j= to_json_shim($v); $j =~ s,/,\\/,g; return $j; } diff --git a/yarrg/web/routetextstring b/yarrg/web/routetextstring index 9107780..be009d4 100755 --- a/yarrg/web/routetextstring +++ b/yarrg/web/routetextstring @@ -69,12 +69,12 @@ my ($output, $output_wrong); if ($format =~ /json/) { $r->content_type($ctype or $format); - $output= sub { print to_json({ + $output= sub { print to_json_shim({ success => 1, show => length $canontext ? encode_entities($canontext) : ' ', })}; - $output_wrong= sub { print to_json({ + $output_wrong= sub { print to_json_shim({ success => 0, show => $_[0], })}; @@ -108,7 +108,7 @@ foreach my $each (split m#[/|,]#, $string) { if (!$results) { if (!%m) { return $err->('no island or arch matches "%s"'); - } elsif (%m > 5) { + } elsif (keys(%m) > 5) { return $err->(' '); } else { return $err->('ambiguous island or arch "%s",'. -- 2.30.2