gobject_deserialize


Description:

[ Version ( since = "0.10" ) ]
public Object gobject_deserialize (Type gtype, Node node)

Creates a new Object of type gtype, and constructs it using the members of the passed Object

Example: GObject deserialization:

public enum MyEnum {
FOO, BAR, FOOBAR
}

public class MyObject : Object {
public string str { get; set; }
public MyEnum en { get; set; }
public int num { get; set; }

public string to_string () {
StringBuilder builder = new StringBuilder ();
builder.append_printf ("str = %s\n", str);
builder.append_printf ("en = %s\n", en.to_string ());
builder.append_printf ("num = %d", num);
return (owned) builder.str;
}
}

public static int main (string[] args) {
string data = """
{
"str" : "my string",
"en" : 2,
"num" : 10
}"""
;


// See Json.gobject_from_data
try {
// Create a Json.Node:
Json.Parser parser = new Json.Parser ();
parser.load_from_data (data);

Json.Node node = parser.get_root ();

// Deserialization:
MyObject obj = Json.gobject_deserialize (typeof (MyObject), node) as MyObject;
assert (obj != null);

// Output:
// ``str = my string``
// ``en = MY_ENUM_FOOBAR``
// ``num = 10``
print (obj.to_string ());
print ("\n");
} catch (Error e) {
print ("Error: %s\n", e.message);
}

return 0;
}

valac --pkg json-glib-1.0 deserialization.vala

Parameters:

gtype

the type of the Object to create

node

a Node of type json_node_object describing the instance of type gtype

Returns:

The newly created Object instance. Use unref to free the resources allocated by this function


Namespace: Json
Package: json-glib-1.0