digip.org blog

Jansson 2.3 released

By Petri Lehtinen on 2012-01-27

Jansson 2.3 has been released. This release adds new features and fixes some minor bugs and documentation issues. The full release notes are available here.

New features

New syntax for optional object keys was added to unpacking functions. For example, this call:

/* obj is a JSON object */
json_unpack(obj, "{s?i}", "foo", &myint)

only writes to myint if the key foo exists in obj.

New functions, json_object_update_existing() and json_object_update_missing() were added. They work like json_object_update(), but only update existing object keys or add new keys, respectively.

json_object_foreach() macro was added for convenient iteration over objects. For example, the following code prints all keys in an object:

/* obj is a JSON object */
const char *key;
json_t *value;

json_object_foreach(obj, key, value) {
    printf("Found key: %s\n", key);
}

The macro expands to an ordinary for loop, and its performance is comparable to hand-written iteration code. It's now also used internally in many places to replace old hand-written loops. Thanks to Marco Aurélio for the idea and initial implementation!

When decoding JSON, the number of bytes read from the input is now stored to error.position even if on success. This makes it possible to use the JSON_DISABLE_EOF_CHECK to decode multiple JSON texts from a single input also when decoding from string with json_loads() or json_loadb(). Before this change, it was only possible when decoding from a file stream using json_loadf(), because the file position could be used to determine where reading stopped.

Jansson can now decode any JSON value, not only arrays or objects. This support can be enabled with the new JSON_DECODE_ANY decoding flag. Note that this violates strict RFC 4627 conformance, so it should be used with caution. There are also some caveats when dealing with decoding errors. See the documentation for details. Patch by Andrea Marchesini.

Bug fixes

Each JSON object has an internal serial number that is used to record the addition order of keys. It's now reset when json_object_clear() is called to avoid it growing out of bounds for long-living objects. Handling of large serial numbers also now works better when encoding.

All decoding functions now properly return NULL when the first argument is NULL. Patch by Andrea Marchesini.

Obsolete leading + and zeros in exponents aren't written anymore when encoding real numbers. Jansson now also compiles and runs correctly on MinGW.