Skip to content

Commit

Permalink
Added json encoding options to json response
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Feb 27, 2014
1 parent 29decb7 commit 417f539
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/Illuminate/Http/JsonResponse.php
Expand Up @@ -5,6 +5,28 @@

class JsonResponse extends \Symfony\Component\HttpFoundation\JsonResponse {

/**
* The json encoding options.
*
* @var int
*/
protected $jsonOptions;

/**
* Constructor.
*
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
*/
public function __construct($data = null, $status = 200, $headers = array(), $options = 0)
{
$this->jsonOptions = $options;

parent::__construct($data, $status, $headers);
}

/**
* Get the json_decoded data from the response
*
Expand All @@ -13,8 +35,11 @@ class JsonResponse extends \Symfony\Component\HttpFoundation\JsonResponse {
* @param int $options
* @return mixed
*/
public function getData($assoc = false, $depth = 512, $options = 0)
public function getData($assoc = false, $depth = 512, $options = null)
{
if (is_null($options))
$options = $this->jsonOptions;

return json_decode($this->data, $assoc, $depth, $options);
}

Expand All @@ -23,7 +48,7 @@ public function getData($assoc = false, $depth = 512, $options = 0)
*/
public function setData($data = array())
{
$this->data = $data instanceof JsonableInterface ? $data->toJson() : json_encode($data);
$this->data = $data instanceof JsonableInterface ? $data->toJson($this->jsonOptions) : json_encode($data, $this->jsonOptions);

return $this->update();
}
Expand Down Expand Up @@ -56,4 +81,4 @@ public function withCookie(Cookie $cookie)
return $this;
}

}
}
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Facades/Response.php
Expand Up @@ -51,16 +51,17 @@ public static function view($view, $data = array(), $status = 200, array $header
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public static function json($data = array(), $status = 200, array $headers = array())
public static function json($data = array(), $status = 200, array $headers = array(), $options = 0)
{
if ($data instanceof ArrayableInterface)
{
$data = $data->toArray();
}

return new JsonResponse($data, $status, $headers);
return new JsonResponse($data, $status, $headers, $options);
}

/**
Expand Down

0 comments on commit 417f539

Please sign in to comment.