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.
Jansson 2.2.1 has been released. This
release fixes a major bug and little documentation and style issues.
The bug has to do with locales: Jansson's encoder and decoder both
failed hard on real numbers when the locale's decimal separator was
not the standard one. Furthermore, the decoder issued invalid error
messages in some cases under non-UTF-8 locales.
The full release notes are
available here.
Jansson 2.2 has been released. This release
adds one new encoding
function, json_dump_callback()
,
and fixes some minor bugs and documentation glitches. The full
release notes are
available here.
The new encoding function makes it possible to send encoder's output
to a callback function. Here's an example:
#include <jansson.h>
/* Print the buffer's contents. */
int callback(const char *buffer, size_t size, void *x) {
printf("%.*s\n", size, buffer);
return 0;
}
int main() {
json_t *root = json_pack("{s:s, s:i}", "greeting", "Hello, World!", "number", 42);
json_dump_callback(root, callback, NULL, 0);
return 0;
}
The third parameter to json_dump_callback()
(NULL
in this case) is passed through to the callback
as x
.
Thanks to Jonathan Landis for the initial patch!