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).
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.
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.