@@ -26,6 +26,7 @@ class JsonResponse extends Response
26
26
{
27
27
protected $ data ;
28
28
protected $ callback ;
29
+ protected $ encodingOptions ;
29
30
30
31
/**
31
32
* Constructor.
@@ -41,6 +42,10 @@ public function __construct($data = null, $status = 200, $headers = array())
41
42
if (null === $ data ) {
42
43
$ data = new \ArrayObject ();
43
44
}
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
+
44
49
$ this ->setData ($ data );
45
50
}
46
51
@@ -80,7 +85,7 @@ public function setCallback($callback = null)
80
85
}
81
86
82
87
/**
83
- * Sets the data to be sent as json .
88
+ * Sets the data to be sent as JSON .
84
89
*
85
90
* @param mixed $data
86
91
*
@@ -90,8 +95,7 @@ public function setCallback($callback = null)
90
95
*/
91
96
public function setData ($ data = array ())
92
97
{
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 );
95
99
96
100
if (JSON_ERROR_NONE !== json_last_error ()) {
97
101
throw new \InvalidArgumentException ($ this ->transformJsonError ());
@@ -101,7 +105,36 @@ public function setData($data = array())
101
105
}
102
106
103
107
/**
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.
105
138
*
106
139
* @return JsonResponse
107
140
*/
0 commit comments