I'm trying to serialize the class below:
public class Person : GLib.Object {
public string name { get; set; }
public int age { get; set; }
public bool alive { get; set; }
public Person (string name, int age, bool alive = true) {
Object (
name: name,
age: age,
alive: alive
);
}
}
public int main () {
var person = new Person ("mike", 33, false);
var node = Json.gobject_serialize (person);
string obj = Json.to_string (node, true);
print (obj+"\n");
return 0;
}
While i expect the output be...
{
"name": "mike",
"age": 32,
"alive": false
}
I'm getting...
{
"name": "mike",
"age": 32
}
How do i get the Boolean serialized even if the value is false?
The default serialization function of json-glib doesn't serialize a property if it contains its default value. For boolean properties, this is false
, unless explicitly specified otherwise.
To make sure that serialization does happen in this case, you should explicitly implement the Serializable.serialize_property()
method yourself.