Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created January 18, 2014 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/8498402 to your computer and use it in GitHub Desktop.
Save brianmed/8498402 to your computer and use it in GitHub Desktop.
Mojo::JSON::j exception
diff --git a/lib/Mojo/JSON.pm b/lib/Mojo/JSON.pm
index cc5c6e3..1b83a27 100644
--- a/lib/Mojo/JSON.pm
+++ b/lib/Mojo/JSON.pm
@@ -102,8 +102,22 @@ sub false {$FALSE}
sub j {
my $d = shift;
- return __PACKAGE__->new->encode($d) if ref $d eq 'ARRAY' || ref $d eq 'HASH';
- return __PACKAGE__->new->decode($d);
+ my $json = __PACKAGE__->new;
+
+ my $res;
+ if (ref $d eq 'ARRAY' || ref $d eq 'HASH') {
+ $res = $json->encode($d);
+ }
+ else {
+ $res = $json->decode($d);
+ }
+
+ if ($json->error) {
+ die($json->error);
+ }
+ else {
+ return($res);
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment