snortlin/json-codec

A PHP library for JSON encoding and decoding with adapter-based extensibility.

Maintainers

Package info

github.com/snortlin/json-codec

pkg:composer/snortlin/json-codec

Transparency log

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-07-03 09:00 UTC

This package is auto-updated.

Last update: 2026-07-03 09:02:58 UTC


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:

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