snortlin/json-codec
Composer 安装命令:
composer require snortlin/json-codec
包简介
A PHP library for JSON encoding and decoding with adapter-based extensibility.
README 文档
README
Small JSON encode/decode API for PHP.
The library provides simple JsonEncoder and JsonDecoder classes that hide the actual JSON implementation behind adapters.
It can automatically use a faster adapter when the supported PHP extension is available, or fall back to native PHP JSON functions.
Features
- Simple JSON encode/decode API
- Static shortcut methods
- Instance-based usage suitable for DI
- Automatic adapter selection with native PHP fallback
- Custom encoder/decoder adapters
- Basic encode/decode context options
Installation
composer require snortlin/json-codec
Supported adapters
The default adapter factories select the best available adapter:
simdjson_encode()/simdjson_decode()from PECL simdjson or awesomized/simdjson-plus-php-ext (recommended)- Native PHP
json_encode()/json_decode()
If a supported simdjson extension is available, the simdjson adapter is used automatically. Otherwise, the native PHP adapter is used.
Encoding
Static API
use Snortlin\JsonCodec\JsonEncoder; $json = JsonEncoder::toJson([ 'name' => 'John', 'active' => true, ]);
Instance API
use Snortlin\JsonCodec\JsonEncoder; $encoder = new JsonEncoder(); $json = $encoder->encode([ 'name' => 'John', 'active' => true, ]);
With options
use Snortlin\JsonCodec\JsonEncoder; $json = JsonEncoder::toJson( data: ['name' => 'John'], context: [ JsonEncoder::OPTIONS => JSON_PRETTY_PRINT, JsonEncoder::RECURSION_DEPTH => 512, ], );
Decoding
Static API
use Snortlin\JsonCodec\JsonDecoder; $data = JsonDecoder::toArray('{"name":"John","active":true}');
use Snortlin\JsonCodec\JsonDecoder; $object = JsonDecoder::toObject('{"name":"John","active":true}');
Instance API
use Snortlin\JsonCodec\JsonDecoder; $decoder = new JsonDecoder(); $data = $decoder->decode('{"name":"John","active":true}');
With options
use Snortlin\JsonCodec\JsonDecoder; $data = JsonDecoder::toArray( data: '{"name":"John"}', context: [ JsonDecoder::RECURSION_DEPTH => 512, ], );
use Snortlin\JsonCodec\JsonDecoder; $data = (new JsonDecoder())->decode( data: '{"name":"John"}', context: [ JsonDecoder::ASSOCIATIVE => true, JsonDecoder::RECURSION_DEPTH => 512, ], );
Custom adapters
Custom adapters can be passed directly to the API classes.
use Snortlin\JsonCodec\JsonEncoder; use Snortlin\JsonCodec\Encoder\JsonEncoderAdapterInterface; final class CustomEncoderAdapter implements JsonEncoderAdapterInterface { public function encode(mixed $value, int $flags, int $depth): string { // Custom implementation } } $encoder = new JsonEncoder( encoder: new CustomEncoderAdapter(), );
use Snortlin\JsonCodec\JsonDecoder; use Snortlin\JsonCodec\Decoder\JsonDecoderAdapterInterface; final class CustomDecoderAdapter implements JsonDecoderAdapterInterface { public function decode(string $json, bool $associative, int $depth): mixed { // Custom implementation } } $decoder = new JsonDecoder( decoder: new CustomDecoderAdapter(), );
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-02