-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathJson.encode().phpt
More file actions
53 lines (32 loc) · 1.5 KB
/
Json.encode().phpt
File metadata and controls
53 lines (32 loc) · 1.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
<?php
/**
* Test: Nette\Utils\Json::encode()
*/
declare(strict_types=1);
use Nette\Utils\Json;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
Assert::same('"ok"', Json::encode('ok'));
Assert::exception(function () {
Json::encode(["bad utf\xFF"]);
}, Nette\Utils\JsonException::class, 'Malformed UTF-8 characters, possibly incorrectly encoded');
Assert::exception(function () {
$arr = ['recursive'];
$arr[] = &$arr;
Json::encode($arr);
}, Nette\Utils\JsonException::class, '%a?%ecursion detected');
// default JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES
Assert::same("\"/I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n\"", Json::encode("/I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n"));
Assert::same('"\u2028\u2029"', Json::encode("\u{2028}\u{2029}"));
// ESCAPE_UNICODE
Assert::same('"/I\u00f1t\u00ebrn\u00e2ti\u00f4n\u00e0liz\u00e6ti\u00f8n"', Json::encode("/I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n", Json::ESCAPE_UNICODE));
Assert::same('"\u2028\u2029"', Json::encode("\u{2028}\u{2029}", Json::ESCAPE_UNICODE));
// JSON_PRETTY_PRINT
Assert::same("[\n 1,\n 2,\n 3\n]", Json::encode([1, 2, 3], Json::PRETTY));
Assert::same('[]', JSON::encode([]));
Assert::same('{}', JSON::encode([], Json::FORCE_OBJECT));
Assert::exception(function () {
Json::encode(NAN);
}, Nette\Utils\JsonException::class, 'Inf and NaN cannot be JSON encoded');
// JSON_PRESERVE_ZERO_FRACTION
Assert::same(defined('JSON_PRESERVE_ZERO_FRACTION') ? '1.0' : '1', Json::encode(1.0));