Skip to content

Commit ddae72d

Browse files
ChinaMoliliu21st
authored andcommitted
优化 Json 字段解析效率
1 parent 6c9031f commit ddae72d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/model/type/Json.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ public static function from(mixed $value, Modelable $model)
2020

2121
public function data($data, ?bool $assoc)
2222
{
23-
if (is_string($data) && json_validate($data)) {
24-
$data = json_decode($data, $assoc);
25-
} elseif (empty($data)) {
26-
$data = [];
23+
if (is_string($data)) {
24+
$this->data = json_decode($data, $assoc);
25+
if (json_last_error() !== JSON_ERROR_NONE) {
26+
$this->data = [$data];
27+
}
28+
return;
2729
}
28-
$this->data = is_string($data) ? [$data] : $data;
30+
$this->data = empty($data) ? [] : $data;
2931
}
3032

3133
public function value()

0 commit comments

Comments
 (0)