Skip to content

Commit 9d6b4ed

Browse files
authored
Merge pull request #94 from swisnl/feature/throw-json-exceptions
Throw JsonExceptions on errors in all json_encode and json_decode calls
2 parents df0cf38 + 265fe96 commit 9d6b4ed

File tree

7 files changed

+12
-13
lines changed

7 files changed

+12
-13
lines changed

src/Concerns/HasAttributes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ protected function setMutatedAttributeValue($key, $value)
426426
*/
427427
protected function asJson($value)
428428
{
429-
return json_encode($value);
429+
return json_encode($value, JSON_THROW_ON_ERROR);
430430
}
431431

432432
/**
@@ -439,7 +439,7 @@ protected function asJson($value)
439439
*/
440440
public function fromJson(string $value, $asObject = false)
441441
{
442-
return json_decode($value, !$asObject);
442+
return json_decode($value, !$asObject, 512, JSON_THROW_ON_ERROR);
443443
}
444444

445445
/**

src/DocumentClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function delete(string $endpoint, array $headers = []): DocumentInterface
116116
*/
117117
protected function prepareBody(ItemDocumentInterface $body): string
118118
{
119-
return $this->sanitizeJson(json_encode($body));
119+
return $this->sanitizeJson(json_encode($body, JSON_THROW_ON_ERROR));
120120
}
121121

122122
/**

src/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function toJsonApiArray(): array
168168
*/
169169
public function toJson($options = 0)
170170
{
171-
return json_encode($this->jsonSerialize(), $options);
171+
return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
172172
}
173173

174174
/**

src/Jsonapi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function toArray(): array
6565
*/
6666
public function toJson($options = 0)
6767
{
68-
return json_encode($this->jsonSerialize(), $options);
68+
return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
6969
}
7070

7171
/**

src/Links.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static function (?Link $link) {
130130
*/
131131
public function toJson($options = 0)
132132
{
133-
return json_encode($this->jsonSerialize(), $options);
133+
return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
134134
}
135135

136136
/**

src/Meta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function toArray()
125125
*/
126126
public function toJson($options = 0)
127127
{
128-
return json_encode($this->jsonSerialize(), $options);
128+
return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
129129
}
130130

131131
/**

src/Parsers/DocumentParser.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Swis\JsonApi\Client\Parsers;
66

7+
use JsonException;
78
use Swis\JsonApi\Client\Collection;
89
use Swis\JsonApi\Client\CollectionDocument;
910
use Swis\JsonApi\Client\Document;
@@ -152,13 +153,11 @@ public function parse(string $json): DocumentInterface
152153
*/
153154
private function decodeJson(string $json)
154155
{
155-
$data = json_decode($json, false);
156-
157-
if (json_last_error() !== JSON_ERROR_NONE) {
158-
throw new ValidationException(sprintf('Unable to parse JSON data: %s', json_last_error_msg()), json_last_error());
156+
try {
157+
return json_decode($json, false, 512, JSON_THROW_ON_ERROR);
158+
} catch (JsonException $exception) {
159+
throw new ValidationException(sprintf('Unable to parse JSON data: %s', $exception->getMessage()), 0, $exception);
159160
}
160-
161-
return $data;
162161
}
163162

164163
/**

0 commit comments

Comments
 (0)