What are the advantages and disadvantages of storing JSON data in MySQL database vs. serialized array?
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.
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).
NumPy array is not JSON serializable.
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.
The choice is yours.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With