I am trying to use this library to serialize a class I wrote. Valadoc : gobject_to_data.
Suppose I have a class written like this:
public class MyObject : Object {
public string property_name { get; set; }
public MyObject (string str) {
this.property_name = property_name;
}
}
public static int main (string[] args) {
MyObject obj = new MyObject ("my string");
string data = Json.gobject_to_data (obj, null);
print (data);
print ("\n");
return 0;
}
The output I see is:
{
"property-name" : "my string"
}
What I want to do is modify the property name to instead be in "Snake case":
{
"property_name" : "my string"
}
How can I do that?
I have tried implementing serialize_property from the Serializable interface like this:
public class MyObject : Object : Json.Serializable {
public string property_name { get; set; }
public MyObject (string str) {
this.property_name = property_name;
}
public Json.Node serialize_property(string property_name, Value value, ParamSpec pspec) {
string property_name_camel_case = property_name.replace("-", "_");
Json.Node value_node = new Json.Node(Json.NodeType.VALUE);
value_node.set_value(value);
Json.Object final_object = new Json.Object();
final_object.set_member(property_name_camel_case, value_node);
return value_node;
}
}
public static int main (string[] args) {
MyObject obj = new MyObject ("my string");
string data = Json.gobject_to_data (obj, null);
print (data);
print ("\n");
return 0;
}
However, I still receive this output:
{
"property-name" : "my string"
}
Answer in case anyone else is looking for this
Unfortunately, JSON serialization does not support non-canonical names see here. When you are implementing
Json.Serializable.list_properties, you still cannot create any kind ofParamSpecwith a non-canonical name.Because of this, I had to resort to building a
Json.Objectand manually setting each of the key-value pairs myself like this: