Re: [Vala] Writing objects to files



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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]