libyaml-glib: YAML and GObject
- From: Yu Feng <rainwoodman gmail com>
- To: yaml-core lists sourceforge net, vala-list <vala-list gnome org>, gtk-devel-list <gtk-devel-list gnome org>
- Cc: gnome-announce-list <gnome-announce-list gnome org>, Кутейников Дмитрий <kuteynikov gmail com>
- Subject: libyaml-glib: YAML and GObject
- Date: Thu, 25 Jun 2009 15:44:04 +0800
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
http://github.com/fengy-research/libyaml-glib
The code can be obtained via
git clone git://github.com/fengy-research/libyaml-glib.git
The draft documentation(compiled by valadoc) is temporarily available at
http://www.does-exist.info/yaml-glib/Documentation/libyaml-glib-1.0/index.htm
More techy details are discussed under GYAMLBuilder, GYAMLBuildable, and
GYAMLDocument.
The attachment is a program that converts the standard invoice example
into GObject objects. After the library is properly installed, compile
with
valac --pkg libyaml-glib-1.0 libyaml-glib-builder.vala -X -export-dynamic
Your feedback is welcomed.
Regards,
Yu
[1] http://bugzilla.gnome.org/show_bug.cgi?id=584400
[2]
http://gore.svn.sourceforge.net/viewvc/gore/trunk/server/yaml_helper.c
http://gore.svn.sourceforge.net/viewvc/gore/trunk/server/libyaml-wrapper.vala
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 {
products.prepend((Product)child);
}
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",
bill_to.given,
bill_to.family,
bill_to.address.lines,
bill_to.address.city,
bill_to.address.state,
bill_to.address.postal);
sb.append_printf("%s %s \n %s %s %s %s\n",
ship_to.given,
ship_to.family,
ship_to.address.lines,
ship_to.address.city,
ship_to.address.state,
ship_to.address.postal);
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
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- 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
comments:
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
""";
public static void main(string[] args) {
Builder b = new Builder();
b.add_from_string(buffer);
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]