PDA

View Full Version : Serializing Objects


killerogue
06-13-2007, 07:23 AM
Earlier, I was questioning Inverness concerning a few things and also leaning a few things from him concerning a system I wanted to create for a project. He suggested I create a way to serialize and un-serialize objects into strings.

It will be useful for the system I'm planning and required a bit of tweaking, but I decided it could be somewhat useful for the public.

Use: These two functions are for turning the values of all the vars in an object into a long string. And vice versa.

Credits:To Inverness, for the idea as well as quite a bit of help with the upper function. (I.E. Basically giving me the answer >_<, brain isn't functioning to well right now.)


public function serialize(obj) {
temp.string;
temp.var;

for (var : obj.getDynamicVarNames()) {
string @= obj.(@ var) @ ":";
}
return string;
}

public function deserialize(string) {
temp.array;
temp.i;
temp.sobj;

if (string.type() != 1)
return;

array = string.tokenize(":");

for (i = 0; i < array.size(); i ++) {
makevar("sobj.var" @ i) = array[i];
}
return sobj;
}

Inverness
06-13-2007, 08:35 AM
You need to do temp.varname = 0; not temp.varname; =P
And the serializing is supposed to be specific to that type of object >_>
Anyone could use savevarstoarray() to serialize any graal object.

Novo
06-13-2007, 01:19 PM
Thank god we all assume no strings EVER will use the ":" character.

Skyld
06-13-2007, 02:07 PM
Ideally your serialised string will have no bias towards certain characters. Like Novo said, your script will probably break if a string is used that contains the : character. It's safe to assume that variable names won't contain colons, so that makes it easier to process variable names, however the script needs to more reliably read the value.

The way that PHP does it is that your serialised string not only has the name and value of the object, but it also has a length value for the value. That way, you do not have to depend upon the positions of : characters.

An example:
foo:3:b:r:bar:4:ZOOM
... This would make a variable "foo" with the value "b:r", and another called "bar" with the value "ZOOM", because it's been instructed to read the 3 characters following the first instruction, and 4 characters for the second one, regardless of the position of colons in the string.

Novo
06-13-2007, 08:40 PM
Like Novo said, your script will probably break if a string is used that contains the : character.

WHAAA?! Now you're just putting words in my mouth! =]

killerogue
06-14-2007, 06:59 PM
Well, it's been working so far on my end without any breakage Skyld. :O

Skyld
06-14-2007, 07:19 PM
Well, it's been working so far on my end without any breakage Skyld. :O
I mean if you put a colon in one of your string values. It'll probably take it as a delimiter instead and cut off the string.

Inverness
06-15-2007, 03:25 AM
<snip>
Thats a nice idea, I'll be sure to implement it if I ever serialize something that could have a colon in it.
Thank god we all assume no strings EVER will use the ":" character.
Your sarcasm is most likely unappreciated by Stan. HINT HINT