gobject_to_data


Description:


[ Version ( since = "0.10" ) ]
public string gobject_to_data (Object gobject, out size_t length)

Serializes a `GObject` instance into a JSON data stream, iterating recursively over each property.

If the given object implements the [[email protected]] interface, it will be asked to serialize all its properties; otherwise, the default implementation will be use to translate the compatible types into JSON native types.

Example: GObject serialization (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 MyObject (string str, MyEnum en, int num) {
this.str = str;
this.num = num;
this.en = en;
}
}

public static int main (string[] args) {
MyObject obj = new MyObject ("my string", MyEnum.FOOBAR, 10);

string data = Json.gobject_to_data (obj, null);
print (data);
print ("\n");

// Output:
// ``{``
// `` "str" : "my string",``
// `` "en" : 2,``
// `` "num" : 10``
// ``}``

return 0;
}

valac --pkg json-glib-1.0 serialization-data.vala

Parameters:

gobject

the object to serialize

length

return value for the length of the buffer

Returns:

a JSON data stream representing the given object


Namespace: Json
Package: json-glib-1.0



2022 vala-language.org