Re: [Vala] Writing objects to files
- From: Steven Oliver <oliver steven gmail com>
- To: Vivien Kraus <vivien planete-kraus eu>
- Cc: vala-list <vala-list gnome org>
- Subject: Re: [Vala] Writing objects to files
- Date: Wed, 28 Mar 2018 13:44:02 -0400
I have not tried gom yet, but I did try the Json.gobject_serialize()
method as described by Christian Hergert. It almost does what I need
it to. It has some odd limitations.
For example, if this is your object everything works exactly as you
would expect.
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;
}
}
On the other hand, though, if you throw in some kind of data structure
besides your basic types (int, string, etc.) it completely fails.
public class MyObject : Object {
public string str { get; set; }
public MyEnum en { get; set; }
public int num { get; set; }
public int[] data;
public MyObject (string str, MyEnum en, int num) {
this.str = str;
this.num = num;
this.en = en;
this.data = {1,2,3,4,5};
}
}
Naturally, in my case I have complex data structures in my class so
the Json method did not work for me.
Steven N. Oliver
On Mon, Mar 26, 2018 at 5:09 PM, Vivien Kraus <vivien planete-kraus eu> wrote:
Hello,
I don't know what it's worth, but if you prefer XML this seems to be a
good solution: https://blogs.gnome.org/despinosa/2016/05/04/howto-gobje
ct-serialization-to-xml-using-gxml/
Vivien
Le lundi 26 mars 2018 à 14:02 -0700, Christian Hergert a écrit :
On 03/26/2018 11:44 AM, Steven Oliver wrote:
I want to implement "saving" in my program. The data I want to save
is
a custom object. Is it possible in vala to simply write the object
to
disk (in binary I assume) without having to implement a to_string
method?
It seems like such a simple concept yet i can't seem to figure out
how
to do it in Vala.
A very simple option (assuming you don't have an object graph to
serialize) could be to use either:
Json.gobject_serialize()
Json.gobject_to_data()
to serialize and
Json.gobject_deserialize()
Json.gobject_from_data()
to deserialize. If you need custom hooks into what will get
serialized,
you can implement Json.Serializable.
If you really want binary, GVariant is a rather nice serialization
format, but you might need to bring your own serializers. Json-GLib
has
some code for working with GVariant too, but I've not used it.
If you need an object graph, the Gom¹ library I made several years
back
can help, but it is more intrusive on your object design. I've only
used
it from C, but others have used it from Gjs, so I would expect it to
work in some form from Vala.
-- Christian
¹ https://git.gnome.org/browse/gom
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]