内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
所以当我偶然发现新的JsonSerializable接口时,我正在php.net中寻找有关将PHP对象序列化为JSON的信息。这只是PHP> = 5.4,而我正在5.3.x环境中运行。
这种功能如何实现PHP <5.4?
我还没有用JSON工作太多,但我试图在应用程序中支持API层,并且将数据对象(否则将被发送到视图)转储到JSON中将是完美的。
如果我试图直接序列化对象,它将返回一个空的JSON字符串; 这是因为我认为json_encode()
不知道该用什么对象来做。应予递归降低对象到一个数组,然后编码该?
$data = new Mf_Data();
$data->foo->bar['hello'] = 'world';
echo json_encode($data)
产生一个空的对象:
{}
var_dump($data)
然而,按预期工作:
object(Mf_Data)#1 (5) {
["_values":"Mf_Data":private]=>
array(0) {
}
["_children":"Mf_Data":private]=>
array(1) {
[0]=>
array(1) {
["foo"]=>
object(Mf_Data)#2 (5) {
["_values":"Mf_Data":private]=>
array(0) {
}
["_children":"Mf_Data":private]=>
array(1) {
[0]=>
array(1) {
["bar"]=>
object(Mf_Data)#3 (5) {
["_values":"Mf_Data":private]=>
array(1) {
[0]=>
array(1) {
["hello"]=>
string(5) "world"
}
}
["_children":"Mf_Data":private]=>
array(0) {
}
["_parent":"Mf_Data":private]=>
*RECURSION*
["_key":"Mf_Data":private]=>
string(3) "bar"
["_index":"Mf_Data":private]=>
int(0)
}
}
}
["_parent":"Mf_Data":private]=>
*RECURSION*
["_key":"Mf_Data":private]=>
string(3) "foo"
["_index":"Mf_Data":private]=>
int(0)
}
}
}
["_parent":"Mf_Data":private]=>
NULL
["_key":"Mf_Data":private]=>
NULL
["_index":"Mf_Data":private]=>
int(0)
}
所以这是toArray()
我为Mf_Data
班级设计的功能:
public function toArray()
{
$array = (array) $this;
array_walk_recursive($array, function (&$property) {
if ($property instanceof Mf_Data) {
$property = $property->toArray();
}
});
return $array;
}
但是由于Mf_Data
对象也有对它们的父对象(包含)的引用,所以这会失败并返回。虽然当我删除_parent
参考时,它就像一个魅力。
为了跟进,我转换复杂树节点对象的最终功能是:
// class name - Mf_Data
// exlcuded properties - $_parent, $_index
public function toArray()
{
$array = get_object_vars($this);
unset($array['_parent'], $array['_index']);
array_walk_recursive($array, function (&$property) {
if (is_object($property) && method_exists($property, 'toArray')) {
$property = $property->toArray();
}
});
return $array;
}
我正在跟进,稍微清理一下实施。使用接口进行instanceof
检查似乎比method_exists()
(但是method_exists()
交叉继承/实现)更干净。
使用unset()
看起来也有点混乱,似乎逻辑应该被重构为另一种方法。然而,这个实现并没有复制属性数组(因为array_diff_key
),所以需要考虑一些事情。
interface ToMapInterface
{
function toMap();
function getToMapProperties();
}
class Node implements ToMapInterface
{
private $index;
private $parent;
private $values = array();
public function toMap()
{
$array = $this->getToMapProperties();
array_walk_recursive($array, function (&$value) {
if ($value instanceof ToMapInterface) {
$value = $value->toMap();
}
});
return $array;
}
public function getToMapProperties()
{
return array_diff_key(get_object_vars($this), array_flip(array(
'index', 'parent'
)));
}
}
你可以定义一个函数,比如named getJsonData();
,它会返回一个数组,stdClass
对象或者其他具有可见参数的对象,而不是私有/受保护的对象,然后执行a json_encode($data->getJsonData());
。实质上,从5.4实现函数,但手工调用它。
像这样的东西可以get_object_vars()
在类中调用,可以访问私有/受保护的变量:
function getJsonData(){
$var = get_object_vars($this);
foreach ($var as &$value) {
if (is_object($value) && method_exists($value,'getJsonData')) {
$value = $value->getJsonData();
}
}
return $var;
}