Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Nov 15th, 2007, 1:17 AM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Messing with Serialization

For a more detailed reason on why I'm doing this read here.

The idea behind this is that when PHP serializes an array it generates a string that contains all the information to recreate the array. However, I would like to be able to separate the array keys (keys that are exclusively strings) from the serialized string itself. So that I would have the keys in one string and the values of the array in another. I would also like to be able to recombine the two strings into the original serialized string. So here's the code I wrote up to do this:
class gbSerializer {
	var $serialString = '';
	var $keys = '';
	var $values = '';
	
	function gbSerializer($keys, $values = null){
		if(!isset($values)){
			$this->serialString = $keys;
			
			$count = 0;
			do{
				$lengthMarker = 0;
				switch(substr($keys, 0, 1)){
					case 'a':
					$lengthMarker = strpos($keys, '{') + 1;
					break;
					case 's':
					$lengthMarker = intval(substr($keys, 2, strpos($keys, ':', 2)));
					$lengthMarker += strpos($keys, '"') + 3;
					break;
					case 'i':
					case 'N':
					case 'b':
					$lengthMarker = strpos($keys, ';') + 1;
					break;
					case '}':
					$lengthMarker++;
					break;
				}
				$temp = substr($keys, 0, $lengthMarker);
				$keys = substr($keys, $lengthMarker);
				$iniChar = substr($temp, 0, 1);
				switch($iniChar){
					case 'a':
					$count = 0;
					$this->values .= $temp;
					$this->keys .= '{';
					break;
					case '}':
					$count = 0;
					$this->values .= $temp;
					$this->keys .= '}';
					break;
					default:
					switch($count % 2){
						case 0:
						if(!strcmp($iniChar, 'i')){
							$this->values .= $temp;
						}else{
							$this->keys .= $temp;
						}
						break;
						case 1:
						$this->values .= $temp;
						break;
					}
					$count++;
					break;
				}
			}while(strlen($keys) > 0);
		}else{
			$this->keys = $keys;
			$this->values = $values;

			do{
				$lengthMarker = 0;
				switch(substr($keys, 0, 1)){
					case '{':
					$keys = substr($keys, 1);
					$lengthMarker = strpos($values, '{') + 1;
					$this->serialString .= substr($values, 0, $lengthMarker);
					$values = substr($values, $lengthMarker);
					break;
					case '}':
					$lengthMarker++;
					$this->serialString .= substr($values, 0, $lengthMarker);
					$keys = substr($values, $lengthMarker);
					$values = substr($values, $lengthMarker);
					break;
					default:
					// will produce an error that can be ignored
					$lengthMarker = intval(substr($keys, 2, @strpos($keys, ':', 2)));
					$lengthMarker += strpos($keys, '"') + 3;
					$temp = substr($keys, 0, $lengthMarker);
					$this->serialString .= $temp;
					$lengthMarker++;
					$arrFlag = false;
					switch(substr($keys, $lengthMarker, 1)){
						case ':':
						$lengthMarker--;
						break;
						case '}':
						$lengthMarker++;
						$arrFlag = true;
						break;
					}
					$keys = substr($keys, $lengthMarker);
					do{
						$lengthMarker = 0;
						switch(substr($values, 0, 1)){
							case 'a':
							$lengthMarker = strpos($values, '{') + 1;
							break;
							case 's':
							$lengthMarker = intval(substr($values, 2, strpos($values, ':', 2)));
							$lengthMarker += strpos($values, '"') + 3;
							break;
							case 'i':
							case 'N':
							case 'b':
							$lengthMarker = strpos($values, ';') + 1;
							break;
							case '}':
							$lengthMarker++;
							$arrFlag = false;
							break;
						}
						$this->serialString .= substr($values, 0, $lengthMarker);
						$values = substr($values, $lengthMarker);
					}while($arrFlag);
					if(strlen($values) == 0 && strlen($keys) != 0){
						$this->serialString = substr($this->serialString, 0, -1 * strlen($temp) - 1) . '}';
					}
					break;
				}
			}while(strlen($values) > 0);
		}
	}
	
	function getKeys(){
		return $this->keys;
	}
	
	function getValues(){
		return $this->values;
	}
	
	function getSerial(){
		return $this->serialString;
	}
}
Pretty lengthy as you can tell. Now I'm pretty confident about the constructor when it is given a serialized string and it removes the keys (it also inserts brackets into the keys so as to indicate the nested structure of the array). However, I'm not so confident about the constructor when it is given keys and values to try and output a serialized string. It works with what I've tried, but there are instances where an error is generated which I just put in an @ to ignore. I was hoping someone with some free time might be able to offer an alternative. Thanks for any help.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialization grimpirate Software Design and Algorithms 7 Oct 31st, 2007 11:35 PM
how to transfer object using socket (tcp/ip) amitpansuria C++ 4 Aug 28th, 2007 5:44 AM
Binary Serialization vs. Byte Streams kurifu C# 1 Apr 7th, 2007 5:17 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC