snortlin / json-codec
A PHP library for JSON encoding and decoding with adapter-based extensibility.
1.0.0
2026-07-03 09:00 UTC
Requires
- php: ^8.5
Requires (Dev)
- phpstan/phpstan: ^2.2
Suggests
- awesome/simdjson-plus: Enables faster JSON encoding and decoding through compatible simdjson_* functions.
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::json([ '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::json( data: ['name' => 'John'], context: [ JsonEncoder::OPTIONS => JSON_PRETTY_PRINT, JsonEncoder::RECURSION_DEPTH => 512, ], );
Decoding
Static API
use Snortlin\JsonCodec\JsonDecoder; $data = JsonDecoder::array('{"name":"John","active":true}');
use Snortlin\JsonCodec\JsonDecoder; $object = JsonDecoder::object('{"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::array( 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