This commit is contained in:
2026-06-24 21:15:14 +08:00
parent 7455dc8d58
commit 3daa4021ec
3 changed files with 6 additions and 3 deletions
+2 -1
View File
@@ -97,7 +97,8 @@ class ConstrictResponse extends Message implements ResponseInterface
*/ */
public function json(array $content, int $statusCode = 200): static public function json(array $content, int $statusCode = 200): static
{ {
$this->stream->write(json_encode($content, JSON_UNESCAPED_UNICODE)); $encoded = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
$this->stream->write($encoded === false ? '{"error":"json encode failed"}' : $encoded);
return $this->withContentType(ContentType::JSON)->withStatus($statusCode); return $this->withContentType(ContentType::JSON)->withStatus($statusCode);
} }
+2 -1
View File
@@ -25,7 +25,8 @@ class ArrayFormat implements IFormat
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); $encoded = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
return $this->response->withBody(new Stream($encoded === false ? '[]' : $encoded));
} }
+2 -1
View File
@@ -31,7 +31,8 @@ class MixedFormat implements IFormat
return $this->response->withBody(new Stream('[object]')); return $this->response->withBody(new Stream('[object]'));
} }
if (is_array($result)) { if (is_array($result)) {
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); $encoded = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
return $this->response->withBody(new Stream($encoded === false ? '[]' : $encoded));
} else { } else {
return $this->response->withBody(new Stream((string)$result)); return $this->response->withBody(new Stream((string)$result));
} }