Creates a new `GObject` instance of the given type, and constructs it using the members of the object in the given node.
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
gtype |
the type of the object to create |
node |
a node of type `JSON_NODE_OBJECT` describing the object instance for the given type |
The newly created instance |