Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

explain what an undef return value means

  • Loading branch information...
commit 349482bb0cc63c8ddeff5af9e33757c1704c37a1 1 parent 8d2fe50
@kraih authored
Showing with 15 additions and 11 deletions.
  1. +7 −4 lib/Mojo/JSON.pm
  2. +8 −7 lib/Mojo/Message.pm
View
11 lib/Mojo/JSON.pm
@@ -310,8 +310,9 @@ Mojo::JSON - Minimalistic JSON
# Handle errors
my $json = Mojo::JSON->new;
- if (defined(my $hash = $json->decode($bytes))) { say $hash->{message} }
- elsif (my $err = $json->error) { say "Error: $err" }
+ my $hash = $json->decode($bytes);
+ my $err = $json->error;
+ say $err ? "Error: $err" : $hash->{message};
=head1 DESCRIPTION
@@ -369,7 +370,8 @@ Encode Perl value to JSON.
my $value = j($bytes);
Encode Perl data structure (which may only be an C<Array> reference or C<Hash>
-reference) to JSON or decode JSON and return C<undef> if decoding fails.
+reference) to JSON or decode JSON, an C<undef> return value indicates a bare
+C<null> or that decoding failed.
=head1 ATTRIBUTES
@@ -391,7 +393,8 @@ following new ones.
my $value = $json->decode($bytes);
-Decode JSON to Perl value and return C<undef> if decoding fails.
+Decode JSON to Perl value, an C<undef> return value indicates a bare C<null>
+or that decoding failed.
=head2 encode
View
15 lib/Mojo/Message.pm
@@ -143,7 +143,7 @@ sub is_limit_exceeded { !!shift->{limit} }
sub json {
my ($self, $pointer) = @_;
return undef if $self->content->is_multipart;
- my $data = $self->{json} ||= j($self->body);
+ my $data = $self->{json} //= j($self->body);
return $pointer ? Mojo::JSON::Pointer->new($data)->get($pointer) : $data;
}
@@ -570,12 +570,13 @@ Check if message has exceeded L</"max_line_size"> or L</"max_message_size">.
my $value = $msg->json;
my $value = $msg->json('/foo/bar');
-Decode JSON message body directly using L<Mojo::JSON> if possible, returns
-C<undef> otherwise. An optional JSON Pointer can be used to extract a specific
-value with L<Mojo::JSON::Pointer>. Note that this method caches all data, so
-it should not be called before the entire message body has been received.
-The whole message body needs to be loaded into memory to parse it, so you have
-to make sure it is not excessively large.
+Decode JSON message body directly using L<Mojo::JSON> if possible, an C<undef>
+return value indicates a bare C<null> or that decoding failed. An optional
+JSON Pointer can be used to extract a specific value with
+L<Mojo::JSON::Pointer>. Note that this method caches all data, so it should
+not be called before the entire message body has been received. The whole
+message body needs to be loaded into memory to parse it, so you have to make
+sure it is not excessively large.
# Extract JSON values
say $msg->json->{foo}{bar}[23];
Please sign in to comment.
Something went wrong with that request. Please try again.