Information storage and retrieval is an important job while designing or developing an application. We all know how to store data with normal data types in database or file systems. Here I am going to discuss about storage and retrieval of data having complex type. Complex type means they are not like normal string or number or boolean values. Complex type means data structure or objects.
Think of a situation where you are working with some array. Now there may be some situations where you need to store the array and retrieve it back. If you are familiar with OOPS concept then objects are another type which you may need to store and retrieve.
Serialization is the process by which you can convert an object or data structure into a sequence of bits which can stored and retrieve back. Serialization is also known as deflating or marshalling. The opposite process (converting the serialized sequence of bits to object or data structure) is known as Deserialization or inflating or unmarshalling. Most of the important languages have their own implementation of these processes. In Java provides automatic serialization by implementing the java.io.Serializable interface. In perl there are modules like Storable or FreezeThaw. Python implements serialization through the standard library module pickle. In PHP there are two built-in functions as serialize() and unserialize() for this purpose. However as I will continue the discussion with PHP then you should be aware of the fact that – there is a difference in the implementation of serialization in PHP 4.x and PHP 5.x.
After successful completion of the project I started working upon the possibilities of storing data structure or object in PHP using JSON. IN case of PHP there are two function as json_encode() and json_decode(). Interesting thing with json_decode() is that it takes a boolean argument as second parameter[$assoc] which is by default false. But when TRUE json_decode() returns objects converted in associative array. After doing a lot R&D I find out certain facts which favors JSON rather than using serialize() /unserialize(). In general if you consider the size of the stream produced by serialize() with json_encode(), later produces small sized stream. While working with data structure like arrays JSON produces the same structure after encode and decode. So in my opinion usage of JSON is better than PHP serialize()/unserialize().
Now while working with objects, JSON produces the same result as PHP serialization functions. However if the object is complex or if you like to take the benefit of magic methods object oriented PHP5 then its better to use serialize()/unserialize(). As serialize() will attempt to call _sleep() [if exists] which can be used to do some last minute clean up of the object prior to serialization. Same way unserialize() will call _wakeup()[if exists] which can be used to do some reconstruction in the object just after unserialization. So if your objects are simple then use JSON otherwise go for serialize()/unserialize().
In the conclusion I will prefer to use JSON rather than PHP serialization methods. I won’t be surprised if I get good nos of comments for/against my preference.