Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 475e7ca

Browse files
committed
Merge pull request laravel#3660 from GrahamCampbell/4.1-json
Allow Json Encoding Options To Be Set On The Json Response
2 parents 4425edc + 417f539 commit 475e7ca

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/Illuminate/Http/JsonResponse.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55

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

8+
/**
9+
* The json encoding options.
10+
*
11+
* @var int
12+
*/
13+
protected $jsonOptions;
14+
15+
/**
16+
* Constructor.
17+
*
18+
* @param mixed $data
19+
* @param int $status
20+
* @param array $headers
21+
* @param int $options
22+
*/
23+
public function __construct($data = null, $status = 200, $headers = array(), $options = 0)
24+
{
25+
$this->jsonOptions = $options;
26+
27+
parent::__construct($data, $status, $headers);
28+
}
29+
830
/**
931
* Get the json_decoded data from the response
1032
*
@@ -13,8 +35,11 @@ class JsonResponse extends \Symfony\Component\HttpFoundation\JsonResponse {
1335
* @param int $options
1436
* @return mixed
1537
*/
16-
public function getData($assoc = false, $depth = 512, $options = 0)
38+
public function getData($assoc = false, $depth = 512, $options = null)
1739
{
40+
if (is_null($options))
41+
$options = $this->jsonOptions;
42+
1843
return json_decode($this->data, $assoc, $depth, $options);
1944
}
2045

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

2853
return $this->update();
2954
}
@@ -56,4 +81,4 @@ public function withCookie(Cookie $cookie)
5681
return $this;
5782
}
5883

59-
}
84+
}

src/Illuminate/Support/Facades/Response.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ public static function view($view, $data = array(), $status = 200, array $header
5151
* @param string|array $data
5252
* @param int $status
5353
* @param array $headers
54+
* @param int $options
5455
* @return \Illuminate\Http\JsonResponse
5556
*/
56-
public static function json($data = array(), $status = 200, array $headers = array())
57+
public static function json($data = array(), $status = 200, array $headers = array(), $options = 0)
5758
{
5859
if ($data instanceof ArrayableInterface)
5960
{
6061
$data = $data->toArray();
6162
}
6263

63-
return new JsonResponse($data, $status, $headers);
64+
return new JsonResponse($data, $status, $headers, $options);
6465
}
6566

6667
/**

0 commit comments

Comments
 (0)