arokettu/json

具有合理默认值的ext-json包装器

2.1.0 2023-12-04 18:29 UTC

This package is auto-updated.

Last update: 2024-08-29 16:14:44 UTC


README

Packagist Packagist Gitlab pipeline status Codecov

标准ext-json的包装器,具有合理默认值

安装

composer require 'arokettu/json'

功能

解码包装器

解码包装器是库的主要目的。它的杀手级特性是JSON对象成为ArrayObject的实例,而不是stdClass。这既保持了原始的数组/对象类型,又允许以数组的方式处理所有数据。

<?php

$obj = \Arokettu\Json\Json::decode('{"abc": 123}');

// we can access any data array-style
var_dump($obj['abc']);
// or object-style
var_dump($obj->abc);

// object will not turn into array
echo \Arokettu\Json\Json::encode($obj);

选项对象

json标志的OOP接口:EncodeOptionsDecodeOptionsValidateOptions

<?php

use Arokettu\Json\EncodeOptions;

// set options with methods
$options = EncodeOptions::build()
    ->withThrowOnError()
    ->withHexAmp();
// set options with PHP 8 named params
$options = EncodeOptions::build(
    throw_on_error: true,   // apply JSON_THROW_ON_ERROR 
    hex_amp: true,          // apply JSON_HEX_AMP 
);
// use both with this library and with the base function
$value = \Arokettu\Json\Json::encode($json, $options);
$value = json_encode($json, $options->value()); 
// pretty print existing options mix
echo EncodeOptions::build(4194752)->toString();
// will get you 'JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT'

文档

在此处阅读完整文档:https://sandfox.dev/php/json.html

也请访问Read the Docs:https://arokettu-json.readthedocs.io/

支持

请在GitLab的主repo上提交问题:https://gitlab.com/sandfox/php-json/-/issues

许可证

该库根据MIT许可证作为开源软件提供。