PHP serialization or Json

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 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.

In last couple of months I need to work with JSON (JavaScript Object Notation). JSON is an ideal data interchange format which is language independent. I had used the JSON to design some Ajax driven functionality in one of my projects. Over there I had encoded into JSON some normal and associative PHP array and passed them to access by JavaScript.

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.


  1. Ian says:

    I think you put it rather well. In a nutshell:
    serialize() data that will remain inside PHP. json_encode() for data that will be shared with other languages (especially JavaScript).

    There other commonly used language-agnostic serialization techniques such as SOAP and XML-RPC, but few are as succinct and readily available as serialization and JSON.

  2. Dennis says:

    Also note that serialize() works about 3 times faster than json_encode()

  3. [...] the Cohesive Web blog there’s a new post looking at storing complex data types like data structures or objects effectively. Think of a situation where you are working with some [...]

  4. programmer says:

    I think you forgot something. Serialize() saves the type of variable (string, integer etc.),
    while for JSON everything is a string.

  5. [...] the Cohesive Web blog there’s a new post looking at storing complex data types like data structures or objects effectively. Think of a situation where you are working with some [...]

  6. nfx says:

    Another point: json encode/decode are faster than built-in php serialization. in fact, i’ve used mostly json, when there was no special need in keeping all object properties (and passed all the properties to object constructor).

  7. Orzech says:

    In my opinion serialize has one big advantage – if you serialize instance of `MyClass`, result of unserialize you’ll get `almost` the same instance. Json_* results with instance of `std_object`. This could be a quite problem because in php there still is nothing like casting to `MyClass`.

  8. Jon Reed says:

    I agree with your conlusion, but came to it for a different reason.
    json_encode() will natively support support UTF-8 strings. I’ve found that unserializing strings, in PHP, which contain UTF-8 characters can break them.

    ie. JSON converts UTF-8 characters to unicode escape sequences. serialize() does not.

    There’s some discussion about it here as well:

  9. Susenjit says:

    I agree with Orzech about the advantage of serialize(). But this is required in some cases as said by nfx.

    Thanks to Jon Reed for sharing another benifit of using json_encode.

  10. [...] the Cohesive Web blog there's a new post looking at storing complex data types like data structures or objects effectively. Think of a situation where you are working with some [...]

  11. Chris Henry says:

    When it comes to choosing a transport for data, JSON wins hands down. It’s way smaller than XML, and in PHP, a string can be built and sent to the browser in a single line. There is instant compatibility with any browser that runs javascript. In most other languages, there are simple libraries that handle serializing and deserializing. For this reason, JSON is also an excellent choice of of transport medium for APIs.

  12. Susenjit says:

    Thanks to Chris for providing this justified point.

Leave a Reply