@@ -34,22 +34,20 @@ class JsonResponse extends Response
3434 protected $ encodingOptions = self ::DEFAULT_ENCODING_OPTIONS ;
3535
3636 /**
37- * Constructor.
38- *
39- * @param mixed $data The response data
40- * @param int $status The response status code
41- * @param array $headers An array of response headers
42- * @param bool $preEncoded If the data is already a JSON string
37+ * @param mixed $data The response data
38+ * @param int $status The response status code
39+ * @param array $headers An array of response headers
40+ * @param bool $json If the data is already a JSON string
4341 */
44- public function __construct ($ data = null , $ status = 200 , $ headers = array (), $ preEncoded = false )
42+ public function __construct ($ data = null , $ status = 200 , $ headers = array (), $ json = false )
4543 {
4644 parent ::__construct ('' , $ status , $ headers );
4745
4846 if (null === $ data ) {
4947 $ data = new \ArrayObject ();
5048 }
5149
52- $ this ->setData ($ data, $ preEncoded );
50+ $ json ? $ this ->setJson ($ data) : $ this -> setData ( $ data );
5351 }
5452
5553 /**
@@ -87,46 +85,57 @@ public function setCallback($callback = null)
8785 return $ this ->update ();
8886 }
8987
88+ /**
89+ * Sets a raw string containing a JSON document to be sent.
90+ *
91+ * @param string $data
92+ *
93+ * @return JsonResponse
94+ *
95+ * @throws \InvalidArgumentException
96+ */
97+ public function setJson ($ json )
98+ {
99+ $ this ->data = $ json ;
100+
101+ return $ this ->update ();
102+ }
103+
90104 /**
91105 * Sets the data to be sent as JSON.
92106 *
93107 * @param mixed $data
94- * @param bool $preEncoded If the data is already a JSON string
95108 *
96109 * @return JsonResponse
97110 *
98111 * @throws \InvalidArgumentException
99112 */
100- public function setData ($ data = array (), $ preEncoded = false )
113+ public function setData ($ data = array ())
101114 {
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...
115+ if (defined ('HHVM_VERSION ' )) {
116+ // HHVM does not trigger any warnings and let exceptions
117+ // thrown from a JsonSerializable object pass through.
118+ // If only PHP did the same...
119+ $ data = json_encode ($ data , $ this ->encodingOptions );
120+ } else {
121+ try {
122+ // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
123+ // objects in a new exception that needs to be removed.
124+ // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
107125 $ 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 ;
126+ } catch (\Exception $ e ) {
127+ if ('Exception ' === get_class ($ e ) && 0 === strpos ($ e ->getMessage (), 'Failed calling ' )) {
128+ throw $ e ->getPrevious () ?: $ e ;
119129 }
120- }
121-
122- if (JSON_ERROR_NONE !== json_last_error ()) {
123- throw new \InvalidArgumentException (json_last_error_msg ());
130+ throw $ e ;
124131 }
125132 }
126133
127- $ this ->data = $ data ;
134+ if (JSON_ERROR_NONE !== json_last_error ()) {
135+ throw new \InvalidArgumentException (json_last_error_msg ());
136+ }
128137
129- return $ this ->update ( );
138+ return $ this ->setJson ( $ data );
130139 }
131140
132141 /**
0 commit comments