From: Ian Jackson Date: Fri, 14 Aug 2009 19:41:54 +0000 (+0100) Subject: website copes with older JSON.pm X-Git-Tag: 3.4~188 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.main.git;a=commitdiff_plain;h=734c62d8185e721a576750ce5aa1182f04d1de9e website copes with older JSON.pm --- 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",'.