Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 399
Zeile 17... Zeile 17...
17
 */
17
 */
18
final class Json
18
final class Json
19
{
19
{
20
	use Nette\StaticClass;
20
	use Nette\StaticClass;
Zeile -... Zeile 21...
-
 
21
 
21
 
22
	/** @deprecated use Json::decode(..., forceArrays: true) */
-
 
23
	public const FORCE_ARRAY = JSON_OBJECT_AS_ARRAY;
-
 
24
 
22
	public const FORCE_ARRAY = JSON_OBJECT_AS_ARRAY;
25
	/** @deprecated use Json::encode(..., pretty: true) */
-
 
26
	public const PRETTY = JSON_PRETTY_PRINT;
-
 
27
 
23
	public const PRETTY = JSON_PRETTY_PRINT;
28
	/** @deprecated use Json::encode(..., asciiSafe: true) */
Zeile 24... Zeile 29...
24
	public const ESCAPE_UNICODE = 1 << 19;
29
	public const ESCAPE_UNICODE = 1 << 19;
25
 
30
 
26
 
31
 
27
	/**
-
 
28
	 * Converts value to JSON format. The flag can be Json::PRETTY, which formats JSON for easier reading and clarity,
32
	/**
29
	 * and Json::ESCAPE_UNICODE for ASCII output.
33
	 * Converts value to JSON format. Use $pretty for easier reading and clarity, $asciiSafe for ASCII output
30
	 * @param  mixed  $value
34
	 * and $htmlSafe for HTML escaping, $forceObjects enforces the encoding of non-associateve arrays as objects.
-
 
35
	 * @throws JsonException
-
 
36
	 */
-
 
37
	public static function encode(
-
 
38
		mixed $value,
-
 
39
		bool|int $pretty = false,
-
 
40
		bool $asciiSafe = false,
31
	 * @throws JsonException
41
		bool $htmlSafe = false,
-
 
42
		bool $forceObjects = false,
-
 
43
	): string
-
 
44
	{
32
	 */
45
		if (is_int($pretty)) { // back compatibility
33
	public static function encode($value, int $flags = 0): string
46
			$flags = ($pretty & self::ESCAPE_UNICODE ? 0 : JSON_UNESCAPED_UNICODE) | ($pretty & ~self::ESCAPE_UNICODE);
-
 
47
		} else {
-
 
48
			$flags = ($asciiSafe ? 0 : JSON_UNESCAPED_UNICODE)
-
 
49
				| ($pretty ? JSON_PRETTY_PRINT : 0)
-
 
50
				| ($forceObjects ? JSON_FORCE_OBJECT : 0)
34
	{
51
				| ($htmlSafe ? JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG : 0);
35
		$flags = ($flags & self::ESCAPE_UNICODE ? 0 : JSON_UNESCAPED_UNICODE)
52
		}
Zeile 36... Zeile 53...
36
			| JSON_UNESCAPED_SLASHES
53
 
37
			| ($flags & ~self::ESCAPE_UNICODE)
54
		$flags |= JSON_UNESCAPED_SLASHES
38
			| (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0); // since PHP 5.6.6 & PECL JSON-C 1.3.7
55
			| (defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0); // since PHP 5.6.6 & PECL JSON-C 1.3.7
Zeile 45... Zeile 62...
45
		return $json;
62
		return $json;
46
	}
63
	}
Zeile 47... Zeile 64...
47
 
64
 
48
 
65
 
49
	/**
-
 
50
	 * Parses JSON to PHP value. The flag can be Json::FORCE_ARRAY, which forces an array instead of an object as the return value.
66
	/**
51
	 * @return mixed
67
	 * Parses JSON to PHP value. The $forceArrays enforces the decoding of objects as arrays.
52
	 * @throws JsonException
68
	 * @throws JsonException
53
	 */
69
	 */
-
 
70
	public static function decode(string $json, bool|int $forceArrays = false): mixed
-
 
71
	{
-
 
72
		$flags = is_int($forceArrays) // back compatibility
-
 
73
			? $forceArrays
-
 
74
			: ($forceArrays ? JSON_OBJECT_AS_ARRAY : 0);
54
	public static function decode(string $json, int $flags = 0)
75
		$flags |= JSON_BIGINT_AS_STRING;
55
	{
76
 
56
		$value = json_decode($json, null, 512, $flags | JSON_BIGINT_AS_STRING);
77
		$value = json_decode($json, flags: $flags);
57
		if ($error = json_last_error()) {
78
		if ($error = json_last_error()) {
Zeile 58... Zeile 79...
58
			throw new JsonException(json_last_error_msg(), $error);
79
			throw new JsonException(json_last_error_msg(), $error);