Serialize/deserialize nested objects to JSON with type in PHP Serialize/deserialize nested objects to JSON with type in PHP json json

Serialize/deserialize nested objects to JSON with type in PHP


I suggest using the jms serializer: http://jmsyst.com/libs/serializerEasy to use, configuarble and supports all the features you requested.


I suggest you to use php's serialize function

In cases like this it's better to use this function because it exists for this purpose: you can store the serialized string wherever you want and after unserializing it you will get back the original PHP object with all the properties

With JSON, as you said, you'll have no clue of what class the object was (unless you store it manually as a string) and of course there will be all the problems related to the private properties


There are three methods for doing this: JSON, Serialize, and var_export.

With JSON, it will only work with objects of stdClass, but it's easy to read and can be used outside of PHP.

Serialize works with instances of classes other than stdClass, but it can be difficult to read and can only be used by PHP.http://php.net/manual/en/function.serialize.php

var_export outputs the PHP code to create the object (so you'd write it to a PHP file), it's very easy to read but can't be used outside PHP. The objects will need to have the set state method.http://php.net/manual/en/function.var-export.php