dreamcat / class2json
将PHP的类转换为json字符串
v2.2.0
2023-09-15 06:07 UTC
Requires
- php: >=8
- ext-json: *
- dreamcat/array_2_class: ~2.3
- myclabs/php-enum: *
Requires (Dev)
- phpunit/phpunit: ~9
This package is auto-updated.
Last update: 2024-12-15 08:37:00 UTC
README
介绍
将PHP的类转换为json字符串
安装教程
composer require dreamcat/class2json
使用说明
可以直接用编码器,如下所示
<?php
use Dreamcat\Class2Array\Impl\Class2Json;
$data = [new \stdClass()];
$data[0]->ext = ["abc"];
$encoder = new Class2Json();
$json = $encoder->jsonEncode($data);
echo $json; # 输出 [{"ext":["abc"]}]
也可以用数据修正器得到可json序列化的数据,如下所示
<?php
use Dreamcat\Class2Array\Impl\DefaultJsonValueFixer;
$data = [new \stdClass()];
$data[0]->ext = ["abc"];
$fixer = new DefaultJsonValueFixer();
$fixed = $fixer->fixValue($data);
echo serialize($fixed);
# 输出 a:1:{i:0;a:1:{s:3:"ext";a:1:{i:0;s:3:"abc";}}}