@@ -39,17 +39,17 @@ class JsonResponse extends Response
3939 * @param mixed $data The response data
4040 * @param int $status The response status code
4141 * @param array $headers An array of response headers
42- * @param bool $preEncoded If the data is already a JSON string
42+ * @param bool $json If the data is already a JSON string
4343 */
44- public function __construct ($ data = null , $ status = 200 , $ headers = array (), $ preEncoded = false )
44+ public function __construct ($ data = null , $ status = 200 , $ headers = array (), $ json = false )
4545 {
4646 parent ::__construct ('' , $ status , $ headers );
4747
4848 if (null === $ data ) {
4949 $ data = new \ArrayObject ();
5050 }
5151
52- $ this ->setData ($ data, $ preEncoded );
52+ $ json ? $ this ->setJson ($ data) : $ this -> setData ( $ data );
5353 }
5454
5555 /**
@@ -87,46 +87,57 @@ public function setCallback($callback = null)
8787 return $ this ->update ();
8888 }
8989
90+ /**
91+ * Sets a raw string containing a JSON document to be sent.
92+ *
93+ * @param string $data
94+ *
95+ * @return JsonResponse
96+ *
97+ * @throws \InvalidArgumentException
98+ */
99+ public function setJson ($ json )
100+ {
101+ $ this ->data = $ json ;
102+
103+ return $ this ->update ();
104+ }
105+
90106 /**
91107 * Sets the data to be sent as JSON.
92108 *
93109 * @param mixed $data
94- * @param bool $preEncoded If the data is already a JSON string
95110 *
96111 * @return JsonResponse
97112 *
98113 * @throws \InvalidArgumentException
99114 */
100- public function setData ($ data = array (), $ preEncoded = false )
115+ public function setData ($ data = array ())
101116 {
102- if (!$ preEncoded ) {
103- if (defined ('HHVM_VERSION ' )) {
104- // HHVM does not trigger any warnings and let exceptions
105- // thrown from a JsonSerializable object pass through.
106- // If only PHP did the same...
117+ if (defined ('HHVM_VERSION ' )) {
118+ // HHVM does not trigger any warnings and let exceptions
119+ // thrown from a JsonSerializable object pass through.
120+ // If only PHP did the same...
121+ $ data = json_encode ($ data , $ this ->encodingOptions );
122+ } else {
123+ try {
124+ // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
125+ // objects in a new exception that needs to be removed.
126+ // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
107127 $ data = json_encode ($ data , $ this ->encodingOptions );
108- } else {
109- try {
110- // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
111- // objects in a new exception that needs to be removed.
112- // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
113- $ data = json_encode ($ data , $ this ->encodingOptions );
114- } catch (\Exception $ e ) {
115- if ('Exception ' === get_class ($ e ) && 0 === strpos ($ e ->getMessage (), 'Failed calling ' )) {
116- throw $ e ->getPrevious () ?: $ e ;
117- }
118- throw $ e ;
128+ } catch (\Exception $ e ) {
129+ if ('Exception ' === get_class ($ e ) && 0 === strpos ($ e ->getMessage (), 'Failed calling ' )) {
130+ throw $ e ->getPrevious () ?: $ e ;
119131 }
120- }
121-
122- if (JSON_ERROR_NONE !== json_last_error ()) {
123- throw new \InvalidArgumentException (json_last_error_msg ());
132+ throw $ e ;
124133 }
125134 }
126135
127- $ this ->data = $ data ;
136+ if (JSON_ERROR_NONE !== json_last_error ()) {
137+ throw new \InvalidArgumentException (json_last_error_msg ());
138+ }
128139
129- return $ this ->update ( );
140+ return $ this ->setJson ( $ data );
130141 }
131142
132143 /**
0 commit comments