Why does WordPress use serialize rather than json_encode for the options table? [duplicate]

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

Leave a Comment