Switch to JSON encoding instead of PHP serialisation
|Reported by:||sanchothefat||Owned by:|
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.
- Better/simpler unicode support
- A straight search replace on the database wouldn't break anything
- Better portability
- Smaller DB footprint
- json_encode is approx 25% faster than serialize
- json_decode is approx 55% slower than unserialize
- no __sleep() or __wakeup() magic methods (not used in core)
- no memory of what class an object was
- 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.