akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 808 forks source link

Unpack default value if key missing #621

Open Zer0-One opened 2 years ago

Zer0-One commented 2 years ago

A feature that I would find useful at the moment is the ability to unpack a given default value when the specified key is missing. You could add a double ? as a format specifier, and then expect two arguments instead of one, the second being the default to "unpack" instead of the missing value.

Something like this: int unpk = json_unpack_ex(my_object, &error, 0, "{s??i}", "foo", 42, &i);

akheron commented 2 years ago

This sounds like a useful feature, although using a single character that's something else than ? might be easier implementation-wise.

Would you be interested in implementing this?

Zer0-One commented 2 years ago

I'm taking a look at it. Having a bit of trouble wrapping my head around the code, but I'll get back to you on that.