This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

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

Releases

Examples

Parser example

   1 #include <stdlib.h>
   2 #include <glib-object.h>
   3 #include <json-glib/json-glib.h>
   4 
   5 int
   6 main (int argc, char *argv[])
   7 {
   8   JsonParser *parser;
   9   JsonNode *root;
  10   GError *error;
  11 
  12   if (argc < 2)
  13     {
  14       g_print ("Usage: test <filename.json>\n");
  15       return EXIT_FAILURE;
  16     }
  17 
  18   parser = json_parser_new ();
  19 
  20   error = NULL;
  21   json_parser_load_from_file (parser, argv[1], &error);
  22   if (error)
  23     {
  24       g_print ("Unable to parse `%s': %s\n", argv[1], error->message);
  25       g_error_free (error);
  26       g_object_unref (parser);
  27       return EXIT_FAILURE;
  28     }
  29 
  30   root = json_parser_get_root (parser);
  31 
  32   /* manipulate the object tree and then exit */
  33 
  34   g_object_unref (parser);
  35 
  36   return EXIT_SUCCESS;
  37 }

Deserialization Sample

   1 #include <json-glib/json-glib.h>
   2 #include <json-glib/json-gobject.h>
   3 
   4 #define FOO_TYPE_OBJECT (foo_object_get_type ())
   5 
   6 /* usual GObject boilerplate */
   7 
   8 typedef enum { /*< prefix=FOO_BLAH >*/
   9   FOO_BLAH_ALPHA,
  10   FOO_BLAH_BRAVO,
  11   FOO_BLAH_CHARLIE
  12 } FooBlahEnum;
  13 
  14 static const gchar *foo_object_json = "
  15 {
  16   \"bar\"  : 42,
  17   \"baz\"  : true,
  18   \"blah\" : \"bravo\"
  19 }
  20 ";
  21 
  22 int
  23 main (int   argc,
  24       char *argv[])
  25 {
  26   FooObject *foo;
  27   gint bar_int;
  28   gboolean baz_boolean;
  29   FooBlahEnum blah_enum;
  30   GError *error;
  31 
  32   error = NULL;
  33   foo = json_construct_gobject (FOO_TYPE_OBJECT, foo_object_json, -1, &error);
  34   if (error)
  35     g_error ("Unable to create instance: %s", error->message);
  36 
  37   /* FooObject has three properties: bar, blah and baz */
  38   g_object_get (G_OBJECT (foo),
  39                 "bar", &bar_int,
  40                 "baz", &baz_boolean,
  41                 "blah", &blah_enum,
  42                 NULL);
  43 
  44   g_object_unref (foo);
  45 
  46   return EXIT_SUCCESS;
  47 }

High level APIs

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

   1   JsonBuilder *builder = json_builder_new ();
   2 
   3   json_builder_begin_object (builder);
   4 
   5   json_builder_set_member_name (builder, "url");
   6   json_builder_add_string_value (builder, "http://www.gnome.org/img/flash/two-thirty.png");
   7 
   8   json_builder_set_member_name (builder, "size");
   9   json_builder_begin_array (builder);
  10   json_builder_add_int_value (builder, 652);
  11   json_builder_add_int_value (builder, 242);
  12   json_builder_end_array (builder);
  13 
  14   json_builder_end_object (builder);
  15 
  16   JsonGenerator *gen = json_generator_new ();
  17   JsonNode * root = json_builder_get_root (builder);
  18   json_generator_set_root (gen, root);
  19   gchar *str = json_generator_to_data (gen, NULL);
  20 
  21   json_node_free (root);
  22   g_object_unref (gen);
  23   g_object_unref (builder);
  24 
  25   /* "str" now contains the string:
  26    * { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] }
  27    */

   1   /* str contains the JSON from the example above */
   2   JsonParser *parser = json_parser_new ();
   3   json_parser_load_from_data (parser, str, -1, NULL);
   4 
   5   JsonReader *reader = json_reader_new (json_parser_get_root (parser));
   6 
   7   json_reader_read_member (reader, "url");
   8   const char *url = json_reader_get_string_value (reader);
   9   json_reader_end_member (reader);
  10 
  11   json_reader_read_member (reader, "size");
  12   json_reader_read_element (reader, 0);
  13   int width = json_reader_get_int_value (reader);
  14   json_reader_end_element (reader);
  15   json_reader_read_element (reader, 1);
  16   int height = json_reader_get_int_value (reader);
  17   json_reader_end_element (reader);
  18   json_reader_end_member (reader);
  19 
  20   g_object_unref (reader);
  21   g_object_unref (parser);

   1 static void
   2 on_load_complete (GObject      *gobject,
   3                   GAsyncResult *result,
   4                   gpointer      user_data)
   5 {
   6   JsonParser *parser = JSON_PARSER (gobject);
   7   GError *error = NULL;
   8   gboolean res;
   9 
  10   res = json_parser_load_from_stream_finish (parser, result, &error);
  11   if (!res)
  12     g_error ("Unable to parse stream: %s", error->message);
  13 
  14   JsonReader *reader = json_reader_new (json_parser_get_root (parser));
  15 
  16   /* read the JSON tree */
  17 
  18   g_object_unref (reader);
  19 }
  20 
  21 ...
  22 
  23   JsonParser *parser = json_parser_new ();
  24 
  25   json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream),
  26                                       NULL,
  27                                       on_load_complete, NULL);

2024-10-23 11:37