WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a basic understanding of serialize (I come across it often when I'm migrating WordPress installs) and I just discovered json_encode.

Given that serialize causes so much trouble with replacing strings in the database (eg. editing an optinos field directly), why does WordPress prefer serialize to (the seemingly similar) json_encode, which doesn't insert the string length into the stored value.

My first thought was that it may provide some security or error-checking benefits - WordPress will ignore the option if the string lengths are incorrect.. but I'm interested in a qualified explanation :)

share|improve this question
If you use the search replace tool here: interconnectit.com/124/… then you can replace strings in serialised form too – Tom J Nowell Aug 7 '12 at 10:10
up vote 9 down vote accepted
  • serialize representation can be stored in text and reversed
  • JSON representation can be stored in text but cannot always be precisely reversed

Run this example:

$query = new WP_Query();
var_dump( $query );
var_dump( unserialize( serialize( $query ) ) );
var_dump( json_decode( json_encode( $query ) ) );
  • After going through serialize accurate WP_Query object is re-created.
  • After going through JSON you get stdClass object.

In a nutshell serialize is native part of PHP and has a lot of synergy with manipulating data in it. JSON is language-agnostic format that is convenient for systems talking to each other, but far from comprehensive and cannot do things in PHP which serialize does without second thought.

share|improve this answer
I knew there had to be a good reason :) thanks @rarst – ptim Aug 8 '12 at 1:04

See here for a good discussion on the reasoning behind not changing it to JSON:


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.