将PHP对象序列化为JSON

Dan*_*ugg 100 php json object object-serialization

所以当我偶然发现新的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';
Run Code Online (Sandbox Code Playgroud)

echo json_encode($data) 产生一个空对象:

{}
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)

附录

1)

所以这就是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;
}
Run Code Online (Sandbox Code Playgroud)

但是,由于Mf_Data对象还具有对其父(包含)对象的引用,因此递归失败.当我删除_parent引用时,工作就像一个魅力.

2)

为了跟进,我改变了一个复杂的树节点对象的最终函数是:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

3)

我正在再次跟进,有点清洁的实现.使用接口进行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'
        )));
    }

}
Run Code Online (Sandbox Code Playgroud)

tak*_*hin 90

在最简单的情况下,类型提示应该起作用:

$json = json_encode( (array)$object );
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用名称空间和自动加载器,这会给出冗长/丑陋的属性名称. (7认同)
  • 这是最好的方法.我爱它,非常感谢你! (6认同)
  • 为什么它会在道具名称的开头添加\ u0000*\u0000? (5认同)
  • 有没有办法获得更清洁的财产名称? (4认同)

Wri*_*ken 44


编辑:目前是2016-09-24,PHP 5.4已经发布2012-03-01,支持已经结束 2015-09-01.不过,这个答案似乎得到了回报.如果您仍然使用PHP <5.4,则会产生安全风险并导致您的项目失败.如果您没有令人信服的理由留在<5.4,或者甚至已经使用版本> = 5.4,请不要使用此答案,只使用PHP> = 5.4(或者,您知道,最近的一个)并实现JsonSerializable接口


您将定义一个函数,例如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;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@Wrikken - 是否有任何快捷方式可以将对象,其中包含的对象(*所有成员,无论可见性或类型*)减少到关联数组,还是将其类型化为`stdClass`?我正在考虑*Reflection*的方向,但如果没有,我会想出一些递归执行的东西. (2认同)

小智 19

json_encode()只会编码公共成员变量.所以如果你想要自己包括私人(如其他人所建议)


小智 8

以下代码使用反射来完成工作.它假定您有要序列化的属性的getter

    <?php

    /**
     * Serialize a simple PHP object into json
     * Should be used for POPO that has getter methods for the relevant properties to serialize
     * A property can be simple or by itself another POPO object
     *
     * Class CleanJsonSerializer
     */
    class CleanJsonSerializer {

    /**
     * Local cache of a property getters per class - optimize reflection code if the same object appears several times
     * @var array
     */
    private $classPropertyGetters = array();

    /**
     * @param mixed $object
     * @return string|false
     */
    public function serialize($object)
    {
        return json_encode($this->serializeInternal($object));
    }

    /**
     * @param $object
     * @return array
     */
    private function serializeInternal($object)
    {
        if (is_array($object)) {
            $result = $this->serializeArray($object);
        } elseif (is_object($object)) {
            $result = $this->serializeObject($object);
        } else {
            $result = $object;
        }
        return $result;
    }

    /**
     * @param $object
     * @return \ReflectionClass
     */
    private function getClassPropertyGetters($object)
    {
        $className = get_class($object);
        if (!isset($this->classPropertyGetters[$className])) {
            $reflector = new \ReflectionClass($className);
            $properties = $reflector->getProperties();
            $getters = array();
            foreach ($properties as $property)
            {
                $name = $property->getName();
                $getter = "get" . ucfirst($name);
                try {
                    $reflector->getMethod($getter);
                    $getters[$name] = $getter;
                } catch (\Exception $e) {
                    // if no getter for a specific property - ignore it
                }
            }
            $this->classPropertyGetters[$className] = $getters;
        }
        return $this->classPropertyGetters[$className];
    }

    /**
     * @param $object
     * @return array
     */
    private function serializeObject($object) {
        $properties = $this->getClassPropertyGetters($object);
        $data = array();
        foreach ($properties as $name => $property)
        {
            $data[$name] = $this->serializeInternal($object->$property());
        }
        return $data;
    }

    /**
     * @param $array
     * @return array
     */
    private function serializeArray($array)
    {
        $result = array();
        foreach ($array as $key => $value) {
            $result[$key] = $this->serializeInternal($value);
        }
        return $result;
    }  
} 
Run Code Online (Sandbox Code Playgroud)


小智 6

只需实现PHP JsonSerializable提供的接口.