Skip to content

Instantly share code, notes, and snippets.

@gergelypolonkai
Created October 4, 2016 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergelypolonkai/ec346648b390de343f35c0aeb25418e8 to your computer and use it in GitHub Desktop.
Save gergelypolonkai/ec346648b390de343f35c0aeb25418e8 to your computer and use it in GitHub Desktop.
Vala Serialization example
/* -*- 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 chante 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) {
Value strval = Value (typeof(string));
value.transform (ref strval);
}
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
}
// }}}
/**
* 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);
}
// }}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment