JCON

Years ago during my tenure at MongoDB I worked on a couple libraries. Notably the libbson and mongo-c-driver libraries. One neat feature we had was this concept called BCON which stood for BSON C Object Notation. It was a succinct format for creating BSON documents that made it easier to reason about what you were creating.

So I decided to do the same thing this evening around json-glib because I found I was writing a lot of code to create objects/arrays/etc.

It looks something like this

[code lang=”c”]
g_autoptr(JsonNode) params = NULL;

params = JCON_NEW (
"changes", "["
"{",
"uri", JCON_STRING (src_uri),
"type", JCON_INT (FILE_CHANGE_TYPE_DELETED),
"}",
"{",
"uri", JCON_STRING (dst_uri),
"type", JCON_INT (FILE_CHANGE_TYPE_CREATED),
"}",
"]"
);
[/code]

Since this uses va_list it’s technically less type safe than your other options. But it uses some magic struct initializers to get things in a situation where we can bail at runtime if you did something wrong.

The other half that I’m currently still missing is the extraction support. If you replace JCON_NEW() with JCON_EXTRACT(node) and the values with pointers to values, you can quickly extract documents.

But that’s not done yet… so for next time…

jcon.h jcon.c