Skip to content

Commit 74fb207

Browse files
committed
feature #9915 [HttpFoundation] Add ability to change JSON encoding options (stloyd)
This PR was merged into the 2.5-dev branch. Discussion ---------- [HttpFoundation] Add ability to change JSON encoding options | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Tests pass? | yes | Fixed tickets | #9086 | License | MIT Commits ------- 89f4784 [HttpFoundation] Add ability to change JSON encoding options
2 parents a596ba3 + 89f4784 commit 74fb207

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.5.0
5+
-----
6+
7+
* added `JsonResponse::setEncodingOptions()` & `JsonResponse::getEncodingOptions()` for easier manipulation
8+
of the options used while encoding data to JSON format.
9+
410
2.4.0
511
-----
612

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class JsonResponse extends Response
2626
{
2727
protected $data;
2828
protected $callback;
29+
protected $encodingOptions;
2930

3031
/**
3132
* Constructor.
@@ -41,6 +42,10 @@ public function __construct($data = null, $status = 200, $headers = array())
4142
if (null === $data) {
4243
$data = new \ArrayObject();
4344
}
45+
46+
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
47+
$this->encodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
48+
4449
$this->setData($data);
4550
}
4651

@@ -80,7 +85,7 @@ public function setCallback($callback = null)
8085
}
8186

8287
/**
83-
* Sets the data to be sent as json.
88+
* Sets the data to be sent as JSON.
8489
*
8590
* @param mixed $data
8691
*
@@ -90,8 +95,7 @@ public function setCallback($callback = null)
9095
*/
9196
public function setData($data = array())
9297
{
93-
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
94-
$this->data = @json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
98+
$this->data = @json_encode($data, $this->encodingOptions);
9599

96100
if (JSON_ERROR_NONE !== json_last_error()) {
97101
throw new \InvalidArgumentException($this->transformJsonError());
@@ -101,7 +105,36 @@ public function setData($data = array())
101105
}
102106

103107
/**
104-
* Updates the content and headers according to the json data and callback.
108+
* Returns options used while encoding data to JSON.
109+
*
110+
* @return integer
111+
*/
112+
public function getEncodingOptions()
113+
{
114+
return $this->encodingOptions;
115+
}
116+
117+
/**
118+
* Sets options used while encoding data to JSON.
119+
*
120+
* @param array $encodingOptions
121+
*
122+
* @return JsonResponse
123+
*/
124+
public function setEncodingOptions(array $encodingOptions)
125+
{
126+
$this->encodingOptions = 0;
127+
foreach ($encodingOptions as $encodingOption) {
128+
if (($this->encodingOptions & $encodingOption) != $encodingOption) {
129+
$this->encodingOptions |= $encodingOption;
130+
}
131+
}
132+
133+
return $this->setData(json_decode($this->data));
134+
}
135+
136+
/**
137+
* Updates the content and headers according to the JSON data and callback.
105138
*
106139
* @return JsonResponse
107140
*/

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ public function testJsonEncodeFlags()
166166
$this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
167167
}
168168

169+
public function testGetEncodingOptions()
170+
{
171+
$response = new JsonResponse();
172+
173+
$this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
174+
}
175+
176+
public function testSetEncodingOptions()
177+
{
178+
$response = new JsonResponse();
179+
$response->setData(array(array(1, 2, 3)));
180+
181+
$this->assertEquals('[[1,2,3]]', $response->getContent());
182+
183+
$response->setEncodingOptions(array(JSON_FORCE_OBJECT));
184+
185+
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
186+
}
187+
169188
/**
170189
* @expectedException \InvalidArgumentException
171190
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy