-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJsonDecoder.php
More file actions
58 lines (48 loc) · 1.35 KB
/
JsonDecoder.php
File metadata and controls
58 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
namespace Brick\Std\Json;
use function json_decode;
use const JSON_BIGINT_AS_STRING;
/**
* Decodes data in JSON format.
*/
final class JsonDecoder extends Common
{
/**
* Whether to decode objects as associative arrays.
*/
private bool $decodeObjectAsArray = false;
/**
* Decodes data in JSON format.
*
* @param string $json The JSON string to decode.
*
* @return mixed The decoded data.
*
* @throws JsonException If the data cannot be decoded.
*/
public function decode(string $json): mixed
{
// max depth is 0+ for json_encode(), and 1+ for json_decode()
$result = json_decode($json, $this->decodeObjectAsArray, $this->maxDepth + 1, $this->options);
$this->checkLastError();
return $result;
}
/**
* Sets whether to decode objects as associative arrays. Defaults to `false`.
*/
public function decodeObjectAsArray(bool $bool): void
{
$this->decodeObjectAsArray = $bool;
}
/**
* Sets whether to decode large integers as strings. Defaults to `false`.
*
* * `true` decodes large integers as strings
* * `false` decodes large integers as floats
*/
public function decodeBigIntAsString(bool $bool): void
{
$this->setOption(JSON_BIGINT_AS_STRING, $bool);
}
}