Looking for json-glib c examples

Can anyone point me to some examples or tutorials on using json-glib?

The API documentation doesn’t explain how all the calls go together.
I found an out of date Gnome wiki with a couple of examples but these do not compile with the latest version and is not very comprehensive.

My task is to read and write some data files (with some configuration metadata).

thanks

printing generated JSON (a write example)

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

static void pinger_print_json(gboolean pretty) {
  JsonGenerator *gen = json_generator_new();
  if (gen) {
    JsonNode *node = json_node_new(JSON_NODE_OBJECT);
    if (node) {
      JsonObject *obj = json_object_new();
      if (obj) {
        json_node_take_object(node, obj);
        if (pinger_json_mainbody(obj, pretty)) {  // <--- add a lot of data into `obj'
          g_object_set(gen, "pretty", pretty, NULL);
          json_generator_set_root(gen, node);
          size_t len; char *json = json_generator_to_data(gen, &len);
          if (json && len) g_print("%s\n", json);  // <--- print generated json
          else g_warning("json_generator_to_data() failed");
          g_free(json);
        }
      } else g_warning("json_object_new() failed");
      json_node_free(node);
    } else g_warning("json_node_new() failed");
    g_object_unref(gen);
  } else g_warning("json_generator_new() failed");
}

p.s. maybe some warnings are excessive

Mainly because JSON requires custom parsing for everything, so any example would be different from what you normally would use for your own code.

The wiki is being retired, and ideally everything should be moved into the API reference itself.

The JsonReader reference has an example of cursor-based parsing a JSON fragment. Similarly, the JsonBuilder reference has an example of building the same JSON fragment programmatically.

If you have more questions, you can ask them on this forum: I’m happy to answer them.

1 Like

I’m storing engineering data at double precision (IEEE 754).
There’s no printf like formatting string with the library.
Do you know if json_builder_add_double_value and json_reader_get_double_value preserve the 64 bit precision (11 bits exponent / 52 bit fraction)?
thanks,
Michael

That’s a problematic question, because RFC 8259 does not specify the precision or range of a number with a fractional component:

This specification allows implementations to set limits on the range and precision of numbers accepted.

It does say, though:

Since software that implements IEEE 754 binary64 (double precision) numbers [IEEE754] is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision.

JSON-GLib will take C double precision floating points and serialise them using g_ascii_dtostr(), which is IEEE754-compatible; when parsing, it’ll use g_ascii_strtod().

In general, if you really want to maintain the same level of precision, and you control both ends of the communication channel, you can convert numbers to and from strings yourself; if you are interoperating with other producers/consumers of the JSON data, then you should be able to rely on IEEE754 floating point representations with JSON-GLib.

Yes, I thought about the converting to a string but I see that g_ascii_dtostr() says
“This function generates enough precision that converting the string back using g_ascii_strtod() gives the same machine-number (on machines with IEEE compatible 64bit doubles).” … which is just what I need!
thanks.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.