summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@openedhand.com>2007-11-21 18:39:04 +0000
committerEmmanuele Bassi <ebassi@openedhand.com>2007-11-21 18:39:04 +0000
commit5f110dc5ad338497c95418b9ad1b267c95fb8ded (patch)
treec7076536636639a7bf7aacf9ca039437bd6b1094
parent557a60b4e0f1fd6fc0fa79efc60f9bfd777e0670 (diff)
downloadjson-glib-5f110dc5ad338497c95418b9ad1b267c95fb8ded.tar.gz
Add JSON-GLib Vala bindings
Add bindings for the basic JSON-GLib API. GObject API will arrive later.
-rw-r--r--Makefile.am2
-rw-r--r--configure.ac12
-rw-r--r--contrib/Makefile.am4
-rw-r--r--contrib/json-glib-1.0.vapi118
-rw-r--r--contrib/json-test.vala87
5 files changed, 222 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index 850897c..c4ddf7e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = json-glib doc tests
+SUBDIRS = json-glib doc tests contrib
pcfiles = json-glib-1.0.pc
diff --git a/configure.ac b/configure.ac
index 53850a5..2481b39 100644
--- a/configure.ac
+++ b/configure.ac
@@ -53,6 +53,16 @@ AC_FUNC_MMAP
PKG_CHECK_MODULES(JSON, gobject-2.0 >= glib_req_version)
+has_vala=no
+PKG_CHECK_MODULES(JSON_VALA, vala-1.0, has_vala=yes, has_vala=no)
+
+if test "x$has_vala" = "xyes"; then
+ VAPI_DIR=`pkg-config --variable=vapidir vala-1.0`
+else
+ VAPI_DIR=
+fi
+AC_SUBST(VAPI_DIR)
+
dnl = Enable debug level ===================================================
m4_define([debug_default],
@@ -104,6 +114,7 @@ AC_CONFIG_FILES([
doc/Makefile
doc/reference/Makefile
doc/reference/version.xml
+ contrib/Makefile
json-glib.pc
])
@@ -116,4 +127,5 @@ echo " Prefix: ${prefix}"
echo " Debug level: ${enable_debug}"
echo " Compiler flags: ${CPPFLAGS}"
echo " API reference: ${enable_gtk_doc}"
+echo " Install vala bindings: ${has_vala} (${VAPI_DIR})"
echo ""
diff --git a/contrib/Makefile.am b/contrib/Makefile.am
new file mode 100644
index 0000000..33325ba
--- /dev/null
+++ b/contrib/Makefile.am
@@ -0,0 +1,4 @@
+vapidir = @VAPI_DIR@
+vapi_DATA = json-glib-1.0.vapi
+
+EXTRA_DIST = json-test.vala
diff --git a/contrib/json-glib-1.0.vapi b/contrib/json-glib-1.0.vapi
new file mode 100644
index 0000000..9b531fa
--- /dev/null
+++ b/contrib/json-glib-1.0.vapi
@@ -0,0 +1,118 @@
+/* json-glib-1.0.vapi: Vala bindings for JSON-GLib
+ *
+ * Copyright (C) 2007 OpenedHand Ltd.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Author: Emmanuele Bassi <ebassi@openedhand.com>
+ */
+
+[Import ()]
+[CCode (cprefix = "Json", lower_case_cprefix = "json_", cheader_filename = "json-glib/json-glib.h")]
+namespace Json {
+ [CCode (ref_function = "json_object_ref", unref_function = "json_object_unref")]
+ public class Object : GLib.Boxed {
+ public Json.Object ();
+ public add_member (string name, Json.Node node);
+ public bool has_member (string name);
+ public weak Json.Node get_member (string name);
+ public GLib.List<string> get_members ();
+ public GLib.List<weak Json.Node> get_values ();
+ public void remove_member (string name);
+ public uint get_size ();
+ }
+
+ [CCode (ref_function = "json_array_ref", unref_function = "json_array_unref")]
+ public class Array : GLib.Boxed {
+ public Array ();
+ [CCode (cname = "json_array_sized_new")]
+ public Array.sized (uint reserved_size);
+ public add_element (Json.Node node);
+ public weak Json.Node get_element (uint index_);
+ public GLib.List<weak Json.Node> get_elements ();
+ public void remove_element (uint index_);
+ public uint get_length ();
+ }
+
+ [CCode (cprefix = "JSON_NODE_", cheader_filename = "json-glib/json-types.h")]
+ public enum NodeType {
+ OBJECT,
+ ARRAY,
+ VALUE,
+ NULL
+ }
+
+ [CCode (copy_function = "json_node_copy", free_function = "json_node_free", type_id = "JSON_TYPE_NODE")]
+ public class Node : GLib.Boxed {
+ public NodeType type;
+ public Node (NodeType type);
+ public Node copy ();
+ public GLib.Type get_value_type ();
+ public void set_object (Json.Object v_object);
+ public void take_object (Json.Object# v_object);
+ public weak Json.Object get_object ();
+ public Json.Object dup_object ();
+ public void set_array (Json.Array v_array);
+ public void take_array (Json.Array# v_array);
+ public weak Json.Array get_array ();
+ public Json.Array dup_array ();
+ public void set_string (string value);
+ public weak string get_string ();
+ public string dup_string ();
+ public void set_int (int value);
+ public int get_int ();
+ public void set_double (double value);
+ public double get_double ();
+ public void set_boolean (bool value);
+ public bool get_boolean ();
+ public weak Json.Node get_parent ();
+ public weak string type_name ();
+ }
+
+ [CCode (cprefix = "JSON_PARSER_ERROR_", cheader_filename = "json-glib/json-parser.h")]
+ public enum ParserError {
+ PARSE,
+ UNKNOWN
+ }
+
+ [CCode (cheader_filename = "json-glib/json-parser.h")]
+ public class Parser : GLib.Object {
+ public Parser ();
+ public bool load_from_file (string filename) throws GLib.Error;
+ public bool load_from_data (string buffer, ulong length) throws ParserError;
+ public weak Json.Node get_root ();
+ public uint get_current_line ();
+ public uint get_current_pos ();
+ public bool has_assingment (out string variable_name);
+ public signal void parse_start ();
+ public signal void parse_end ();
+ public signal void object_start ();
+ public signal void object_member (Json.Object object, string name);
+ public signal void object_end (Json.Object object);
+ public signal void array_start ();
+ public signal void array_element (Json.Array array, uint index_);
+ public signal void array_end (Json.Array array);
+ public signal void error (GLib.Error error);
+ }
+
+ [CCode (cheader_filename = "json-glib/json-generator.h")]
+ public class Generator : GLib.Object {
+ public Generator ();
+ public string to_data (out ulong length);
+ public bool to_file (string! filename) throws GLib.FileError;
+ public void set_root (Json.Node node);
+ }
+}
diff --git a/contrib/json-test.vala b/contrib/json-test.vala
new file mode 100644
index 0000000..744c5fa
--- /dev/null
+++ b/contrib/json-test.vala
@@ -0,0 +1,87 @@
+/* json-test.vala - Test app for the JSON-GLib Vala bindings
+ *
+ * Copyright (C) 2007 OpenedHand Ltd.
+ *
+ * Released under the terms of the GNU General Public License,
+ * version 2.1.
+ *
+ * Author: Emmanuele Bassi <ebassi@openedhand.com>
+ */
+
+using GLib;
+using Json;
+
+public class Sample : GLib.Object {
+ public void parse (string buffer) {
+ var parser = new Json.Parser ();
+
+ parser.parse_start += p => {
+ stdout.printf ("parsing started\n");
+ };
+
+ parser.array_end += (p, a) => {
+ stdout.printf ("array (lenght: %d) parsed\n",
+ a.get_length ());
+ };
+
+ parser.object_end += (p, o) => {
+ stdout.printf ("object (size: %d) parsed\n",
+ o.get_size ());
+
+ var node = new Json.Node (Json.NodeType.OBJECT);
+ node.set_object (o);
+
+ var gen = new Json.Generator ();
+ gen.set_root (node);
+
+ var len = 0;
+ var dump = gen.to_data (ref len);
+ stdout.printf ("** object dump (length: %d): %s\n",
+ len, dump);
+ };
+
+ parser.parse_end += p => {
+ stdout.printf ("parsing complete\n");
+ };
+
+ try { parser.load_from_data (buffer, -1); }
+ catch (Json.ParserError e) {
+ stdout.printf ("%s\n", e.message);
+ return;
+ }
+
+ var root = parser.get_root ().copy ();
+ stdout.printf ("root node type: %s\n", root.type_name ());
+
+ switch (root.type) {
+ case Json.NodeType.OBJECT:
+ break;
+ case Json.NodeType.ARRAY:
+ var array = root.get_array ();
+ stdout.printf ("* length: %d\n", array.get_length ());
+
+ var count = 0;
+ foreach (weak Json.Node element in array.get_elements ()) {
+ stdout.printf ("* element[%d] type: %s\n",
+ count++,
+ element.type_name ());
+ }
+ break;
+ case Json.NodeType.VALUE:
+ break;
+ case Json.NodeType.NULL:
+ stdout.printf ("null node\n");
+ break;
+ }
+
+ return;
+ }
+
+ static int main (string[] args) {
+ var sample = new Sample ();
+
+ sample.parse ("[ 42, true, { \"foo\" : \"bar\" } ]");
+
+ return 0;
+ }
+}