flavioheleno / jay
json & simdjson 的轻量级封装
1.0.0
2023-08-17 20:12 UTC
Requires
- php: ^8.1
Requires (Dev)
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.3
- psy/psysh: ^0.11.20
- roave/security-advisories: dev-latest
README
Jay 是 json & simdjson 的轻量级封装,允许以透明的方式使用最快的可用 json 解码器。
在底层,如果可用并且 json 编码字符串不超过 4GiB(4.294.967.295 字节),Jay 将选择 simdjson,否则将回退到 PHP 的 JSON 核心扩展。
安装
要使用 Jay,只需简单运行
composer require flavioheleno/jay
用法
该库的使用非常简单。
$jsonEncoded = '{"a":"b","c":true,"d":10}'; // before $jsonDecoded = json_decode($jsonEncoded, true); // after $jsonDecoded = Jay\Json::fromString($jsonEncoded, true);
// before $jsonEncoded = filge_get_contents('path/to/file.json'); $jsonDecoded = json_decode($jsonEncoded, true); // after $jsonDecoded = Jay\Json::fromFile('path/to/file.json', true);
API
/** * @param string $path Name of the file to read * @param bool $associative When true, JSON objects will be returned as associative arrays; when * false, JSON objects will be returned as an instance of stdClass * @param int $depth Maximum nesting depth of the structure being decoded. The value must * be greater than 0, and less than or equal to 2.147.483.647 * * @return array|stdClass Returns the value encoded in json as an appropriate PHP type; unquoted * values true, false and null are returned as true, false and null * respectively * * @throws InvalidArgumentException if the json cannot be decoded or if the encoded data is * deeper than the nesting limit */ Jay\Json::fromFile(string $path, bool $associative = false, int $depth = 512): array|stdClass; /** * @param string|Stringable $contents The json string being decoded; this function only works * with UTF-8 encoded strings * @param bool $associative When true, JSON objects will be returned as associative * arrays; when false, JSON objects will be returned as an * instance of stdClass * @param int $depth Maximum nesting depth of the structure being decoded. The * value must be greater than 0, and less than or equal to * 2.147.483.647 * * @return array|stdClass Returns the value encoded in json as an appropriate PHP type; unquoted * values true, false and null are returned as true, false and null * respectively * * @throws InvalidArgumentException if the json cannot be decoded or if the encoded data is * deeper than the nesting limit. */ Jay\Json::fromString( string|Stringable $contents, bool $associative = false, int $depth = 512 ): array|stdClass;
许可协议
该库采用MIT 许可协议。