From: Graham Knop <haarg@haarg.org>
Date: Fri, 11 Dec 2015 11:54:37 +0000 (-0500)
Subject: add prototype to decode_json when using JSON::PP
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fpp-proto;p=p5sagit%2FJSON-MaybeXS.git

add prototype to decode_json when using JSON::PP

JSON::PP's decode_json function doesn't have a prototype, unlike
Cpanel::JSON::XS and JSON::XS.  We always want to export the same
prototypes, so wrap JSON::PP::decode_json in a prototyped sub.
---

diff --git a/lib/JSON/MaybeXS.pm b/lib/JSON/MaybeXS.pm
index 6898131..8910452 100644
--- a/lib/JSON/MaybeXS.pm
+++ b/lib/JSON/MaybeXS.pm
@@ -28,7 +28,20 @@ sub _choose_json_module {
 
 BEGIN {
     our $JSON_Class = _choose_json_module();
-    $JSON_Class->import(qw(encode_json decode_json));
+
+    if ($JSON_Class eq 'JSON::PP') {
+        $JSON_Class->import(qw(encode_json));
+        eval '#line ' . __LINE__ . ' "' . __FILE__ . '"' . q{
+            package JSON::PP;
+            sub JSON::MaybeXS::decode_json ($) {
+                &decode_json;
+            }
+            1;
+        } or die $@;
+    }
+    else {
+        $JSON_Class->import(qw(encode_json decode_json));
+    }
 }
 
 our @EXPORT = qw(encode_json decode_json JSON);
diff --git a/t/pp.t b/t/pp.t
index 9a74a9e..5520a64 100644
--- a/t/pp.t
+++ b/t/pp.t
@@ -15,10 +15,15 @@ is(
   'Correct encode_json function'
 );
 
-is(
-  \&decode_json, \&JSON::PP::decode_json,
-  'Correct encode_json function'
-);
+is prototype \&decode_json, '$',
+  'decode_json has correct prototype';
+
+is_deeply decode_json '[]', [],
+  'decode_json works as expected';
+
+eval { decode_json undef }; my $msg = ' at ' . __FILE__ . ' line ' . __LINE__;
+like $@, qr/\Q$msg\E/,
+  'decode_json reports error at correct location';
 
 require 't/lib/is_bool.pm';