-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJsonEncoder.php
More file actions
133 lines (115 loc) · 3.5 KB
/
JsonEncoder.php
File metadata and controls
133 lines (115 loc) · 3.5 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Brick\Std\Json;
use function json_encode;
use const JSON_FORCE_OBJECT;
use const JSON_HEX_AMP;
use const JSON_HEX_APOS;
use const JSON_HEX_QUOT;
use const JSON_HEX_TAG;
use const JSON_NUMERIC_CHECK;
use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_LINE_TERMINATORS;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
/**
* Encodes data in JSON format.
*/
final class JsonEncoder extends Common
{
/**
* Encodes data in JSON format.
*
* @return string A JSON string representation of the data.
*
* @throws JsonException If the data cannot be encoded.
*/
public function encode(mixed $data): string
{
$result = json_encode($data, $this->options, $this->maxDepth);
$this->checkLastError();
return $result;
}
/**
* Sets whether to convert `<` and `>` to `\u003C` and `\u003E`. Defaults to `false`.
*/
public function escapeTags(bool $bool): void
{
$this->setOption(JSON_HEX_TAG, $bool);
}
/**
* Sets whether to convert `&` to `\u0026`. Defaults to `false`.
*/
public function escapeAmpersands(bool $bool): void
{
$this->setOption(JSON_HEX_AMP, $bool);
}
/**
* Sets whether to convert `'` to `\u0027`. Defaults to `false`.
*/
public function escapeApostrophes(bool $bool): void
{
$this->setOption(JSON_HEX_APOS, $bool);
}
/**
* Sets whether to convert `"` to `\u0022`. Defaults to `false`.
*/
public function escapeQuotes(bool $bool): void
{
$this->setOption(JSON_HEX_QUOT, $bool);
}
/**
* Sets whether to output an object rather than an array when a non-associative array is used. Defaults to `false`.
*
* Especially useful when the recipient of the output is expecting an object and the array is empty.
*/
public function forceObject(bool $bool): void
{
$this->setOption(JSON_FORCE_OBJECT, $bool);
}
/**
* Sets whether to encode numeric strings as numbers. Defaults to `false`.
*/
public function encodeNumericStringsAsNumbers(bool $bool): void
{
$this->setOption(JSON_NUMERIC_CHECK, $bool);
}
/**
* Sets whether to use whitespace in returned data to format it. Defaults to `false`.
*/
public function prettyPrint(bool $bool): void
{
$this->setOption(JSON_PRETTY_PRINT, $bool);
}
/**
* Sets whether to escape `/`. Defaults to `true`.
*/
public function escapeSlashes(bool $bool): void
{
$this->setOption(JSON_UNESCAPED_SLASHES, ! $bool);
}
/**
* Sets whether to escape multibyte Unicode characters as `\uXXXX`. Defaults to `true`.
*/
public function escapeUnicode(bool $bool): void
{
$this->setOption(JSON_UNESCAPED_UNICODE, ! $bool);
}
/**
* Sets whether to escape multibyte Unicode line terminator characters as `\uXXXX`. Defaults to `true`.
*
* Note that line terminators will still be escaped if `escapeUnicode()` is set to true (default).
*/
public function escapeLineTerminators(bool $bool): void
{
$this->setOption(JSON_UNESCAPED_LINE_TERMINATORS, ! $bool);
}
/**
* Sets whether to always encode float values as float values, even when the fraction is zero. Defaults to `false`.
*/
public function preserveZeroFraction(bool $bool): void
{
$this->setOption(JSON_PRESERVE_ZERO_FRACTION, $bool);
}
}