定制 snortlin/json-codec 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固