Re: [Vala] Is there any way to deserialize something into object in vala?
- From: Derek Dai <daiderek gmail com>
- To: vala-list gnome org
- Subject: Re: [Vala] Is there any way to deserialize something into object in vala?
- Date: Fri, 23 Mar 2012 20:03:56 +0800
interface Shape : Object
{
public abstract string name { get; set; }
public abstract void dump();
}
class Point : Object, Shape
{
public string name { get; set; }
public int x { get; set; }
public int y { get; set; }
public Point(string name, int x, int y)
{
this.name = name;
this.x = x;
this.y = y;
}
public void dump()
{
message("%s(%p)[name=%s,x=%d,y=%d]", get_type().name(), this, name, x, y);
}
}
class NameValue
{
public unowned string name;
public Value value;
}
int main()
{
Shape o = new Point("p1", 1000, 500);
o.dump();
// Serialize o
string type_name = o.get_type().name();
List<NameValue> props = null;
foreach(unowned ParamSpec pspec in o.get_class().list_properties()) {
var nv = new NameValue();
nv.name = pspec.name;
nv.value.init(pspec.value_type);
o.get_property(pspec.name, ref nv.value);
props.prepend(nv);
}
o = null;
// Deserialize o
o = Object.new(Type.from_name(type_name)) as Shape;
foreach(unowned NameValue prop in props) {
o.set_property(prop.name, prop.value);
}
o.dump();
return 0;
}
# Makefile
all: test
test: test.vala
valac -o test test.vala
clean:
rm -rf test
$ make clean all && ./test
rm -rf test.c test.h test
valac --ccode --use-header --header test.h test.vala
valac -o test test.vala
** Message: test.vala:25: Point(0x8055600)[name=p1,x=1000,y=500]
** Message: test.vala:25: Point(0x8055620)[name=p1,x=1000,y=500]
Derek Dai
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]