JSON-GLib
About
JSON-GLib is a library providing serialization and deserialization support for the JavaScript Object Notation (JSON) format described by RFC 4627. JSON is:
a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language. -- From Introducing JSON
Many high-level languages already provide native modules for parsing, generating and manipulating JSON data streams. JSON-GLib is a C library based on GLib and released under the terms of the GNU Lesser General Public License version 2.1. It provides a parser and a generator GObject classes and various wrappers for the complex data types employed by JSON, such as arrays and objects.
JSON-GLib uses GLib native data types and the generic value container GValue for ease of development. It also provides integration with the GObject classes for direct serialization into, and deserialization from, JSON data streams.
Development
Maintainer: EmmanueleBassi
main SVN repository: svn co http://svn.gnome.org/svn/json-glib/trunk json-glib
development git repository: git clone git://github.com/ebassi/json-glib.git
Bugzilla: Enter new bug for json-glib
Releases
Latest stable release: 0.6.2 (download, MD5 checksum: d98f5580035ad0b37fa11896053a57af)
Examples
Parser example
#include <stdlib.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>
int
main (int argc, char *argv[])
{
JsonParser *parser;
JsonNode *root;
GError *error;
if (argc < 2)
{
g_print ("Usage: test <filename.json>\n");
return EXIT_FAILURE;
}
g_type_init ();
parser = json_parser_new ();
error = NULL;
json_parser_load_from_file (parser, argv[1], &error);
if (error)
{
g_print ("Unable to parse `%s': %s"\n, argv[1], error->message);
g_error_free (error);
g_object_unref (parser);
return EXIT_FAILURE;
}
root = json_parser_get_root (parser);
/* manipulate the object tree and then exit */
g_object_unref (parser);
return EXIT_SUCCESS;
}
Deserialization Sample
#include <json-glib/json-glib.h>
#include <json-glib/json-gobject.h>
#define FOO_TYPE_OBJECT (foo_object_get_type ())
/* usual GObject boilerplate */
typedef enum { /*< prefix=FOO_BLAH >*/
FOO_BLAH_ALPHA,
FOO_BLAH_BRAVO,
FOO_BLAH_CHARLIE
} FooBlahEnum;
static const gchar *foo_object_json = "
{
\"bar\" : 42,
\"baz\" : true,
\"blah\" : \"bravo\"
}
";
int
main (int argc,
char *argv[])
{
FooObject *foo;
gint bar_int;
gboolean baz_boolean;
FooBlahEnum blah_enum;
GError *error;
g_type_init ();
error = NULL;
foo = json_construct_gobject (FOO_TYPE_OBJECT, foo_object_json, -1, &error);
if (error)
g_error ("Unable to create instance: %s", error->message);
/* FooObject has three properties: bar, blah and baz */
g_object_get (G_OBJECT (foo),
"bar", &bar_int,
"baz", &baz_boolean,
"blah", &blah_enum,
NULL);
g_object_unref (foo);
return EXIT_SUCCESS;
}