gobject_from_data
Description:
[ Version ( since = "0.10" ) ]
public Object? gobject_from_data (Type gtype, string data, ssize_t length = -1) throws Error
Deserializes a JSON data stream and creates an instance of the given type.
If the type implements the [iface@Json.Serializable] interface, it will be asked to deserialize all the JSON members into their respective properties; otherwise, the default implementation will be used to translate the compatible JSON native types.
**Note**: the JSON data stream must be an object
Example: GObject deserialization (string):
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
}""";
try {
MyObject obj = Json.gobject_from_data (typeof (MyObject), data) 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-data.vala
Parameters:
gtype |
the type of the object to construct |
data |
a JSON data stream |
length |
length of the data stream, or -1 if it is `NUL`-terminated |
Returns:
a new object instance of the given type |
Namespace: Json
Package: json-glib-1.0