承接 technically/json 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

technically/json

最新稳定版本:1.0.1

Composer 安装命令:

composer require technically/json

包简介

Minimalistic wrapper around PHP native JSON functions, with additional data validity and error checks.

README 文档

README

Technically\Json is a minimalistic wrapper around PHP native json_encode and json_decode functions, which always throws an exception in case of an error.

Tests Status

Features

  • PHP 7.3+
  • PHP 8.0
  • Semver
  • Tests

Problem

Look, if an error occurs with json_decode(), by default, it sets the global error state that can be retrieved with json_last_error() and json_last_error_msg(). Alternatively, developers can use JSON_THROW_ON_ERROR option to make json_decode() throw an exception in case of invalid input.

Proper way of using the native json_decode() function, though many developers forget about it:

try {
    $data = json_decode($input, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
    // handle invalid input
}

Or this:

$data = \json_decode($input);
$error = \json_last_error();

if ($error !== JSON_ERROR_NONE) {
    throw new \JsonException(\json_last_error_msg(), $error);
}

I believe it should be the default behavior of json_decode() and json_encode() to throw on errors. We don't have to remember about it.

Solution

use function Technically\Json\json_decode;

try {
    $data = json_decode($input);
} catch (JsonException $exception) {
    // handle invalid input
}

Installation

Use Composer package manager to add the library to your project:

composer require technically/json

Usage

The namespaced json_encode() and json_decode() functions do always add JSON_THROW_ON_ERROR flag, but in the rest they work identically to the native functions: same arguments, same options, same result.

<?php

use JsonException;
use function Technically\Json\json_decode;
use function Technically\Json\json_encode;

// encode
echo json_encode(['name' => 'technically/json', 'type' => 'library']);

// decode with safety check exception to protected
try {
    $data = json_decode('[{ invalid JSON');
} catch (JsonException $exception) {
    // handle invalid data
}

Changelog

All notable changes to this project will be documented in the CHANGELOG file.

Credits

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-30