[wp-trac] [WordPress Trac] #23753: Switch to JSON encoding instead of PHP serialisation

WordPress Trac noreply at wordpress.org
Wed Mar 13 12:37:08 UTC 2013


#23753: Switch to JSON encoding instead of PHP serialisation
-----------------------------+-----------------------------
 Reporter:  sanchothefat     |       Type:  feature request
   Status:  new              |   Priority:  normal
Milestone:  Awaiting Review  |  Component:  Database
  Version:  trunk            |   Severity:  minor
 Keywords:  dev-feedback     |
-----------------------------+-----------------------------
 When storing PHP arrays or objects in the database eg. options or metadata
 I think it would be better to use JSON encoding rather than PHP
 serialisation.

 Pros:

 1. Better/simpler unicode support
 2. A straight search replace on the database wouldn't break anything
 3. Better portability
 4. Smaller DB footprint
 5. `json_encode` is approx 25% faster than `serialize`

 Cons:

 1. `json_decode` is approx 55% slower than `unserialize`
 2. no `__sleep()` or `__wakeup()` magic methods (not used in core)
 3. no memory of what class an object was
 4. only public object properties encoded

 The biggest drawback here is obviously the speed of the JSON decoding
 however we're still talking only 1,000ths at most or 10,000ths of a
 second. The other big wins in my opinion outweigh the cons.

 There are of course a couple of instances where I've seen plugin authors
 store entire class instances in the options table but I'd argue over
 whether that's good practice. In consideration of backwards compatibility
 perhaps a set of alternative `add|update|get_option()` methods that use
 JSON encoding could be offered.

 What sparked this suggestion was writing a twitter API plugin that needs
 to cache the JSON results as transients. In cases where memcache or APC is
 unavailable the default serialisation and unserialization was breaking on
 unicode characters causing the cache to be invalid almost every time.

 I'd love to hear your thoughts on this issue and if you have any
 suggestions for how it could be implemented over time I'll be happy to
 work on a patch.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/23753>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list