Hi,

I'm trying to implement Json.Serializable interface.
For the moment, I've just made a lazy implementation (see the attached
file), which mocks default behavior of Glib.Object serialization.

To test :
class DummyObject : Core.JsonApi.SimpleObject {
    // declare properties here
}

int main (string[] argv) {
    var obj = new DummyObject ();
    message("%s", Json.gobject_to_data (obj, null));
    return 0;
}

There is 2 issues :

The 'list_properties' is never called (it doesn't override the base one).
If I add manually the line the C generated file, in the
core_json_api_simple_object_json_serializable_interface_init method :
iface->list_properties = (GParamSpec* (*)(JsonSerializable*))
core_json_api_simple_object_list_properties;
It works just great.

String properties are handled wearily.
A quick view into the glib code learns me that properties with default
value are note serialized (return a null Node).
But, in vala (don't know for C), string are always serialized, like if
pointers was compared instead of value (maybe binding issue between vala
and C, because in the glib code it is strcmp which is used for string).
And with vala 0.26 (under debian), I get that (label is a string with
"name" as default value) :
...
** (process:5271): DEBUG: core-json-api-simple-object.vala:54:
get_property(label) => name
** (process:5271): DEBUG: core-json-api-simple-object.vala:31:
serialize_property(gchararray label) => \xb0\xf7\\u0001
...
With vala 0.28 (with mingw under windows), the property is steal
serialized, but in this case the result is correct.

Did I miss something, or is there a binding problem with json-glib ?

Thank you.
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * core-json-api-simple-object.vala
 * Copyright (C) 2015 Luc Chante <luc.cha...@gmail.com>
 *
 */

public class Core.JsonApi.SimpleObject : Object, Json.Serializable {
	
	public bool serialize_default_value = false;
	
	public SimpleObject () {
		Object ();
	}
	
	// {{{ Serialization
	/**
	 * Calls the JsonSerializableIface.list_propertiess implementation on the this instance.
	 */
	public new ParamSpec[] list_properties () {
		debug ("list_properties()");
		return get_class ().list_properties ();
	}
	
	/**
	 * Asks a Serializable implementation to serialize a Object property into a Node object.
	 */
	public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
		Json.Node node = default_serialize_property (property_name, value, pspec);
		if (node != null)
			debug ("serialize_property(%s %s) => %s", Core.get_fundamental (pspec.value_type).name (), property_name, node.dup_string ());
		else {
			Value strval = Value (typeof(string));
			value.transform (ref strval);
			debug ("serialize_property(%s %s, %s)", Core.get_fundamental (pspec.value_type).name (), property_name, strval.get_string ());
		}

		return node;
	}
	
	/**
	 * Calls the get_property implementation on the this instance.
	 */
#if VALA_0_28
	public new Value get_property (ParamSpec pspec) {
#else
	public new void get_property (ParamSpec pspec, Value value) {
#endif
		Value result = Value (pspec.value_type);
		base.get_property (pspec.name, ref result);
		
		Value strval = Value (typeof (string));
		result.transform (ref strval);
		debug ("get_property(%s) => %s", pspec.name, strval.get_string ());
		
#if VALA_0_28
		return result;
#else
		value = result;
#endif
	}
	// }}}
	
	// {{{ Deserialization
	/**
	 * Calls the find_property implementation on the this instance.
	 */
	public unowned ParamSpec find_property (string name) {
		debug ("find_property(%s)", name);
		return get_class ().find_property (name);
	}
	
	/**
	 * Asks a Serializable implementation to deserialize the property contained inside property_node into value.
	 */
	public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
		debug ("deserialize_property(%s)", property_name);
		value = Value (pspec.value_type);
		return default_deserialize_property (property_name, value, pspec, property_node);
	}
	
	/**
	 * Calls the set_property implementation on the this instance.
	 */
	public new void set_property (ParamSpec pspec, Value value) {
		debug ("set_property(%s)", pspec.name);
		base.set_property (pspec.name, value);
	}
	// }}}
}
_______________________________________________
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to