-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now you can send JSON payloads as an actual body in a PUT or POST request, instead of only decoding a specific POST param, or whatever it was doing before. Also, if a non-JSON mime-type is sent it will refuse to try and parse the data, instead only returning the default value. This to me seems like a much more logical usage of Input::json() than the initial implementation.
- Loading branch information
Phil Sturgeon
committed
Feb 20, 2013
1 parent
075781c
commit cd9fcd0
Showing
1 changed file
with
11 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,15 +379,21 @@ public function replace(array $input) | |
/** | ||
* Get the JSON payload for the request. | ||
* | ||
* @return object | ||
* @param string $key | ||
* @param mixed $default | ||
* @return string | ||
*/ | ||
public function json() | ||
public function json($key = null, $default = null) | ||
{ | ||
$arguments = func_get_args(); | ||
$mime = $this->retrieveItem('server', 'CONTENT_TYPE', null); | ||
|
||
array_unshift($arguments, $this->getContent()); | ||
if (strpos($mime, '/json') === false) { | ||
return $default; | ||
} | ||
|
||
$json = json_decode($this->getContent(), true); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
philsturgeon
|
||
|
||
return call_user_func_array('json_decode', $arguments); | ||
return array_get($json, $key, $default); | ||
} | ||
|
||
/** | ||
|
2 comments
on commit cd9fcd0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this return an object rather than an array?
For example, for the following request payload:
{"name":"John", "age":30}
The expected behavior should be:
$person = Input::json();
$person->name; // doesn't work
$person['name']; // works, but the request payload was never an array, it was an object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but if you try picking a specific item it would complicate matters checking for object then checking array. It would also ruin dot notation.
Guess it could do both and only support dot notation on arrays. There's no perfect answer, but if docs says "it's an array" then there is little room for confusion.
…Sent from my iPhone
On Feb 27, 2013, at 4:25 AM, aleemb ***@***.*** wrote:
Shouldn't this return an object rather than an array?
For example, for the following request payload:
{"name":"John", "age":30}
The expected behavior should be:
$person = Input::json();
$person->name; // doesn't work
$person['name']; // works, but the request payload was never an array, it was an object
—
Reply to this email directly or view it on GitHub.
Is there any chance you make the second parameter of the
json_decode()
optional and changable likeRequest::json(null, null, false);
in order to not convert the objects to array?