Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Serializing Objects (https://forums.graalonline.com/forums/showthread.php?t=74568)

killerogue 06-13-2007 06:23 AM

Serializing Objects
 
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.)

PHP Code:

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 (
0< array.size(); ++) {
    
makevar("sobj.var" i) = array[i];
  }
  return 
sobj;



Inverness 06-13-2007 07: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 12:19 PM

Thank god we all assume no strings EVER will use the ":" character.

Skyld 06-13-2007 01: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 07:40 PM

Quote:

Originally Posted by Skyld (Post 1317632)
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 05:59 PM

Well, it's been working so far on my end without any breakage Skyld. :O

Skyld 06-14-2007 06:19 PM

Quote:

Originally Posted by killerogue (Post 1317963)
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 02:25 AM

Quote:

Originally Posted by Skyld (Post 1317632)
<snip>

Thats a nice idea, I'll be sure to implement it if I ever serialize something that could have a colon in it.
Quote:

Originally Posted by Novo (Post 1317631)
Thank god we all assume no strings EVER will use the ":" character.

Your sarcasm is most likely unappreciated by Stan. HINT HINT


All times are GMT +2. The time now is 08:58 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.