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.



  • Latest stable release: 1.0 (here)


Parser example

#include <stdlib.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>

main (int argc, char *argv[])
  JsonParser *parser;
  JsonNode *root;
  GError *error;

  if (argc < 2)
      g_print ("Usage: test <filename.json>\n");
      return EXIT_FAILURE;

  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 >*/
} FooBlahEnum;

static const gchar *foo_object_json = "
  \"bar\"  : 42,
  \"baz\"  : true,
  \"blah\" : \"bravo\"

main (int   argc,
      char *argv[])
  FooObject *foo;
  gint bar_int;
  gboolean baz_boolean;
  FooBlahEnum blah_enum;
  GError *error;

  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,

  g_object_unref (foo);

  return EXIT_SUCCESS;

High level APIs

The 0.12 release added these new, high level classes for building and reading JSON trees

  • JsonBuilder: a simple API for building JSON trees

  JsonBuilder *builder = json_builder_new ();

  json_builder_begin_object (builder);

  json_builder_set_member_name (builder, "url");
  json_builder_add_string_value (builder, "http://www.gnome.org/img/flash/two-thirty.png");

  json_builder_set_member_name (builder, "size");
  json_builder_begin_array (builder);
  json_builder_add_int_value (builder, 652);
  json_builder_add_int_value (builder, 242);
  json_builder_end_array (builder);

  json_builder_end_object (builder);

  JsonGenerator *gen = json_generator_new ();
  JsonNode * root = json_builder_get_root (builder);
  json_generator_set_root (gen, root);
  gchar *str = json_generator_to_data (gen, NULL);

  json_node_free (root);
  g_object_unref (gen);
  g_object_unref (builder);

  /* "str" now contains the string:
   * { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] }
  • JsonReader: a cursor-based API for parsing JSON trees

  /* str contains the JSON from the example above */
  JsonParser *parser = json_parser_new ();
  json_parser_load_from_data (parser, str, -1, NULL);

  JsonReader *reader = json_reader_new (json_parser_get_root (parser));

  json_reader_read_member (reader, "url");
  const char *url = json_reader_get_string_value (reader);
  json_reader_end_member (reader);

  json_reader_read_member (reader, "size");
  json_reader_read_element (reader, 0);
  int width = json_reader_get_int_value (reader);
  json_reader_end_element (reader);
  json_reader_read_element (reader, 1);
  int height = json_reader_get_int_value (reader);
  json_reader_end_element (reader);
  json_reader_end_member (reader);

  g_object_unref (reader);
  g_object_unref (parser);
  • Asynchronous stream parsing

static void
on_load_complete (GObject      *gobject,
                  GAsyncResult *result,
                  gpointer      user_data)
  JsonParser *parser = JSON_PARSER (gobject);
  GError *error = NULL;
  gboolean res;

  res = json_parser_load_from_stream_finish (parser, result, &error);
  if (!res)
    g_error ("Unable to parse stream: %s", error->message);

  JsonReader *reader = json_reader_new (json_parser_get_root (parser));

  /* read the JSON tree */

  g_object_unref (reader);


  JsonParser *parser = json_parser_new ();

  json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream),
                                      on_load_complete, NULL);

Projects/JsonGlib (last edited 2017-03-11 15:50:40 by EmmanueleBassi)