dreamcat/class2json

将PHP的类转换为json字符串

v2.0.1 2021-09-19 02:36 UTC

This package is auto-updated.

Last update: 2021-11-19 03:05:18 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";}}}