Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON vs. Serialized Array in database [closed]

What are the advantages and disadvantages of storing JSON data in MySQL database vs. serialized array?

like image 919
Inez Avatar asked Aug 20 '09 14:08

Inez


People also ask

Why does JSON need to be serialized?

The purpose of serializing it into JSON is so that the message will be a format that can be understood and from there, deserialize it into an object type that makes sense for the consumer.

Is JSON serialized data?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

Is an array JSON serializable?

NumPy array is not JSON serializable.

What is a serialized array?

The serialize array function is a built-in function in PHP. The serialization of data means converts a value into a sequence of bits to be stored in a memory buffer, in a file, or transfer across a network.


2 Answers

  1. JSON encode() & decode()
    • PHP Version >= 5.0.0
      • Nesting Limit of 20.
    • PHP Version >= 5.2.3
      • Nesting Limit of 128.
    • PHP Version >= 5.3.0
      • Nesting Limit of 512.
    • Small footprint vs PHP's serialize'd string.
  2. serialize() & unserialize()
    • PHP Version >= 4.0.0
      • Methods are not lost on PHP Datatype Object.
      • __wakeup() magic method called on any object being unserialize. (VERY POWERFUL)
      • It has been noted that it is some times best the base64 encode strings put into the database, and base64 decode strings taken out of the database with this function, as there are some issues with the handling of some white space characters.

The choice is yours.

like image 98
Mark Tomlin Avatar answered Sep 23 '22 11:09

Mark Tomlin


Pro JSON:

  • The JSON data can be used by many different languages, not just PHP
  • JSON data is human readable and writable.
  • It takes up less space
  • It is faster to encode JSON than to serialize

Pro Serialized Array:

  • It is faster do unserialize than to JSON decode

As the comments indicate, JSON takes up less space than a serialize array. I also checked whether JSON or Serializing is faster, and surprisingly, it is faster to JSON encode than to Serialize. It is faster to unserialize than to JSON decode though.

This is the script I used to test:

<?php  function runTime(){       $mtime = microtime();        $mtime = explode(' ', $mtime);        $mtime = $mtime[1] + $mtime[0];        return $mtime;  } ?>  <pre> <?php $start = runTime();  $ser;  for($i=0; $i<1000; $i++){     $a = array(a => 1, x => 10);     $ser = serialize($a); } $total = runTime() - $start; echo "Serializing 1000 times took \t$total seconds"; ?>  <?php $start = runTime();  $json;  for($i=0; $i<1000; $i++){     $a = array(a => 1, x => 10);     $json = json_encode($a); } $total = runTime() - $start; echo "JSON encoding 1000 times took \t$total seconds"; ?>  <?php $start = runTime();  $ser;  for($i=0; $i<1000; $i++){     $a = unserialize($ser); } $total = runTime() - $start; echo "Unserializing 1000 times took \t$total seconds"; ?>  <?php $start = runTime();  $json;  for($i=0; $i<1000; $i++){     $a = json_decode($json); } $total = runTime() - $start; echo "JSON decoding 1000 times took \t$total seconds"; ?> </pre> 
like image 35
Marius Avatar answered Sep 23 '22 11:09

Marius