Monday, 9 August 2010

Object Serialization

Some 15 years ago when I got my first job as a programmer my first task was to learn SQL. A language which had been around for a while but I'd never encountered before and was only just coming of age. I remember throughout the late 90's and early 00's the pain of writing some enormous SQL queries simply to load, save and delete business data from the data servers and the, sometimes, weeks wasted just because another field was added.
These days of course we've moved on and the situation has improved. Most databases allow for the use of UUID's/GUID's for row identities removing the need to query the database server after an insert to find out what value was assigned to the row and, now, many language's support Object Serialization.

Technically Serializing an object into a byte array / string has always been possible but without the appropriate language support, downright dangerous! And completely against the 'Safe Hex' practice which had been drilled into me from my very first line of code.
Today though, with languages like PHP and the .Net flavours, the risk of your code and your data source spawning a mutant object hell bent on turning your structured data into a garbled mess is practically impossible post-development and is a feature I really miss when going back to older projects.

When writing members to load, save or delete data today I find myself selecting the members which I want my users to be able to search on and leave everything else to be stored in the serialized object within a Binary object field of the table. Once the short amount of code needed to complete my 3 IO operations has been finished adding additional members to my object rearly means 6-7 other edits per member elsewhere.

For example, like most developers I try to keep all of my settings to one file and in PHP would often set the values within the globals array directly. However a major drawback of doing this is that unless you write some script to recreate the settings file the average user cannot modify the settings of their website.
To get around this we built a generic object which would serialize itself to a plain text file and use the member fields of the object to store settings, allowing us to store all of the settings in a way which was easy to modify and easy to maintain. The method for saving the websites settings is just:


 public function Save()
 {
  if(($user = User::getCurrentUser()) && $user->IsAdmin())
   return file_put_contents($this->SettingsFilePath,
    serialize($this))>0; 
  else 
   throw new SecurityException('Access Denied'); 
 }

Generally when storing data via SQL data is base 64 encoded to avoid binary data making a mess of the SQL statment and in PHP a modified version of base64_decode is used as PHP's built in function can only handle 4Kb of data.

 function base64_decode_long($value)
 {
   $result = '';
   for($i=0;$i<ceil(strlen($value)/256);$i++)
    $result .= base64_decode(substr($value,$i*256,256));
   return $result;
 }

No comments:

Post a Comment

Custom computer software development Bespoke software