Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89f4784

Browse files
committed
[HttpFoundation] Add ability to change JSON encoding options
1 parent8850456 commit89f4784

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 asjson.
88+
* Sets the data to be sent asJSON.
8489
*
8590
* @param mixed $data
8691
*
@@ -90,8 +95,7 @@ public function setCallback($callback = null)
9095
*/
9196
publicfunctionsetData($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
thrownew \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+
publicfunctiongetEncodingOptions()
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+
publicfunctionsetEncodingOptions(array$encodingOptions)
125+
{
126+
$this->encodingOptions =0;
127+
foreach ($encodingOptionsas$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+
publicfunctiontestGetEncodingOptions()
170+
{
171+
$response =newJsonResponse();
172+
173+
$this->assertEquals(JSON_HEX_TAG |JSON_HEX_APOS |JSON_HEX_AMP |JSON_HEX_QUOT,$response->getEncodingOptions());
174+
}
175+
176+
publicfunctiontestSetEncodingOptions()
177+
{
178+
$response =newJsonResponse();
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)

[8]ページ先頭

©2009-2025 Movatter.jp