Skip to content

Commit 957ab86

Browse files
author
lidanyang
committed
fix
1 parent 9b1184f commit 957ab86

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
5+
namespace Lib\Framework\Http;
6+
7+
8+
use Hyperf\HttpServer\Exception\Http\EncodingException;
9+
use Hyperf\Utils\Contracts\Arrayable;
10+
use Hyperf\Utils\Contracts\Jsonable;
11+
12+
class HttpResponse extends \Hyperf\HttpServer\Response
13+
{
14+
protected function toJson($data): string
15+
{
16+
if (is_array($data)) {
17+
return json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
18+
}
19+
20+
if ($data instanceof Jsonable) {
21+
return (string) $data;
22+
}
23+
24+
if ($data instanceof Arrayable) {
25+
return json_encode($data->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
26+
}
27+
28+
throw new EncodingException('Error encoding response data to JSON.');
29+
}
30+
}

0 commit comments

Comments
 (0)