libyaml-glib: YAML and GObject

Dear list(s),

I have been playing around GLib and libyaml with vala for a couple of
months. While I don't see a clear future of the code, I would like to
post a link to my code in case it will be useful to others.

The library is written in VALA, but one can write applications and link
against it in the same way as against other traditional GLib libraries.

This library provides the geometry object builder for a simulation
program I was writing at IU. It includes two parts:

a) a native representation of YAML nodes in GLib data type.
  GList <-> Sequence
  GHashTable <-> Mapping
  gchar * <-> Scalar
However, as the fundamental containers in GLib are type-unaware,
decorators(is it the correct name?) are added to store the
type-information, as well as extra document stream information. 

  GYAMLNodeSequence <-> Sequence
  GYAMLNodeMapping <-> Mapping
  GYAMLNodeScalar <-> Scalar
  GYAMLNodeAnchor <-> Anchor

The corresponding data structures in libyaml are not used, as they do
not work nicely with vala.

this part is based on the yaml wrapper in gore (gore @ sourceforge)[2];

b) an object builder that builds GObject from YAML,

The builder scans the YAML tree created by GYAMLParser, and builds
GYAMLBuildable objects from the document tree. GYAMLBuildable is
designed in such a way that it is compatible with GtkBuildable unless
the API is XML/YAML specific; nevertheless so far the interface is not
interchangeable with GtkBuildable, because one of the changes needed in
vala 'doen't make sense'.[1]

A short installation guide is at

The code can be obtained via
git clone git://

The draft documentation(compiled by valadoc) is temporarily available at

More techy details are discussed under GYAMLBuilder, GYAMLBuildable, and

The attachment is a program that converts the standard invoice example
into GObject objects. After the library is properly installed, compile
valac --pkg libyaml-glib-1.0 libyaml-glib-builder.vala -X -export-dynamic

Your feedback is welcomed.




using GLib.YAML;

public class Invoice: GLib.Object, Buildable {
	public int invoice {get; set;}
	public string date {get; set;}
	public Contact bill_to {get; set;}
	public Contact ship_to {get; set;}
	public double tax {get; set;}
	public double total {get; set;}
	public string comments {get; set;}

	private List<Product> products;

	public void add_child(Builder builder, GLib.Object child, string? type) throws GLib.Error {
	public Type get_child_type (Builder builder, string tag) {
		if(tag == "product") {
			return typeof(Product);
		return Type.INVALID;
	public string summary(StringBuilder? sb = null) {
		StringBuilder sb_ref = null;
		if(sb == null) {
			sb_ref = new StringBuilder("");
			sb = sb_ref;
		sb.append_printf("%d\n", invoice);
		sb.append_printf("%s\n", date);
		sb.append_printf("%g\n", tax);
		sb.append_printf("%g\n", total);
		sb.append_printf("%s\n", comments);
		foreach(var p in products) {
			sb.append_printf("%s %d %s %g\n", p.sku, p.quantity, p.description, p.price);
		sb.append_printf("%s %s \n %s %s %s %s\n", 
		sb.append_printf("%s %s \n %s %s %s %s\n", 

		return sb.str;

public class Product : GLib.Object, Buildable {
	public string sku {get; set;}
	public int quantity {get; set;}
	public string description {get; set;}
	public double price {get; set;}
public class Contact : GLib.Object, Buildable {
	public string given {get; set;}
	public string family {get; set;}
	public Address address {get; set;}
public class Address : Object, Buildable {
	public string lines {get; set;}
	public string city {get; set;}
	public string state {get; set;}
	public string postal {get; set;}

const string buffer =
# This is the YAML 1.1 example. The YAML 1.2 example fails.
--- !Invoice
invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.

public static void main(string[] args) {
	Builder b = new Builder();
	Invoice invoice = b.get_root_object() as Invoice;
	stdout.printf("%s", invoice.summary());

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