forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonOptimizer.php
More file actions
49 lines (41 loc) · 1.37 KB
/
JsonOptimizer.php
File metadata and controls
49 lines (41 loc) · 1.37 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
<?php
namespace ProcessMaker\Support;
use Illuminate\Support\Facades\Log;
class JsonOptimizer
{
/**
* Global configuration set by ServiceProvider
*/
public static bool $useSimdjsonDecode = false;
public static bool $useSimdjsonEncode = false;
/**
* Decodes a JSON using simdjson if available.
*/
public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
{
if (self::$useSimdjsonDecode) {
try {
return simdjson_decode($json, $assoc, $depth);
} catch (\Throwable $e) {
Log::warning("simdjson_decode failed: {$e->getMessage()}");
}
}
return json_decode($json, $assoc, $depth, $options);
}
/**
* Encodes a JSON using simdjson if available.
* But json_encode is more performant than json_optimization_encode.
* to_do: research other options for json_encode optimization.
*/
public static function encode(mixed $value, int $flags = 0, int $depth = 512): string|false
{
if (self::$useSimdjsonEncode) {
try {
return simdjson_encode($value, $flags, $depth);
} catch (\Throwable $e) {
Log::warning("simdjson_encode failed: {$e->getMessage()}");
}
}
return json_encode($value, $flags, $depth);
}
}