File: Json.decode%28%29.phpt

package info (click to toggle)
php-nette-utils 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,420 kB
  • sloc: php: 4,069; xml: 12; makefile: 4
file content (96 lines) | stat: -rw-r--r-- 2,183 bytes parent folder | download | duplicates (3)
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
<?php

/**
 * Test: Nette\Utils\Json::decode()
 */

declare(strict_types=1);

use Nette\Utils\Json;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::same('ok', Json::decode('"ok"'));
Assert::null(Json::decode('null'));
Assert::null(Json::decode(' null'));


Assert::equal((object) ['a' => 1], Json::decode('{"a":1}'));
Assert::same(['a' => 1], Json::decode('{"a":1}', Json::FORCE_ARRAY));
Assert::same(['a' => 1], Json::decode('{"a":1}', forceArrays: true));


Assert::exception(
	fn() => Json::decode(''),
	Nette\Utils\JsonException::class,
	'Syntax error',
);


Assert::exception(
	fn() => Json::decode('NULL'),
	Nette\Utils\JsonException::class,
	'Syntax error',
);


Assert::exception(
	fn() => Json::decode('{'),
	Nette\Utils\JsonException::class,
	'Syntax error',
);


Assert::exception(
	fn() => Json::decode('{}}'),
	Nette\Utils\JsonException::class,
	'Syntax error',
);


Assert::exception(
	fn() => Json::decode("\x00"),
	Nette\Utils\JsonException::class,
	defined('JSON_C_VERSION') ? 'Syntax error' : 'Control character error, possibly incorrectly encoded',
);


Assert::exception(
	fn() => Json::decode('{"\u0000": 1}'),
	Nette\Utils\JsonException::class,
	'The decoded property name is invalid',
);


Assert::same(["\x00" => 1], Json::decode('{"\u0000": 1}', Json::FORCE_ARRAY));
Assert::equal((object) ['a' => "\x00"], Json::decode('{"a": "\u0000"}'));
Assert::equal((object) ["\"\x00" => 1], Json::decode('{"\"\u0000": 1}'));


Assert::exception(
	fn() => Json::decode("\"\xC1\xBF\""),
	Nette\Utils\JsonException::class,
	'Malformed UTF-8 characters, possibly incorrectly encoded',
);


// default JSON_BIGINT_AS_STRING
if (defined('JSON_C_VERSION')) {
	if (PHP_INT_SIZE > 4) {
		// 64-bit
		Assert::same([9_223_372_036_854_775_807], Json::decode('[12345678901234567890]'));   // trimmed to max 64-bit integer
	} else {
		// 32-bit
		Assert::same(['9223372036854775807'], Json::decode('[12345678901234567890]'));  // trimmed to max 64-bit integer
	}

} else {
	Assert::same(['12345678901234567890'], Json::decode('[12345678901234567890]'));
}


// JSON_* constants support
Assert::same('ab', Json::decode("\"a\x80b\"", JSON_INVALID_UTF8_IGNORE));